Pagini recente » Cod sursa (job #2516225) | Cod sursa (job #3278275) | Cod sursa (job #3003021) | Cod sursa (job #886299) | Cod sursa (job #3237023)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
const int NMAX = 100000;
int N, M, T[NMAX+1], H[NMAX+1];
int Find(int x) {
if(T[x] == 0) return x;
return T[x] = Find(T[x]);
}
void Union(int x, int y) {
if(H[x] < H[y])
T[x] = y;
else {
T[y] = x;
if (H[x] == H[y])
H[x]++;
}
}
int main()
{
int op, x, y;
f >> N >> M;
while(M--) {
f >> op >> x >> y;
x = Find(x);
y = Find(y);
if (op == 1) {
if (x != y)
Union(x, y);
} else
if (x == y)
g << "DA\n";
else
g << "NU\n";
}
f.close();
g.close();
return 0;
}