Repository navigation
Expand file tree
/
Copy pathBOJ_2606.java
More file actions
56 lines (42 loc) · 1.44 KB
/
Copy pathBOJ_2606.java
File metadata and controls
56 lines (42 loc) · 1.44 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
48
49
50
51
52
53
54
55
56
package baekjoon.silver;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_2606 {
static int[][] graph;
static boolean[] isVirus;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine()); // 컴퓨터의 수
int P = Integer.parseInt(br.readLine()); // 연결된 컴퓨터 쌍의 수
graph = new int[N + 1][N + 1];
isVirus = new boolean[N + 1];
for (int i = 0; i < P; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
graph[a][b] = 1;
graph[b][a] = 1;
}
bfs(1);
}
private static void bfs(int start) {
Queue<Integer> q = new LinkedList<>(); // 바이러스에 걸린 컴퓨터
q.offer(start);
isVirus[start] = true;
int count = 0;
while(!q.isEmpty()) {
int turn = q.poll();
for (int i = 1; i < graph.length; i++) {
if(graph[turn][i] == 1 && !isVirus[i]) {
isVirus[i] = true;
q.offer(i);
count++;
}
}
}
System.out.println(count);
}
}