Pagini recente » Cod sursa (job #2255022) | Cod sursa (job #2623474) | Cod sursa (job #2929900) | Cod sursa (job #909486) | Cod sursa (job #679010)
Cod sursa(job #679010)
#include<stdio.h>
long t[100050],a[100050],n,m,x,y,z,t1,t2,i;
void addup(long x1,long x2)
{
if(a[x1]<a[x2])t[x1]=x2;
else
{
t[x2]=x1;
if(a[x1]==a[x2])++a[x1];
}
}
long find(long x)
{
long tt,xx;
xx=x;
while(xx!=t[xx])xx=t[xx];
tt=xx;
while(x!=tt)
{
xx=x;
x=t[x];
t[xx]=tt;
}
return tt;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;++i)t[i]=i,a[i]=1;
for(i=1;i<=m;++i)
{
scanf("%ld%ld%ld",&z,&x,&y);
t1=find(x);
t2=find(y);
if(z==1)addup(t1,t2);
else
if(t1==t2)printf("DA\n");
else printf("NU\n");
}
return 0;
}