Cod sursa(job #2863685)

Utilizator DordeDorde Matei Dorde Data 7 martie 2022 08:41:59
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 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 sz[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(sz[a] < sz[b])
        swap(a , b);
    t[b] = a;
    sz[a] += sz[b];
}
int main()
{
    fin >> n >> m;
    for(int i = 1 ; i <= n ; ++ i){
        t[i] = i;
        sz[i] = 1;
    }
    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;
}