Cod sursa(job #2989267)

Utilizator GoofyAhBalea Gabriel GoofyAh Data 6 martie 2023 12:01:25
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

int dad[100001], n , m;

void read() {
    fin >> n;
    for (int i=1;i<=n;i++) {
        dad[i]=i;
    }
}

int do_find(int n){
    while (n!=dad[n]) {
        n=dad[n];
    }
    return n;
}

void do_union(int n1, int n2) {
    dad[n1]=dad[n2];
}

int main(){
    read();
    int x,y,z;
    fin >> m;
    for (int i=1;i<=m;i++){
        fin  >> z >> x >> y;
        if (z==1) do_union(x,y);
        if (z==2){
            if (do_find(x)==do_find(y)) cout << "DA\n";
            else cout << "NU\n";
        }
    }
}