Pagini recente » Cod sursa (job #944343) | Cod sursa (job #2478055) | Cod sursa (job #749255) | Cod sursa (job #1688524) | Cod sursa (job #2740661)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t, n, m, x, y, op, rang[100010], tt[100010];
int find_root(int x) {
int node = x;
while (tt[node] != node)
node = tt[node];
while (tt[x] != x) {
int aux = tt[x];
tt[x] = node;
x = aux;
}
return node;
}
void unite(int x, int y) {
if (rang[x] > rang[y])
tt[y] = x;
else
tt[x] = y;
if (rang[x] == rang[y])
++rang[y];
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
rang[i] = 1;
tt[i] = i;
}
for (int i = 1; i <= m; ++i) {
fin >> op >> x >> y;
if (op == 1)
unite(find_root(x), find_root(y));
else {
if (find_root(x) == find_root(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
}