-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_11724
More file actions
68 lines (56 loc) · 1.59 KB
/
BAEKJOON_11724
File metadata and controls
68 lines (56 loc) · 1.59 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
import java.io.*;
import java.util.*;
public class Main {
public static List<Integer>[] list;
public static boolean[] v;
public static int count=0;
public static void main(String[] args) throws IOException {
/**
* 6 5 //N= 6 연결 개수 M =5
* 1 2
* 2 5
* 5 1
* 3 4
* 4 6
*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st =new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
list = new ArrayList[N+1];
v= new boolean[N+1];
for(int i=0; i<N+1; i++){
list[i]=new ArrayList<>();
}
for(int i=0; i<M; i++){
st=new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
list[x].add(y);
list[y].add(x);
}
for(int i=1; i<=N; i++){
if(!v[i]){
count++;
dfs(i);
//bfs(i);
}
}
bw.write(String.valueOf(count));
bw.flush();
bw.close();
}
public static void dfs(int start){
if(v[start]){
return;
}
v[start]=true;
for(int i : list[start]){
int next = i;
if(v[next]==false){
dfs(next);
}
}
}
}