Repository navigation
Expand file tree
/
Copy pathdisjoint_sets.cpp
More file actions
101 lines (91 loc) · 2.13 KB
/
Copy pathdisjoint_sets.cpp
File metadata and controls
101 lines (91 loc) · 2.13 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
int num_nodes; //num_nodes is the total number of elements
struct node
{
int rank =-1; int val=-1; struct node* parent=NULL;
};
struct node* make_set( int num )
{
struct node* t = new node;
t->val = num; t->rank=1; t->parent = t;
return t;
}
struct node* get_parent(struct node* t)
{
while(t->parent->val!=t->val) t=t->parent;
return t;
}
void do_union(struct node* point_to_node[], int a, int b)
{
struct node *node1, *node2;
struct node *p_a = get_parent(point_to_node[a-1]), *p_b = get_parent(point_to_node[b-1]);
if (p_a->rank >= p_b->rank) { node1=p_a; node2=p_b; }
else { node1=p_b; node2=p_a; }
node2->parent=node1;
if (node1->rank == node2->rank) node1->rank = node1->rank + 1;
}
int handle_operations()
{
cin>>num_nodes; struct node* point_to_node[num_nodes];
for(int i=0;i<num_nodes;i++)
point_to_node[i] = make_set((i+1));
//array with pointers to all nodes has been created
//all nodes have been initialized as singleton sets
char ch=' ';
while ((ch=fgetc(stdin))!=EOF && ch!='N')
{
if (ch=='?')
{
int a,b; cin>>a>>b;
if (a<1 || a>num_nodes || b<1 || b>num_nodes ) cout<<"-1\n";
else
{
struct node* p_a = get_parent(point_to_node[a-1]);
struct node* p_b = get_parent(point_to_node[b-1]);
if (p_a->val == p_b->val) cout<<"1\n";
else cout<<"0\n";
}
}
else if (ch=='S')
{
int a; cin>>a;
if (a>=1 && a<=num_nodes) cout<<get_parent(point_to_node[a-1])->val<<"\n";
else cout<<"-1\n";
}
else if (ch=='R')
{
int a; cin>>a;
if (a>=1 && a<=num_nodes) cout<<point_to_node[a-1]->rank<<"\n";
else cout<<"-1\n";
}
else if (ch=='U')
{
int a,b; cin>>a>>b;
if (a>=1 && a<=num_nodes && b>=1 && b<=num_nodes) do_union(point_to_node, a, b);
}
}
//freeing the used memory for list here
for(int i=0;i<num_nodes;i++)
{
struct node *t1 = point_to_node[i];
point_to_node[i]=NULL;
delete t1;
}
if (ch=='N') return 1;
else return -1;
}
int main()
{
char ch=' ';
while ((ch=fgetc(stdin))!=EOF)
{
if (ch=='N')
{
int res=handle_operations();
while (res==1)
res=handle_operations();
}
}
return 0;
}