Pagini recente » Cod sursa (job #1056510) | Cod sursa (job #1053273) | Cod sursa (job #3206825) | Cod sursa (job #2673959) | Cod sursa (job #580390)
Cod sursa(job #580390)
#include <stdio.h>
int n,m,x,a,b,t[100001],rg[100001],i;
int unite(int x,int y)
{
if (rg[x]>rg[y])
t[y]=x;
else t[x]=y;
if (rg[x]==rg[y]) rg[y]++;
return 0;
}
int rad(int x)
{
int p=x,y;
while (t[p]!=p) p=t[p];
while (t[x]!=x)
{
y=t[x];
t[x]=p;
x=y;
}
return p;
}
int main(void)
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
{
t[i]=i;
rg[i]=1;
}
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&a,&b);
if (x==1)
{
unite(a,b);
continue;
}
if (rad(a)==rad(b)) printf("DA\n");
else printf("NU\n");
}
return 0;
}