Pagini recente » Cod sursa (job #1995667) | Cod sursa (job #2950408) | Cod sursa (job #2839627) | Cod sursa (job #1237761) | Cod sursa (job #2049621)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int tata[MAXN + 1];
int sef (int x) {
if (tata[x] == x)
return x;
else
return tata[x] = sef(tata[x]);
}
void unire (int x, int y) {
int tx, ty;
tx = sef (x);
ty = sef (y);
tata[tx] = ty;
}
int main()
{
FILE *fin, *fout;
int n, m, i, c, x, y, tx, ty;
fin = fopen ("disjoint.in", "r");
fout = fopen ("disjoint.out", "w");
fscanf (fin, "%d%d", &n, &m);
for (i = 1; i <= n; i++)
tata[i] = i;
for (i = 1; i <= m; i++) {
fscanf (fin, "%d%d%d", &c, &x, &y);
if (c == 1) {
unire (x, y);
}
else {
if (sef (x) == sef (y))
fprintf (fout, "DA\n");
else
fprintf (fout, "NU\n");
}
}
fclose (fin);
fclose (fout);
return 0;
}