Cod sursa(job #833060)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int T[100010], H[100010];
int find (int x)
{
if (x == T[x])
return x;
return find (T[x]);
}
void unite (int x, int y)
{
if (H[x] == H[y]){
H[x] ++;
T[y] = x;
}
else
if (H[x] > H[y])
T[y] = x;
else
T[x] = y;
}
int main ()
{
int N, M, tx, ty, i, x, y, op;
in >> N >> M;
for (i = 1; i <= N; i ++)
T[i] = i;
while (M --){
in >> op >> x >> y;
tx = find (x);
ty = find (y);
if (op == 1)
unite (tx, ty);
else
if (tx == ty)
out << "DA\n";
else
out << "NU\n";
}
return 0;
}