Pagini recente » Cod sursa (job #1239509) | Cod sursa (job #1416854) | Cod sursa (job #2796402) | Cod sursa (job #897459) | Cod sursa (job #1677320)
#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])
{
t[y]=x;
h[x]++;
}
else
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,i,op,x,y,a,b;
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",&op,&x,&y);
if(op==1)
{
a=findset(x);
b=findset(y);
unionset(a,b);
}
else
{
if(findset(x)==findset(y))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}