Cod sursa(job #2755922)

Utilizator ViAlexVisan Alexandru ViAlex Data 28 mai 2021 19:46:06
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<iostream>
#include<fstream>
using namespace std;

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

const int mx=1e5+100;

int n,m,father[mx],depth[mx];

int root(int node){
    while(father[node]){
        node=father[node];
    }
    return node;
}

void merge(int a,int b){
    if(depth[a]==depth[b]){
        father[a]=b;
        depth[b]++;
    }
    else if(depth[a]<depth[b]){
        father[a]=b;
    }
    else{
        father[b]=a;
    }
}

void read(){
    in>>n>>m;
}

void solve(){
    int x,a,b;
    for(int i=0;i<m;i++){
        in>>x>>a>>b;

        if(x==1){
            merge(root(a),root(b));
        }
        else{
            root(a)==root(b)?out<<"DA\n":out<<"NU\n";
        }
    }
}

int main(){
    read();
    solve();
    return 0;
}