Pagini recente » Cod sursa (job #246841) | Cod sursa (job #3201517) | Cod sursa (job #1063635) | Cod sursa (job #1475732) | Cod sursa (job #1906019)
#include <cstdio>
using namespace std;
int t[1000005],h[1000005];
int findMS(int x)
{
while(x!=t[x])x=t[x];
return x;
}
void unionMS(int tx,int ty)
{
if(h[tx]==h[ty])
{
h[tx]++;
t[ty]=tx;
}
else
if(h[tx]>h[ty])
t[ty]=tx;
else
t[tx]=ty;
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n , x,y,i,l,op;
scanf("%d %d",&n,&l);
for(i=1;i<=n;i++)t[i]=i,h[i]=1;
for(i=1;i<=l;i++)
{
scanf("%d%d%d",&op,&x,&y);
if(op==1)
{
int tx=findMS(x),ty=findMS(y);
if(tx!=ty)unionMS(tx,ty);
}
else
{
int tx=findMS(x),ty=findMS(y);
if(tx==ty)printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}