Pagini recente » Cod sursa (job #281773) | Cod sursa (job #2412664) | Cod sursa (job #2328192) | Cod sursa (job #2393027) | Cod sursa (job #2838898)
#include <iostream>
#include <stdio.h>
#define MAXN 100005
using namespace std;
int t[MAXN];
int root(int node)
{
int x = node, aux;
while(t[node] > 0)
node = t[node];
while(x != node)
{
aux = t[x];
t[x] = node;
x = aux;
}
return node;
}
void join(int a, int b)
{
int rootA = root(a), rootB = root(b);
if(rootA == rootB)
return;
if(t[rootA] <= t[rootB])
{
t[rootA] += t[rootB];
t[rootB] = rootA;
}
else
{
t[rootB] += t[rootA];
t[rootA] = rootB;
}
}
int main()
{
int n, m, op, a, b, i;
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d ", &n, &m);
for(i = 1; i <= n; i++)
t[i] = -1;
for(i = 1; i <= m; i++)
{
scanf("%d %d %d", &op, &a, &b);
if(op == 1)
join(a, b);
else
{
if(root(a) == root(b))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}