Pagini recente » Cod sursa (job #1946419) | Cod sursa (job #1113471) | Cod sursa (job #1716821) | Cod sursa (job #2209435) | Cod sursa (job #2032841)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int *father, *height;
int n, m;
void init() {
father = new int[n + 1];
height = new int[n + 1];
for (int i = 1; i <= n; i++) {
father[i] = i;
height[i] = 0;
}
}
void unite(int f1, int f2) {
if (height[f1] > height[f2])
father[f2] = f1;
else
father[f1] = f2;
if (height[f1] == height[f2])
height[f2]++;
}
int det_father(int x) {
int root = x, aux;
while (root != father[root])
root = father[root];
while (x != father[x]) {
aux = father[x];
father[x] = root;
x = aux;
}
return root;
}
int main() {
in >> n >> m;
init();
int op, x, y, f1, f2;
for (int i = 1; i <= m; i++) {
in >> op >> x >> y;
f1 = det_father(x);
f2 = det_father(y);
if (op == 1 && f1 != f2)
unite(f1, f2);
if (op == 2)
out << ((f1 == f2) ? "DA" : "NU") << "\n";
}
in.close();
out.close();
delete[] father;
delete[] height;
return 0;
}