-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSU.cpp
79 lines (75 loc) · 1.5 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<stdlib.h>
using namespace::std;
class DSU // Disjoint Set Union
{
int *array; // array representing the parents
int *rank; // array representing the ranks
int get_parent(int index)
{
int parent;
// TODO
if(array[index]==index) return index;
else
{
parent = get_parent(array[index]);
if(array[index]!=parent)
{
rank[array[index]]-=1+rank[index];
}
array[index]=parent;
}
return parent;
}
public:
DSU(int n)
{
array = (int*)malloc(n*sizeof(int));
rank = (int*)malloc(n*sizeof(int));
for(int i=0;i<n;++i)
{
array[i]=i;
rank[i]=0;
}
}
~DSU()
{
free(rank);
free(array);
}
bool find_connected(int i,int j)
{
return get_parent(i)==get_parent(j);
}
void quick_union(int i,int j)
{
if(find_connected(i,j))return; // same set already
else
{
if(get_parent(i) > get_parent(j) || (get_parent(i)==get_parent(j) && i>j) )
{
array[get_parent(j)] = get_parent(i);
rank[get_parent(i)]+=1+rank[get_parent(j)];
}
else
{
array[get_parent(i)] = get_parent(j);
rank[get_parent(j)]+=1+rank[get_parent(i)];
}
}
return;
}
};
int main() // driver to test
{
DSU dsu = DSU(5);
cout<<dsu.find_connected(2,3)<<endl; //false
dsu.quick_union(2,4); // 2 is child of 4
dsu.quick_union(3,1); // 1 is child of 3
cout<<dsu.find_connected(2,4)<<endl; //true
cout<<dsu.find_connected(2,1)<<endl; //false
dsu.quick_union(0,2); // 0 is child of 4
cout<<dsu.find_connected(0,4)<<endl; //true
dsu.quick_union(1,2); // 1 union 2
cout<<dsu.find_connected(1,4)<<endl; // true
}