-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP049_SWEA1238_Contact.java
66 lines (50 loc) · 1.56 KB
/
P049_SWEA1238_Contact.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Queue;
import java.util.ArrayDeque;
// bfs
public class P049_SWEA1238_Contact {
static int[][] graph;
static int[] depth;
static int answer;
public static void bfs(int start) { // bfs의 leaf 노드의 최댓값
Queue<Integer> q = new ArrayDeque<>();
q.offer(start);
depth[start] = 1;
while(!q.isEmpty()) { // q가 비어있을 동안
int cur = q.poll(); // 현재 꺼낸 거
for (int i = 0; i < 101; i++) {
if (graph[cur][i] == 1 && depth[i] == 0) { // 아직 방문 안 했는데, 갈 수 있는 그래프일 경우
q.offer(i);
depth[i] = depth[cur] + 1;
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for (int t = 1; t <= 10; t++) {
answer = -1;
String[] s = br.readLine().split(" ");
int length = Integer.parseInt(s[0]);
int start = Integer.parseInt(s[1]);
s = br.readLine().split(" ");
graph = new int[101][101];
depth = new int[101];
for (int l = 0; l < length; l += 2) {
graph[Integer.parseInt(s[l])][Integer.parseInt(s[l + 1])] = 1; // from - to 연결
}
bfs(start);
int max = 0;
for (int i = 0; i < depth.length; i++) {
if (max <= depth[i]) {
max = depth[i]; // depth
if (answer < i) answer = i; // node 번호 업데이트
}
}
sb.append("#" + t + " " + answer + "\n");
}
System.out.println(sb);
}
}