Cod sursa(job #2845347)

Utilizator OvidRata Ovidiu Ovid Data 7 februarie 2022 18:51:40
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("disjoint.in"); ofstream fout("disjoint.out");
#define cin fin
#define cout fout


int n, m;


int rep[100010];
int sz[100010];

void initialize(){

for(int i=1; i<=n; i++){
    rep[i]=i;
    sz[i]=1;
}

}


int get_rep(int u){

if(u!=rep[u]){
    rep[u]=get_rep(rep[u]);
}
else{
    return u;
}

}



void unite(int u, int v){

if(sz[u]>sz[v]){
    sz[u]+=sz[v];
    rep[v]=u;
}
else{
    sz[v]+=sz[u];
    rep[u]=v;
}

}

int32_t main(){
INIT
cin>>n>>m;
initialize();




for(int i=1; i<=m; i++){
    int tp;
    cin>>tp;
    if(tp==1){
        int x, y;
        cin>>x>>y;
        unite(get_rep(x), get_rep(y));
    }
    if(tp==2){
        int x, y;
        cin>>x>>y;
        if(get_rep(x)==get_rep(y)){
            cout<<"DA\n";
        }
        else{
            cout<<"NU\n";
        }
    }
}


return 0;
}