Pagini recente » Cod sursa (job #724451) | Cod sursa (job #3273577) | Cod sursa (job #76641) | Cod sursa (job #3184617) | Cod sursa (job #1142335)
#include<cstdio>
int t[100001],h[100001];
int find(int x)
{
while(x!=t[x])
x=t[x];
return x;
}
void unifica(int x,int y)
{
// x si y sunt radacinile arborilor
if (h[x]>h[y])
t[y]=x;
else
if (h[y]>h[x])
t[x]=y;
else
{
h[x]++;
t[y]=x;
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,tx,ty,x,y,nr=0,i,cas;
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",&cas,&x,&y);
tx=find(x);
ty=find(y);
if (cas==1)
unifica(tx,ty);
else
if (tx==ty)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}