-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSU.cpp
More file actions
44 lines (37 loc) · 781 Bytes
/
DSU.cpp
File metadata and controls
44 lines (37 loc) · 781 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class DSU {
public:
vector<int> parent, weight;
DSU(int n) {
parent.resize(n + 1);
weight.resize(n + 1, 1);
for (int i = 1; i <= n; i++)
parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
void join(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (weight[a] < weight[b])
swap(a, b);
parent[b] = a;
weight[a] += weight[b];
}
}
bool same(int a, int b) {
return find(a) == find(b);
}
int size(int x) {
return weight[find(x)];
}
};
int main()
{
return 0;
}