-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjoint_Set_Union.cpp
60 lines (45 loc) · 1.06 KB
/
Disjoint_Set_Union.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
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
class DSU {
vector<int> rank, parent;
public:
DSU(int n) {
rank.resize(n + 1, 1);
parent.resize(n + 1, 0);
for(int i = 0; i < n + 1; i++) parent[i] = i;
}
int findUltParent(int node) {
if(node == parent[node]) return node;
return parent[node] = findUltParent(parent[node]);
}
void unionByRank(int node1, int node2) {
int p1 = findUltParent(node1);
int p2 = findUltParent(node2);
if(p1 == p2) return;
if(rank[p1] > rank[p2]) {
rank[p1] += rank[p2];
parent[p2] = p1;
} else {
rank[p2] += rank[p1];
parent[p1] = p2;
}
}
};
void testCase() {
DSU dsu(5);
dsu.unionByRank(1, 2);
dsu.unionByRank(2, 3);
dsu.unionByRank(4, 5);
// is there any edge b/w 3 & 4
// cout << dsu.findUltParent(3) << " " << dsu.findUltParent(2) << endl;
if(dsu.findUltParent(3) == dsu.findUltParent(4)) cout << "yes";
else cout << "no";
}
int main() {
int t = 1;
// cin >> t;
while(t--) testCase();
}