Pagini recente » Cod sursa (job #2974440) | Cod sursa (job #581052) | Cod sursa (job #1396964) | Cod sursa (job #541890) | Cod sursa (job #1016053)
#include <cstdio>
using namespace std;
long t[100001],n,m,i,h[100001],cod,x,y,tx,ty;
int find (int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
int unionset(int x, int y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
if(h[x]>h[y])
{
t[y]=x;
}
else{
t[x]=y;
}
}
int main () {
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++){
scanf("%d%d%d",&cod,&x,&y);
ty=find(x);
tx=find(y);
if(cod==1)
if(tx!=ty)
unionset(x,y);
if(cod==2){
if(tx==ty)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}