Cod sursa(job #2863683)

Utilizator DordeDorde Matei Dorde Data 7 martie 2022 08:40:57
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int const N = 1e5 + 3;
int n , m;
int p[N] , h[N] , t[N];
int T(int x){
    if(t[x] == x)
        return x;
    return t[x] = T(t[x]);
}
void union_set(int a , int b){
    a = T(a);
    b = T(b);
    if(h[a] < h[b])
        swap(a , b);
    t[b] = a;
    if(h[b] == h[a])
        ++ h[a];
}
int main()
{
    fin >> n >> m;
    for(int i = 1 ; i <= n ; ++ i){
        t[i] = i;
        h[i] = 1;
        p[i] = i;
    }
    for(int i = 1 ; i <= m ; ++ i){
        int type , a , b;
        fin >> type >> a >> b;
        if(type == 1)
            union_set(a , b);
        else
            fout << (T(a) == T(b) ? "DA\n" : "NU\n");
    }
    return 0;
}