-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPECFEST Publicity.cpp
More file actions
75 lines (64 loc) · 1.24 KB
/
PECFEST Publicity.cpp
File metadata and controls
75 lines (64 loc) · 1.24 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
69
70
71
72
73
74
75
#include "bits/stdc++.h"
#ifdef LOCAL
#include "F:\CPP\Debug\debug.h"
#else
#define print(...) 1;
#endif
using i64 = long long;
class DSU
{
std::vector<int> rnk, par;
public:
DSU() {}
DSU(int n) : rnk(n, 1), par(n) { std::iota(par.begin(), par.end(), 0); }
int get(int x)
{
while (x != par[x]) x = par[x] = par[par[x]];
return x;
}
bool unite(int x, int y)
{
int p1 = get(x), p2 = get(y);
if (p1 == p2)
return false;
else
{
if (rnk[p2] > rnk[p1])
std::swap(p1, p2);
par[p2] = p1, rnk[p1] += rnk[p2], rnk[p2] = 0;
return true;
}
}
bool same(int x, int y)
{
return get(x) == get(y);
}
int size(int x)
{
return rnk[get(x)];
}
};
void solve()
{
int n, m;
std::cin >> n >> m;
DSU dsu(n);
int cc = n;
for (int i = 0; i < m; i++)
{
int u, v;
std::cin >> u >> v;
cc -= dsu.unite(u, v);
}
std::cout << cc << "\n";
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--)
solve();
return 0;
}