Pagini recente » Cod sursa (job #44946) | Cod sursa (job #1349259) | Cod sursa (job #1379528) | Cod sursa (job #1979520) | Cod sursa (job #263630)
Cod sursa(job #263630)
#include <stdio.h>
#include <stdlib.h>
long *a, *b, i;
long query(long x)
{
long k = x, p = 0;
while (a[x]!=-1)
x = a[x];
while (a[k]!= x && a[k]!=-1)
{
p = k;
k = a[k];
a[p] = x;
}
return x;
}
int main()
{
long n, m, i, x, y, c;
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w", stdout);
scanf("%ld %ld", &n, &m);
a = new long[n+1];
// int *b = new long[n+1];
for (i = 1; i <= n; ++ i)
a[i] = -1;
for (i = 1; i <= m; ++ i)
{
scanf("%ld %ld %ld", &c, &x, &y);
if (c == 1)
{
x = query(x);
y = query(y);
a[x] = y;
}
else
{
if (query(x)==query(y))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}