Pagini recente » Cod sursa (job #406810) | Cod sursa (job #1407347) | Cod sursa (job #585701) | Cod sursa (job #1800800) | Cod sursa (job #1500373)
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cassert>
using namespace std;
#define MAXN 100002
int N, M, T[MAXN], Rank[MAXN];
inline int ROOT(int x) {
int root = x, aux;
for (; T[root]; root = T[root]);
for (; T[x]; aux = T[x], T[x] = root, x = aux);
return root;
}
inline void JOIN(int x, int y) {
if (Rank[x] < Rank[y])
T[x] = y;
else {
T[y] = x;
if (Rank[x] == Rank[y]) ++Rank[x];
}
}
int main()
{
assert(freopen("disjoint.in", "r", stdin));
freopen("disjoint.out", "w", stdout);
int x, y, root1, root2, type;
scanf("%d%d", &N, &M);
while (M--) {
scanf("%d%d%d", &type, &x, &y);
root1 = ROOT(x), root2 = ROOT(y);
if (type == 1) JOIN(root1, root2), ROOT(x), ROOT(y);
else root1 == root2 ? puts("DA") : puts("NU");
}
return 0;
}