Created
July 29, 2018 19:06
-
-
Save anshumanc007/1d4b08e9f3d94a4ae46aa5be090a1464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
long long int fa(long long int par[],long long int a) | |
{ | |
if(par[a]==-1) | |
{ | |
return a; | |
} | |
else | |
{ | |
return par[a]; | |
} | |
} | |
void load(long long int par[],long long int rank[],long long int a,long long int b) | |
{ | |
a=fa(par,a); | |
b=fa(par,b); | |
if(rank[a]>rank[b]) | |
{ | |
par[b]=a; | |
} | |
else if(rank[b]>rank[a]) | |
{ | |
par[a]=b; | |
} | |
else | |
{ | |
rank[b]++; | |
par[a]=b; | |
} | |
} | |
void check(long long int par[],long long int rank[],long long int a,long long int b) | |
{ | |
a=fa(par,a); | |
b=fa(par,b); | |
if(a==b) | |
{ | |
cout<<"YES"<<endl; | |
} | |
else | |
{ | |
cout<<"NO"<<endl; | |
} | |
} | |
int main() | |
{ | |
long long int q,t,i,j,k,l,m,n,o,p,ans,a,b; | |
cin>>n>>q; | |
long long int par[n+1]; | |
long long int rank[n+1]; | |
for(i=1;i<=n;i++) | |
{ | |
par[i]=-1; | |
rank[i]=1; | |
} | |
while(q--) | |
{ | |
cin>>k; | |
if(k==0) | |
{ | |
cin>>a>>b; | |
load(par,rank,a,b); | |
} | |
else{ | |
cin>>a>>b; | |
check(par,rank,a,b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment