Pagini recente » Cod sursa (job #934542) | Cod sursa (job #3172704) | Cod sursa (job #1792539) | Borderou de evaluare (job #1797789) | Cod sursa (job #1905999)
#include <cstdio>
using namespace std;
int t[100005];
int h[100005];
int findMD(int x)
{
while(x!=t[x])
x=t[x];
return x;
}
void unionMD(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,m,i,tx,ty,cod,x,y;
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",&cod);
scanf("%d%d",&x,&y);
tx=findMD(x);
ty=findMD(y);
if(tx!=ty && cod==1)
unionMD(tx,ty);
if(cod==2){
if(tx==ty)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}