Pagini recente » Cod sursa (job #1159580) | Monitorul de evaluare | Cod sursa (job #168157) | Cod sursa (job #2966748) | Cod sursa (job #2417566)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m,t[100005],r[100005];
int tata(int x){
if(t[x]==x)
return x;
else{
int tt=tata(t[x]);
t[x]=tt;
return tt;
}
}
void reuniune(int x,int y){
int tx=tata(x);
int ty=tata(y);
if(tx!=ty){
if(r[tx]>r[ty])
t[ty]=tx;
else if(r[tx]<r[ty])
t[tx]=ty;
else{
t[tx]=ty;
r[tx]++;
}
}
}
int main(){
cin>>n>>m;
int c,x,y;
for(int i=1;i<=n;i++)
t[i]=i;
for(int i=1;i<=m;i++){
cin>>c>>x>>y;
if(c==1)
reuniune(x,y);
if(c==2){
if(tata(x)==tata(y))
cout<<"DA"<<'\n';
else
cout<<"NU"<<'\n';
}
}
return 0;
}