Cod sursa(job #2489042)

Utilizator gheorghe_cristiGheorghe Florin Cristi gheorghe_cristi Data 7 noiembrie 2019 21:26:29
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n,m,s,x,y,rx,ry;
int t[100001];

int radacina(int x) {
    while(t[x]>0)
        x = t[x];
    return x;
}

int main()
{
    fin>>n>>m;
    for (int i=1;i<=n;++i)
        t[i] = -1;
    while(m--) {
        fin>>s>>x>>y;
        rx=radacina(x);
        ry=radacina(y);
        if(s==1) {
            if(t[rx]<t[ry]) {
                t[rx] += t[ry];
                t[ry] = rx;
            } else {
                t[ry] += t[rx];
                t[rx] = ry;
            }
        }else
            if(rx==ry)
                fout<<"DA"<<"\n";
            else
                fout<<"NU"<<"\n";
    }
}