Cod sursa(job #2304052)

Utilizator Bogdan_BuzatuBuzatu Bogdan Mihai Bogdan_Buzatu Data 17 decembrie 2018 14:21:57
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100100],x,y,n,p,op,rx,ry;
int rad(int xx){
    while(t[xx]>0){
        xx=t[xx];
    }
    return xx;
}
int main(){
    fin>>n>>p;
    for(int i=1;i<=n;i++){
        t[i]=-1;
    }
    while(p!=0){
        fin>>op>>x>>y;
        if(op==1){
            rx=rad(x);
            ry=rad(y);
            if(rx!=ry){
                if(t[rx]<t[ry]){
                    t[rx]+=t[ry];
                    t[ry]=rx;

                }
                else{
                    t[ry]+=t[rx];
                    t[rx]=ry;
                }
            }
        }
        if(op==2){
            rx=rad(x);
            ry=rad(y);
            if(rx==ry){
                fout<<"DA\n";
            }
            else{
                fout<<"NU\n";
            }
        }
        p--;
    }


}