Pagini recente » Cod sursa (job #298995) | Cod sursa (job #1081234) | Cod sursa (job #2616859) | Cod sursa (job #3177313) | Cod sursa (job #2117719)
#include <iostream>
#include <fstream>
#define nmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[nmax], h[nmax];
void unire (int x, int y) {
if (h[x] > h[y]) tata[y] = x;
else tata[x] = y;
if (x[h] == h[y]) h[y]++;
}
int gasire (int x) {
int r = x;
while (tata[r]) r = tata[r];
int y = x, t;
while (y != r) {
t = tata[y];
tata[y] = r;
y = t;
}
return r;
}
int main()
{
int n, m, x, y;
f >> n >> m;
int q;
for (int i = 1; i <= m; ++i) {
f >> q >> x >> y;
if (q == 1) unire (gasire(x), gasire(y));
else {
if (gasire(x) != gasire(y)) g << "NU\n";
else g << "DA\n";
}
}
return 0;
}