Cod sursa(job #2935597)

Utilizator AdelaCorbeanuAdela Corbeanu AdelaCorbeanu Data 7 noiembrie 2022 00:53:16
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#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;
}