-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam5.cpp
66 lines (60 loc) · 1.08 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
#include <iostream>
#include <cstring> //memset
#include <algorithm>
#include <bitset>
#include <string>
#include <vector>
#include <utility>
#include <cstdio>
#include <queue>
#include <cmath>
#include <list>
#include <set>
#include <map>
using namespace std;
int parent[1000000];
int wt[1000000];
void initialise (int sz) {
for (int i = 0; i < sz; ++i) {
parent[i] = i;
wt[i] = 1;
}
}
int root(int a) {
while (parent[a] != a) {
parent[a] = parent[parent[a]];
a = parent[a];
}
return a;
}
void union_(int a, int b) {
if (wt[a] <= wt[b]) {
parent[root(a)] = parent[root(b)];
wt[b] += wt[a];
}
else {
parent[root(b)] = parent[root(a)];
wt[a] += wt[b];
}
}
int main() {
freopen("input.txt", "r", stdin);
int test_cases, nodes, edges, a, b, res;
cin >> test_cases;
while (test_cases--) {
res = 0;
cin >> nodes >> edges;
initialise(nodes);
while (edges--) {
cin >> a >> b;
union_(a, b);
}
// for (int i = 0; i < nodes; ++i)
// cout << parent[i] << " ";
for (int i = 0; i < nodes; ++i)
if (i == root(i))
res++;
cout << res << endl;
}
return 0;
}