Pagini recente » Cod sursa (job #1171329) | Cod sursa (job #2030456) | Cod sursa (job #1503707) | Cod sursa (job #1187374) | Cod sursa (job #2491384)
#include <cstdio>
using namespace std;
int t[1005],h[1005],n;
int findset(int x)
{
while (x!=t[x])
x=t[x];
return x;
}
void unionset(int tx,int ty)
{
if(h[tx]>h[ty])
t[ty]=tx;
if(h[ty]>h[tx])
t[tx]=ty;
if(h[tx]==h[ty])
{
t[ty]=tx;
h[tx]++;
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int i,x,y,tx,ty,m,c;
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",&c,&x,&y);
tx=findset(x);
ty=findset(y);
if(c==1 && tx!=ty)
unionset(tx,ty);
else
{
if(tx==ty)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}