Cod sursa(job #2576153)

Utilizator andrei42Oandrei42O andrei42O Data 6 martie 2020 17:37:13
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, T[N], getRoot(int);
int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; i++)
        T[i] = i;
    for(; m; m--)
    {
        int c, x, y;
        f >> c >> x >> y;
        int dx = getRoot(x);
        int dy = getRoot(y);
        if(c == 1)
            T[dy] = dx;
        else if(dx == dy)
            g << "DA\n";
        else
            g << "NU\n";
    }
    return 0;
}
int getRoot(int nod)
{
    if(T[nod] == nod)
        return nod;
    T[nod] = getRoot(T[nod]);
    return T[nod];
}