-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdisjoint-set.cpp
More file actions
58 lines (49 loc) · 1.33 KB
/
disjoint-set.cpp
File metadata and controls
58 lines (49 loc) · 1.33 KB
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
# include <bits/stdc++.h>
using namespace std;
// Disjoint set data structure implemented using union by rank and path comptression
class disjoint_set{
private:
int *parent;
int *rank;
int size;
public:
disjoint_set(int n){
this->parent = new int[n];
this->rank = new int[n];
this->size = n;
for(int i=0; i<n; i++){
this->parent[i] = i;
this->rank[i] = 0;
}
}
int find(int a){
if(a != this->parent[a])
this->parent[a] = this->find(this->parent[a]);
return this->parent[a];
}
void Union(int a, int b){
int a_id = find(a);
int b_id = find(b);
if(a_id == b_id)
return;
if(rank[a_id] > rank[b_id])
parent[b_id] = a_id;
else{
parent[a_id] = b_id;
if(rank[a_id] == rank[b_id])
rank[b_id] = 1 + rank[b_id];
}
}
};
int main(){
disjoint_set *s = new disjoint_set(11);
s->Union(2, 4);
s->Union(4, 6);
s->Union(6, 8);
s->Union(3, 9);
s->Union(0, 1);
s->Union(1, 10);
for(int i=0; i<=10; i++)
cout << i << " -> " << s->find(i) << endl;
return 0;
}