Cod sursa(job #3344168)

Utilizator IleaIlea Bogdan Ilea Data 1 martie 2026 15:36:44
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<iostream>
using namespace std;

#define NMAX 100001
#define da "DA\n"
#define nu "NU\n"

int n,m,root[NMAX];
int get_root(int x){
    if(x==root[x])return x;
    return (root[x]=get_root(root[x]));
}
void join(int x,int y){
    x=get_root(x);
    y=get_root(y);
    root[x]=y;
}
bool joined(int x,int y){
    return get_root(x)==get_root(y);
}
signed main(){
#ifndef LOCAL
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#endif // LOCAL
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;++i)root[i]=i;
    for(;m;--m){
        int op,x,y;
        cin>>op>>x>>y;
        if(op-1){
            cout<<(joined(x,y)?da:nu);
        }else{
            join(x,y);
        }
    }
    return 0;
}