Cod sursa(job #2642988)

Utilizator mex7Alexandru Valentin mex7 Data 18 august 2020 08:45:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector <int> parents[100001];

int getRoot(int current) {
    for (int node : parents[current])
        return getRoot(node);

    if (parents[current].empty())
        return current;
}

int main() {
    int n, m, t, x, y;

    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        fin >> t >> x >> y;

        if (t == 1)
            parents[getRoot(y)].push_back(getRoot(x));
        else
            if (getRoot(x) == getRoot(y))
                fout << "DA\n";
            else
                fout << "NU\n";
    }

    return 0;
}