Cod sursa(job #3252771)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 30 octombrie 2024 23:25:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include "bits/stdc++.h"
const int SIZE = 100000;
struct DSU{
    int root[SIZE + 5], size[SIZE + 5];
    DSU(int n){
        for(int i = 1; i <= n; i++){
            root[i] = i;
            size[i] = 1;
        }
    }
    int Find(int n){
        if(root[n] != n){
            root[n] = Find(root[n]);
        }
        return root[n];
    }
    void Unite(int a, int b){
        int rootA = Find(a);
        int rootB = Find(b);
        if(size[rootA] < size[rootB]){
            std :: swap(rootA, rootB);
        }
        root[rootB] = rootA;
        size[rootB] += size[rootA];
    }
};

void Solve(){
    int n, q;
    std :: cin >> n >> q;
    DSU dsu(n);
    while(q -- ){
        int op, x, y;
        std :: cin >> op >> x >> y;
        if(op == 1){
            dsu.Unite(x, y);
        }else{
            if(dsu.Find(x) == dsu.Find(y)){
                std :: cout << "DA" << '\n';
            }else{
                std :: cout << "NU" << '\n';
            }
        }
    }
}
signed main(){
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    std :: ios_base :: sync_with_stdio(false);
    std :: cin.tie(0);
    Solve();
    return 0;
}