Cod sursa(job #2614194)

Utilizator eugen5092eugen barbulescu eugen5092 Data 11 mai 2020 13:45:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

ifstream ci("disjoint.in");
ofstream cou("disjoint.out");

int t[100005],n,m;

int Find(int x){
    int r=x,r1;
    while(t[r]){
        r=t[r];
    }
    while(t[x]){
        r1=t[x];
        t[x]=r;
        x=r1;
    }
    return r;
}


void Union(int x,int y){
    t[y]=x;
}

void rez(){
    ci>>n>>m;
    int i,c,x,y;
    for(i=1;i<=m;i++){
        ci>>c>>x>>y;
        x=Find(x);
        y=Find(y);
        if(c==1){
            Union(x,y);
        }else{
            if(x==y){
                cou<<"DA \n";
            }else{
                cou<<"NU \n";
            }
        }
    }
}


int main()
{
    rez();
    return 0;
}