Pagini recente » Cod sursa (job #2646913) | Cod sursa (job #2586268) | Cod sursa (job #1934245) | Cod sursa (job #2811391) | Cod sursa (job #1163135)
#include <cstdio>
using namespace std;
#define FILEIN "disjoint.in"
#define FILEOUT "disjoint.out"
#define NMAX 100005
int Set[NMAX], Rank[NMAX], N, M;
int Find(int);
void Union(int, int);
void Union(int x, int y) {
int xRoot = Find(x),
yRoot = Find(y);
if (xRoot == yRoot)
return;
if (Rank[xRoot] < Rank[yRoot]) {
Set[xRoot] = yRoot;
return;
}
if (Rank[xRoot] > Rank[yRoot]) {
Set[yRoot] = xRoot;
return;
}
Set[yRoot] = xRoot;
Rank[yRoot] = Rank[xRoot] + 1;
}
int Find(int x) {
if (Set[x] == x)
return x;
return (Set[x] = Find(Set[x]));
}
int main() {
freopen(FILEIN, "r", stdin);
freopen(FILEOUT, "w", stdout);
for (scanf("%d", &N); N; N--) {
Set[N] = N;
}
for (scanf("%d", &M); M; M--) {
int T, x, y;
scanf("%d %d %d", &T, &x, &y);
if (T == 1) {
Union(x, y);
} else {
if (Find(x) == Find(y)) {
printf("DA\n");
} else {
printf("NU\n");
}
}
}
return 0;
}