-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDSU.cpp
More file actions
64 lines (55 loc) · 943 Bytes
/
DSU.cpp
File metadata and controls
64 lines (55 loc) · 943 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int parent[100000];
int rank1[100000];
void makeSet()
{
for(int i = 0; i <= 100000; i++)
{
parent[i] = i;
rank1[i] = 0;
}
}
int findPar(int node)
{
if(node == parent[node])
{
return node;
}
return parent[node] = findPar(parent[node]);
}
void union1(int u, int v)
{
u = findPar(u);
v = findPar(v);
if(rank1[u]<rank1[v])
{
parent[u] = v;
}
else if(rank1[v] < rank1[u])
{
parent[v] = u;
}
else
{
parent[v] = u;
rank1[u]++;
}
}
int main()
{
makeSet();
int m;
cin >> m;
while(m--)
{
int u, v;
cin >> u >> v;
union1(u, v);
}
if(findPar(5) != findPar(6))
{
cout << "Different Component";
}else cout << "Same Component";
return 0;
}