Pagini recente » Cod sursa (job #3203141) | Cod sursa (job #2391805) | Cod sursa (job #2572408) | Cod sursa (job #451977) | Cod sursa (job #1039122)
#include <cstdio>
using namespace std;
int t[100005],h[100005];
int findset(int x){
while(t[x]!=x)
x=t[x];
return x;
}
void uniset(int x,int y){
// x,y sunt radacini
if(h[x]==h[y]){
h[x]++;
t[y]=x;
}else
if(h[x]<h[y])
t[x]=y;
else
t[y]=x;
}
int main(){
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,x,y,cod,i,xx,yy;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++){
scanf("%d%d%d",&cod,&x,&y);
xx=findset(x);
yy=findset(y);
if(cod==2){
if(xx==yy){
printf("DA\n");
}else{
printf("NU\n");
}
}else{
uniset(xx,yy);
}
}
return 0;
}