-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSU.cpp
46 lines (40 loc) · 1.1 KB
/
DSU.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
//use class
class DisjointSets {
private:
vector<int> parents;
vector<int> sizes;
public:
DisjointSets(int size) : parents(size), sizes(size, 1) {
for (int i = 0; i < size; i++) { parents[i] = i; }
}
int find(int x) {
return parents[x] == x ? x : (parents[x] = find(parents[x]));
}
bool unite(int x, int y) {
int x_root = find(x);
int y_root = find(y);
if (x_root == y_root) { return false; }
if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
sizes[x_root] += sizes[y_root];
parents[y_root] = x_root;
return true;
}
bool connected(int x, int y) { return find(x) == find(y); }
};
//use struct
template <int SZ> struct DSU {
int par[SZ], sz[SZ];
DSU() { for (int i=0;i<=SZ;++i) par[i] = -1, sz[i] = 1; }
int findRoot(int x) { // path compression
if (par[x]==x) return x;
return par[x] = findRoot(par[x]);
}
bool unite(int x, int y) { // union-by-rank
x = findRoot(x), y = findRoot(y);
if (x == y) return 0;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y], par[y] = x;
return 1;
}
bool isConnected(int x, int y) { return findRoot(x) == findRoot(y); }
};