-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam5.cpp
53 lines (53 loc) · 1.17 KB
/
cam5.cpp
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
#include<cstdio>
#include<cstring>
struct Node {
int rank=0;
int parent=-1;
};
int findParent(Node* array, int a) {
if(array[a].parent != -1) {
array[a].parent = findParent(array, array[a].parent);
return array[a].parent;
} else {
return a;
}
}
void unin(Node* array, int a, int b) {
int p1 = findParent(array, a);
int p2 = findParent(array, b);
if(p1 == p2) {
return;
}
if(array[p1].rank > array[p2].rank) {
array[p2].parent = p1;
} else if(array[p1].rank < array[p2].rank) {
array[p1].parent = p2;
} else {
array[p2].parent = p1;
array[p1].rank++;
}
}
int main() {
int test;
scanf("%d", &test);
while(test--) {
int n;
scanf("%d", &n);
Node *array = new Node[n+1];
int e;
scanf("%d", &e);
for(int i=0;i<e;++i) {
int a, b;
scanf("%d %d", &a, &b);
unin(array, a+1, b+1);
}
int count =0;
for(int i=1;i<n+1;++i) {
if(array[i].parent == -1) {
count++;
}
}
delete [] array;
printf("%d\n", count);
}
}