Cod sursa(job #2279552)

Utilizator Teodor01Dorde Teodor Teodor01 Data 9 noiembrie 2018 18:06:20
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int T[100001];
int sef(int x){
    if(x!=T[x]){
        return sef(T[x]);
    }
    else
        return x;
}
struct operatii{
    int op;
    int n1;
    int n2;
}o[100001];
int main()
{
    int m,n,i;
    cin>>m>>n;
    for(i=1;i<=m;i++)
        T[i]=i;
    for(i=1;i<=n;i++)
        cin>>o[i].op>>o[i].n1>>o[i].n2;
    for(i=1;i<=n;i++){
        if(o[i].op==1){
            T[sef(o[i].n2)]=sef(o[i].n1);
        }
        if(o[i].op==2){
            if(sef(T[o[i].n1])==sef(T[o[i].n2])){
                cout<<"DA\n";
            }
            else{
                cout<<"NU\n";
            }
        }
    }
    return 0;
}