-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAM5.cpp
71 lines (67 loc) · 1.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
int parent[100005], syz[100005];
int get_parent(int a)
{
int p = a;
while (parent[p] != p)
{
p = parent[parent[p]];
}
return p;
}
void join(int a, int b)
{
int pa = get_parent(a);
int pb = get_parent(b);
if (syz[pa] > syz[pb])
{
parent[pb] = pa;
syz[pa] += syz[pb];
}
else
{
parent[pa] = pb;
syz[pb] += syz[pa];
}
}
void init()
{
int i;
for (i = 0; i < 100005; i++)
{
parent[i] = i;
syz[i] = 1;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t, n, e, a, b, ans, i;
cin >> t;
while (t--)
{
ans = 0;
init();
cin >> n;
cin >> e;
for (i = 0; i < e; i++)
{
cin >> a >> b;
if (get_parent(a) != get_parent(b))
{
join(a, b);
}
}
for (i = 0; i < n; i++)
{
if (parent[i] == i)
ans++;
}
cout << ans << endl;
}
return 0;
}