Pagini recente » Cod sursa (job #244535) | Cod sursa (job #1755322) | Cod sursa (job #1736544) | Cod sursa (job #1580058) | Cod sursa (job #2935596)
#include <fstream>
#include <vector>
int main()
{
std::ifstream fin("disjoint.in");
std::ofstream fout("disjoint.out");
int n, m;
fin >> n >> m;
std::vector<int> ancestor(n + 1);
for (int i = 1; i <= n; ++i) ancestor[i] = i;
for (int i = 0; i < m; ++i) {
int op, x, y;
fin >> op >> x >> y;
while (ancestor[x] != x || ancestor[y] != y) {
if (ancestor[x] != x) x = ancestor[x];
if (ancestor[y] != y) y = ancestor[y];
}
if (op == 1) ancestor[x] = y;
else fout << (x == y ? "DA" : "NU") << '\n';
}
return 0;
}