forked from gojung/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_1238.java
More file actions
47 lines (47 loc) · 1.73 KB
/
java_1238.java
File metadata and controls
47 lines (47 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//SWEA 1238 [S/W ¹®Á¦ÇØ°á ±âº»] 10ÀÏÂ÷ - Contact(D4)
import java.io.*;
import java.util.*;
public class java_1238{
static int[][] contact;
static int[] visit;
static HashMap<Integer, ArrayList<Integer>> list;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for(int t = 1; t <= 10; t++){
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int L = Integer.parseInt(st.nextToken());
int X =Integer.parseInt(st.nextToken());
contact = new int[101][101];
visit = new int[101];
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < L/2; i++)
contact[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1;
Deque<Integer> q = new ArrayDeque<>();
q.offer(X);
visit[X] = 1;
while(!q.isEmpty()){
int x = q.poll();
for(int i = 1; i <= 100; i++){
if(contact[x][i] == 1 && visit[i] == 0){
q.offer(i);
visit[i] = visit[x] + 1;
}
}
}
int max = X;
for(int i = 1; i <= 100; i++){
if(visit[i] > visit[max]){
max = i;
}
else if(visit[i] == visit[max]){
if(i > max) max = i;
}
}
sb.append("#" + t + " " + max + "\n");
}
System.out.print(sb);
br.close();
}
}