-
Notifications
You must be signed in to change notification settings - Fork 0
/
Union Find - Disjoint Set Learning.cpp
170 lines (152 loc) · 3.09 KB
/
Union Find - Disjoint Set Learning.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<bits/stdc++.h>
using namespace std;
int findParent(int i,int parent[])
{
if(parent[parent[i]]!=parent[i])
parent[i]=findParent(parent[i],parent);
return parent[i];
}
void unionNodes(int a,int b,int parent[],int size[])
{
int parent_a=findParent(a,parent),parent_b=findParent(b,parent);
if(parent_a==parent_b)
return;
if(size[parent_a]>=size[parent_b])
{
size[parent_a]+=size[parent_b];
parent[parent_b]=parent_a;
}
else
{
size[parent_b]+=size[parent_a];
parent[parent_a]=parent_b;
}
return;
}
int main()
{
int N,M,i,a,b;
scanf(" %d %d",&N,&M);
int parent[100001]={0},size[100001]={0};
for(i=1;i<=N;i++)
{
parent[i]=i;
size[i]=1;
}
for(i=1;i<=M;i++)
{
scanf(" %d %d",&a,&b);
unionNodes(a,b,parent,size);
}
for(i=1;i<=N;i++)
printf("Node %d belongs to connected component %d\n",i,findParent(i,parent));
long long ways=1;
int nos=0;
for(i=1;i<=N;i++)
{
if(findParent(i,parent)==i)
{
printf("%d leader %d size\n",i,size[i]);
nos++;
}
}
printf("Total connected components : %d",nos);
return 0;
}
/*int nodes[10];
int rankof[10];
int findnode(int a, int nodes[])
{
if(nodes[nodes[a]]!=nodes[a])
nodes[a] = findnode(nodes[a], nodes);
return nodes[a];
}
void makeunion(int a, int b, int nodes[], int rankof[])
{
int u = findnode(a, nodes);
int v = findnode(b, nodes);
//cout<<"GOT="<<u<<' '<<v<<endl;
if(u==v) return;
if(rankof[u]>=rankof[v])
{
rankof[u] = rankof[u] + rankof[v];
nodes[v] = u;
}
else
{
rankof[v] = rankof[v] + rankof[u];
nodes[u] = v;
}
return;
}
int main()
{
//init();
int x, y;
memset(nodes,0,sizeof(nodes));
memset(rankof,0,sizeof(rankof));
for(int i=1; i<=10; i++)
{ nodes[i] = i; rankof[i] = 1; }
for(int i=1; i<=7; i++)
{
scanf("%d %d", &x, &y);
makeunion(x,y, nodes, rankof);
}
for(int i=1; i<10; i++)
cout<<findnode(i, nodes)<<endl;
return 0;
}*/
/*2 4
2 3
2 1
3 6
6 7
4 5
8 9
#include<bits/stdc++.h>
using namespace std;
int nodes[10];
int rankof[10];
void init()
{
for(int i=1; i<=10; i++)
{ nodes[i] = i; rankof[i] = 1; }
}
int findnode(int a, int nodes[])
{
if(nodes[nodes[a]]!=nodes[a])
nodes[a] = findnode(nodes[a], nodes);
return nodes[a];
}
void makeunion(int a, int b, int nodes[], int rankof[])
{
int u = findnode(a, nodes);
int v = findnode(b, nodes);
//cout<<"GOT="<<u<<' '<<v<<endl;
if(u==v) return;
if(rankof[u]>=rankof[v])
{
rankof[u] = rankof[u] + rankof[v];
nodes[v] = u;
}
else
{
rankof[v] = rankof[v] + rankof[u];
nodes[u] = v;
}
return;
}
int main()
{
init();
int x, y;
for(int i=1; i<=7; i++)
{
scanf("%d %d", &x, &y);
makeunion(x,y, nodes, rankof);
}
for(int i=1; i<10; i++)
cout<<findnode(i, nodes)<<endl;
return 0;
}
*/