-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunion_by_rank.cpp
66 lines (56 loc) · 1.58 KB
/
union_by_rank.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
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
class DisjointSet{
vector<int> rank, parent;
public:
DisjointSet(int n){
rank.resize(n + 1, 0);
parent.resize(n + 1, 0);
for (int i = 0; i <= n; i++)
parent[i] = i;
}
// func returns ultimate parent
int findParent(int node) {
if(node == parent[node])
return node;
// path compression is storing the ans
return parent[node] = findParent(parent[node]);
}
void unionByRank(int u, int v){
// finding pu and pv
int ulp_u = findParent(u);
int ulp_v = findParent(v);
// u and v are in same component
if(ulp_u == ulp_v)
return;
// smaller rank get attached to bigger rank
if(rank[ulp_v] > rank[ulp_u])
parent[ulp_u] = ulp_v;
else if (rank[ulp_v] < rank[ulp_u])
parent[ulp_v] = ulp_u;
else {
parent[ulp_v] = ulp_u;
rank[ulp_u]++;
}
}
};
int main()
{
DisjointSet ds(7);
ds.unionByRank(1, 2);
ds.unionByRank(2, 3);
ds.unionByRank(4, 5);
ds.unionByRank(6, 7);
ds.unionByRank(5, 6);
// if 3 and 7 belong to same comp?
if(ds.findParent(3) == ds.findParent(7))
cout << "3 & 7 belong to same component\n";
else
cout << "3 & 7 DO NOT belong to same component\n";
ds.unionByRank(3, 7);
if(ds.findParent(3) == ds.findParent(7))
cout << "3 & 7 belong to same component\n";
else
cout << "3 & 7 DO NOT belong to same component\n";
return 0;
}