-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob1238.java
56 lines (49 loc) · 1.27 KB
/
prob1238.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
package Algorithm_0804;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
// 인접 행렬을 이용한 구현
public class SW_D4_1238_contact {
public static int MAXN = 100;
public static int[][] contact;
public static int[] visit;
public static int max;
public static void main(String args[]) throws Exception {
System.setIn(new FileInputStream("src/com/ssafy/day07/input.txt"));
Scanner sc = new Scanner(System.in);
int T = 1;// sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
int L = sc.nextInt();
int S = sc.nextInt();
contact = new int[MAXN + 1][MAXN + 1];
visit = new int[MAXN + 1];
for (int i = 0; i < L / 2; i++) {
int f = sc.nextInt();
int t = sc.nextInt();
contact[f][t] = 1;
}
Queue<Integer> queue = new LinkedList<>();
visit[S] = 1;
queue.offer(S);
while (!queue.isEmpty()) {
max = 0;
int size = queue.size();
for (int k = 0; k < size; k++) {
int i = queue.poll();
if (max < i)
max = i;
for (int j = 1; j <= MAXN; j++) {
if (contact[i][j] == 1 && visit[j] == 0) {
visit[j] = 1;
queue.offer(j);
}
}
}
}
System.out.println("#" + tc + " " + max);
}
sc.close();
}
}