Pagini recente » Cod sursa (job #322144) | Cod sursa (job #2119643) | Cod sursa (job #905028) | Cod sursa (job #1980036) | Cod sursa (job #3218464)
#include <cstdio>
using namespace std;
FILE *input, *output;
#define NMAX 100002
int T[NMAX], H[NMAX];
void Union(int x, int y) {
if (H[x] > H[y]) T[y] = x;
else if (H[x] < H[y]) T[x] = y;
else {
T[y] = x;
++H[x];
}
}
int Find(int x) {
if (T[x] == 0) return x;
T[x] = Find(T[x]);
return T[x];
}
int n, m;
int main(void) { int i;
input = fopen("disjoint.in", "r"), output = fopen("disjoint.out", "w");
(void)fscanf(input, "%d %d", &n, &m);
{
int c, x, y, rx, ry;
for (i = 1; i <= m; ++i) {
(void)fscanf(input, "%d %d %d", &c, &x, &y);
rx = Find(x);
ry = Find(y);
if (c == 1) {
Union(rx, ry);
} else {
(void)fprintf(output, "%s\n", (rx == ry) ? "DA" : "NU");
}
}
}
fclose(output);
return 0;
}