forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSU.cpp
More file actions
78 lines (67 loc) · 1.73 KB
/
Copy pathDSU.cpp
File metadata and controls
78 lines (67 loc) · 1.73 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
#include <iostream>
#include <set>
#include <iterator>
#include <bitset>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod=1000000007;
struct unionfind{
int n,setsize,*rank,*parent;
unionfind(int a){
n=a;setsize=a;
rank =new int[n+1];
parent = new int[n+1];
for(int i=1;i<=n;i++){
rank[i] = 1;
parent[i] = i;
}
}
int find(int x){
if(parent[x]!=x) return parent[x] = find(parent[x]);
else return x;
}
void merge(int x,int y){
int xrep=find(x);int yrep=find(y);
if(xrep!=yrep){
if(rank[xrep] >= rank[yrep]){
parent[yrep] = xrep;
rank[xrep]+=rank[yrep];
}
else{
parent[xrep] = yrep;
rank[yrep]+= rank[xrep];
}
setsize-=1;
}
}
};
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin>>t;
while(t--){
//ENTER NUMBER OF NODES as n
//ENTER NUMBER OF QUESRIES as q
int n,q;
cin>>n>>q;
struct unionfind U(n);
//TWO TYPES OF QUESRIES
//QUERY TYPE - 0, y, z -> merge(y,z)
//QUERY TYPE - 1, y, z -> indicate if they are in the same set
while(q--){
int x,y,z;
cin>>x>>y>>z;
if(!x) U.merge(y,z);
else {
int yrep = U.find(y);
int zrep = U.find(z);
if(yrep==zrep)
cout<<1<<endl;
else
cout<<0<<endl;
}
}
}
return 0;
}