-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnionFind.cpp
47 lines (42 loc) · 1.07 KB
/
UnionFind.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
class UnionFind {
// Weighted Quick Union With Path Compression
public:
UnionFind(int n) {
this->id.assign(n, 0);
for (int i = 0; i < n; ++i)
this->id[i] = i;
this->w.assign(n, -1);
}
public:
// id
vector<int> id;
// weight
vector<int> w;
public:
int root(int i) {
// path compression, flat tree, avoid deep tree
while (i != id[i]) {
id[i] = id[id[i]];
i = id[i];
}
return i;
}
bool find(int p, int q) {
return root(p) == root(q);
}
// weighted quick union, O(N+M lg*N), where lg*N represents number of times needed to
// take log of a number unitil reaching 1, almost constant, meaning complexity O(N+M)
// https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
void unite(int p, int q) {
auto i = root(p);
auto j = root(q);
if (w[i] < w[j]) {
id[i] = j;
w[j] += w[i];
}
else {
id[j] = i;
w[i] += w[j];
}
}
};