Pagini recente » Cod sursa (job #628735) | Cod sursa (job #3195894) | Cod sursa (job #2107724) | Cod sursa (job #1611970) | Cod sursa (job #770632)
Cod sursa(job #770632)
#include <stdio.h>
#define NMAX 100001
#define NIL 0
using namespace std;
int Parent[NMAX];
int n;
void join(int x, int y, int Hx, int Hy) {
if (Hx > Hy) {
Parent[y] = x;
} else {
Parent[x] = y;
}
}
bool same_set(int x, int y) {
return (x == y);
}
int go_top(int &x) {
int Hx = 0;
while (Parent[x] != NIL) {
x = Parent[x];
Hx++;
}
return Hx;
}
void print_(bool SS) {
if (SS == true) {
printf("DA\n");
} else {
printf("NU\n");
}
}
void solve_(int m) {
int cod, x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &cod, &x, &y);
int Hx = go_top(x);
int Hy = go_top(y);
if (cod == 1) {
join(x, y, Hx, Hy);
} else {
bool SS = same_set(x, y);
print_(SS);
}
}
}
int main() {
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int m;
scanf("%d%d", &n, &m);
solve_(m);
return 0;
}