Pagini recente » Cod sursa (job #80722) | Cod sursa (job #309351) | Cod sursa (job #2790562) | Cod sursa (job #3245810) | Cod sursa (job #1669011)
#include <cstdio>
#include <cstring>
using namespace std;
int t[100005],h[100005];
int fs(int x)
{
while(x != t[x])
x = t[x];
return x;
}
void us(int x,int y)
{
int tx,ty;
tx = fs(x);
ty = fs(y);
if(tx != ty)
{
if(h[tx] > h[ty])
t[ty] = tx;
else if(h[ty] > h[tx])
t[tx] = ty;
else
{
h[tx]++;
t[ty] = tx;
}
}
}
int main()
{
freopen("disjoint.in", "r",stdin);
freopen("disjoint.out", "w",stdout);
int m,n,i,c,a,b,ta,tb;
scanf("%d%d", &n, &m);
for(i = 1;i <= n;i++)
t[i] = i;
for(i = 1;i <= m;i++)
{
scanf("%d %d %d", &c, &a, &b);
ta = fs(a);
tb = fs(b);
if(c == 1)
{
if(ta != tb)
us(a,b);
}
else if(c == 2)
{
if(ta == tb)
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}