안녕하세요 GALLERY 문제를 풀고 제출시에
RTE(nonzero return code)가 발생합니다.
해당 코드의 알고리즘은
갤러리들을 나타내는 list를 담고 있는 list(junctions)를 생성합니다해당 list들은 각 갤러리들을 나타내기 위해 1번째 인덱스에는 juncions에서 해당 list의 index를 할당합니다.
이후 입력값들을 받아 작은수를 parent로, 큰수를 child로 하여
index가 parent인 list에 child를 add합니다.
즉, 테스트 값으로 나온
6 5
0 1
1 2
1 3
2 5
0 4 의 경우에 제 알고리즘을 적용하면
0 1 4
1 2 3
2 5
라는 3개의 list와 나머지 각각 자신의 index 값을 가진 list가 만들어 집니다.
이후 setCamera() 메소드를 통해 junctions안의 list 값들을 다 체크하여
설치된 카메라수를 계산합니다.
코드는 아래와 같습니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<ArrayList<Integer>> junctions = new ArrayList<ArrayList<Integer>>();
public static boolean[] installs;
public static int cameraCount;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
for(int i=0; i<cases; i++){
String[] inputs = br.readLine().split(" ");
int galleryCount = Integer.parseInt(inputs[0]);
int passageCount = Integer.parseInt(inputs[1]);
installs = new boolean[galleryCount];
for(int j=0; j<galleryCount; j++){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(j);
junctions.add(list);
}
for(int j=0; j<passageCount; j++){
String[] galleries = br.readLine().split(" ");
int parent, child;
int gallery1 = Integer.parseInt(galleries[0]);
int gallery2 = Integer.parseInt(galleries[1]);
if(gallery1 < gallery2){
parent = gallery1;
child = gallery2;
}else{
parent = gallery2;
child = gallery1;
}
junctions.get(parent).add(child);
}
int junctionsSize = junctions.size();
for(int j=0; j<junctionsSize; j++){
setCamera(junctions.get(j));
}
System.out.println(cameraCount);
}
}
public static void setCamera(List<Integer> list){
int size = list.size();
int value;
boolean isChecked = false;
for(int i=0; i<size; i++){
value = list.get(i);
for(int j=0; j<installs.length; j++){
if(installs[value] == false){
isChecked = true;
installs[value] = true;
}
}
}
if(isChecked){
cameraCount++;
}
}
}
jojoldu
안녕하세요 GALLERY 문제를 풀고 제출시에
RTE(nonzero return code)가 발생합니다.
해당 코드의 알고리즘은
갤러리들을 나타내는 list를 담고 있는 list(junctions)를 생성합니다해당 list들은 각 갤러리들을 나타내기 위해 1번째 인덱스에는 juncions에서 해당 list의 index를 할당합니다.
이후 입력값들을 받아 작은수를 parent로, 큰수를 child로 하여
index가 parent인 list에 child를 add합니다.
즉, 테스트 값으로 나온
6 5
0 1
1 2
1 3
2 5
0 4 의 경우에 제 알고리즘을 적용하면
0 1 4
1 2 3
2 5
라는 3개의 list와 나머지 각각 자신의 index 값을 가진 list가 만들어 집니다.
이후 setCamera() 메소드를 통해 junctions안의 list 값들을 다 체크하여
설치된 카메라수를 계산합니다.
코드는 아래와 같습니다.
11년 전