-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_1863_종교_유승아.java
56 lines (41 loc) · 1.1 KB
/
Main_1863_종교_유승아.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_1863_Á¾±³_À¯½Â¾Æ {
public static int[] p;
public static int cnt;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st= new StringTokenizer(bf.readLine(), " ");
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
cnt = n;
p=new int[n+1];
for(int i=1;i<n+1;i++) {
p[i] = i;
}
for(int i=0;i<m;i++) {
st = new StringTokenizer(bf.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
union(a,b);
}
System.out.println(cnt);
}
public static void union(int x,int y){
int px = findSet(x);
int py = findSet(y);
if(px!=py) {
p[py]=px;
cnt--;
}
}
public static int findSet(int x){
if(x==p[x]) return x;
else {
p[x] = findSet(p[x]);
return p[x];
}
}
}