Pagini recente » Cod sursa (job #231645) | Cod sursa (job #2012032) | Cod sursa (job #2089424) | Cod sursa (job #2935899) | Cod sursa (job #3356053)
#include <stdio.h>
#include <stdbool.h>
#define N 100000
int t[N+1], h[N+1];
int radacina(int x) {
if (t[x] == 0) {
return x;
}
return radacina(t[x]);
}
void reuniune(int x, int y) {
int rx = radacina(x);
int ry = radacina(y);
if (h[x] < h[y]) {
t[rx] = ry;
} else {
t[ry] = rx;
if (h[rx] == h[y]) {
h[rx]++;
}
}
}
bool verif(int x, int y) {
return (radacina(x) == radacina(y));
}
int main(void) {
FILE *in = fopen("disjoint.in", "r");
FILE *out = fopen("disjoint.out", "w");
int n, q;
fscanf(in, "%d %d", &n, &q);
for (int i = 0; i < q; i++) {
int tip, x, y;
fscanf(in, "%d%d%d", &tip, &x, &y);
if (tip == 1) {
reuniune(x, y);
} else {
if (verif(x, y)) {
fprintf(out, "DA\n");
} else {
fprintf(out, "NU\n");
}
}
}
fclose(in);
fclose(out);
return 0;
}