Pagini recente » Cod sursa (job #1060927) | Cod sursa (job #747825) | Cod sursa (job #1838581) | Cod sursa (job #1106329) | Cod sursa (job #1039129)
#include <stdio.h>
int t[10001],h[10001];
int findset(int x)
{
while(t[x]!=x)
{
x=t[x];
}
return x;
}
void unionset(int x,int y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
{
if(h[x]<h[y])
{
t[x]=y;
}
else
{
t[y]=t[x];
}
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(int i=0;i<m;i++)
{
int a,x,y;
scanf("%d%d%d",&a,&x,&y);
if(a==1)
{
unionset(findset(x),findset(y));
}
else
{
if(findset(x)==findset(y))
{
printf("DA\n");
}
else
{
printf("NU\n");
}
}
}
return 0;
}