Pagini recente » Cod sursa (job #1908025) | Cod sursa (job #2980841) | Cod sursa (job #174010) | Cod sursa (job #1049204) | Cod sursa (job #2917155)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int maxN = 1e5 + 5;
int rang[maxN], t[maxN];
int radacina (int node) {
if(t[node] == 0)
return node;
int root = radacina(t[node]);
t[node] = root;
return root;
}
int main()
{
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i) {
int op; fin >> op;
int u, v; fin >> u >> v;
if(op == 1) {
int rad1 = radacina(u), rad2 = radacina(v);
if(rad1 != rad2) {
if(rang[rad1] > rang[rad2]) {
t[rad2] = rad1;
} else {
t[rad1] = rad2;
if(rang[rad1] == rang[rad2])
rang[rad1]++;
}
}
} else {
int rad1 = radacina(u), rad2 = radacina(v);
if(rad1 == rad2)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}