forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisjointset_using_pointers.cpp
68 lines (58 loc) · 1.11 KB
/
Disjointset_using_pointers.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
//A simple implementation of DJS using map data structure.
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
struct node
{
int data;
int rank;
node* parent;
};
map<int,node*> m;
void make_set(int data)
{
node* temp = new node;
temp->data = data;
temp->rank = 0;
temp->parent = temp;
m.insert(make_pair(data,temp));
}
node* find_set(node* temp)
{
if(temp->parent == temp)
return temp->parent;
temp->parent = find_set(temp->parent);
return temp->parent;
}
void union_set(int d1,int d2)
{
node* t1 = find_set(m[d1]);
node* t2 = find_set(m[d2]);
if(t1->data==t2->data)
return ;
if(t1->rank>=t2->rank)
{
t1->rank = (t1->rank==t2->rank)?(t1->rank+1):(t1->rank);
t2->parent = t1;
}
else
t1->parent = t2;
}
int main()
{
int i=0;
for(i=0;i<=9;i++)
{
make_set(i+1);
}
union_set(2,1);
union_set(2,3);
union_set(3,4);
union_set(5,6);
union_set(7,8);
union_set(3,8);
union_set(6,8);
union_set(5,9);
cout<<find_set(m[9])->data<<" ";
return 0;
}