Pagini recente » Cod sursa (job #1895438) | Cod sursa (job #2089696) | Cod sursa (job #2333181) | Cod sursa (job #150714) | Cod sursa (job #2789326)
#include <iostream>
#include <fstream>
using namespace std;
const int maxN = 1e5 + 5;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int t[maxN], rang[maxN];
int radacina (int node) {
if(t[node] == 0)
return node;
int root = radacina(t[node]);
t[node] = root;
return root;
}
void unire (int x, int y) {
int root1 = radacina(x);
int root2 = radacina(y);
if(root1 != root2) {
if(rang[root1] > rang[root2])
t[y] = x;
else {
t[x] = y;
if(rang[x] == rang[y])
rang[x] ++;
}
}
}
int main() {
int n, m; fin >> n >> m;
for(int i = 1; i <= m; ++i) {
int op, x, y;
fin >> op >> x >> y;
if(op == 1) {
unire(x, y);
} else {
int root1 = radacina(x);
int root2 = radacina(y);
if(root1 == root2) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}