-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2606.java
More file actions
84 lines (63 loc) · 1.93 KB
/
BOJ2606.java
File metadata and controls
84 lines (63 loc) · 1.93 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
import java.io.*;
public class BOJ2606 {
static int vertexCount, edgeCount, answerCount;
static List<Integer>[] graph;
static boolean[] visited;
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
vertexCount = Integer.parseInt(br.readLine());
edgeCount = Integer.parseInt(br.readLine());
visited = new boolean[vertexCount+1];
graph = new List[vertexCount+1];
for(int i = 1; i < graph.length; i++){
graph[i] = new ArrayList<Integer>();
}
for(int i = 0 ; i < edgeCount; i++){
st = new StringTokenizer(br.readLine());
int vertex1 = Integer.parseInt(st.nextToken());
int vertex2 = Integer.parseInt(st.nextToken());
graph[vertex1].add(vertex2);
graph[vertex2].add(vertex1);
}
answerCount = 0;
}
public static void main(String[] args) throws IOException {
input();
process();
}
private static void process(){
// dfs(1);
bfs(1);
System.out.println(answerCount-1);
}
private static void bfs(int startVertex){
Queue<Integer> queue = new LinkedList<>();
queue.add(startVertex);
visited[startVertex] = true;
while(!queue.isEmpty()){
answerCount++;
int nowVertex = queue.poll();
for(int i : graph[nowVertex]){
if(!visited[i]){
visited[i] = true;
queue.add(i);
}
}
}
}
private static void dfs(int vertex){
visited[vertex] = true;
answerCount++;
for(int i : graph[vertex]){
if(!visited[i]){
dfs(i);
}
}
}
}
/*
바이러스
https://www.acmicpc.net/problem/2606
*/