Pagini recente » Cod sursa (job #2269867) | Cod sursa (job #179080) | Cod sursa (job #1849769) | Cod sursa (job #372247) | Cod sursa (job #1016066)
#include<cstdio>
using namespace std;
int t[100005],h[100005];
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[y]=x;
else
t[x]=y;
}
int main()
{
int n,m,tx,ty,i,cod,x,y;
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);
tx=findset(x);
ty=findset(y);
if(cod==1)
if(tx!=ty)
unionset(tx,ty);
if(cod==2)
if(tx==ty)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}