Pagini recente » Cod sursa (job #1623250) | Cod sursa (job #1288436) | Cod sursa (job #1574479) | Cod sursa (job #880820) | Cod sursa (job #1806372)
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
int tata[100010];
int h[100010];
int caut(int x)
{
if(x==tata[x])
return x;
else
{
tata[x]=caut(tata[x]);
return tata[x];
}
}
void unesc (int x,int y)
{
int a,b;
a=caut(x);
b=caut(y);
if(h[a]>h[b])
tata[a]=b;
else
{
tata[b]=a;
h[b]++;
}
}
int main()
{
freopen ("disjoint.in","r",stdin);
freopen ("disjoint.out","w",stdout);
srand(time(0));
int n,m,x,y,p,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
tata[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&p,&x,&y);
if(p==1)
unesc(x,y);
else if(caut(x)==caut(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}