Pagini recente » Cod sursa (job #458126) | Cod sursa (job #2429865) | Cod sursa (job #1910444) | Cod sursa (job #3210282) | Cod sursa (job #2789329)
#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[root2] = root1;
else {
t[root1] = root2;
if(rang[root1] == rang[root2])
rang[root2] ++;
}
}
}
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;
}