Cod sursa(job #2926812)

Utilizator SeracovanuEdwardSeracovanu Edward SeracovanuEdward Data 18 octombrie 2022 18:44:13
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

int const nmax = 1e5 + 5;

int n , q;
int T[nmax];

int rad(int nod){
if(T[nod] == nod)return nod;
return rad(T[nod]);
}

int main()
{
    freopen("disjoint.in" , "r" , stdin);
    freopen("disjoint.out" , "w" ,stdout);
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    cin >> n >> q;
    for(int i = 1;i <= n; ++i)T[i] = i;
    while(q--){
        int cod , x , y;
        cin >> cod >> x >> y;
        if(cod == 1)
            T[rad(x)] = rad(y);
        else cout << (rad(x) == rad(y) ? "DA" : "NU") << "\n";
    }
}