Cod sursa(job #2940792)

Utilizator kronos719Serban Andrei kronos719 Data 16 noiembrie 2022 14:58:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>


using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int N=100001;

int T[N],n,q;
int getRoot(int);
int main()
{
    f>>n>>q;
    for(int i=1;i<=n;i++)
        T[i]=i;
    for(;q;q--){
        int tip,x,y;
        f>>tip>>x>>y;
        int rx=getRoot(x);
        int ry=getRoot(y);
        if(tip==1){
            T[rx]=ry;
        }
        else {
            if(rx==ry)
                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];

}