Pagini recente » Cod sursa (job #2096783) | Cod sursa (job #933912) | Cod sursa (job #2626646) | Cod sursa (job #2076923) | Cod sursa (job #2935597)
#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)
if (ancestor[x] != x) x = ancestor[x];
while (ancestor[y] != y)
if (ancestor[y] != y) y = ancestor[y];
if (op == 1) ancestor[x] = y;
else fout << (x == y ? "DA" : "NU") << '\n';
}
return 0;
}