Pagini recente » Cod sursa (job #595977) | Cod sursa (job #726688) | Cod sursa (job #1923409) | Cod sursa (job #2092869) | Cod sursa (job #3319129)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector<int> tata;
vector<int> h;
int Find(int x) {
if (tata[x] == -1)
return x;
tata[x] = Find(tata[x]);
return Find(tata[x]);
}
void Union (int x, int y) {
int tx = Find(x);
int ty = Find(y);
if (tx == ty)
return;
if (h[tx] == h[ty]) {
tata[tx] = ty;
h[tx]++;
}
else if (h[tx] > h[ty]) {
tata[ty] = tx;
}
else {
tata[tx] = ty;
}
}
int main() {
int n, op;
in >> n >> op;
tata = vector<int> (n, -1);
h = vector<int> (n, 0);
for (int i = 0; i < op; i++) {
int type, x, y;
in >> type >> x >> y;
switch (type) {
case 1: {
Union(x, y);
break;
}
case 2 : {
if (Find(x) == Find(y))
out << "DA\n";
else
out << "NU\n";
break;
}
}
}
return 0;
}