Cod sursa(job #2947268)

Utilizator andreihodoAndrei Hodoroaga andreihodo Data 25 noiembrie 2022 21:25:49
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n, m, x, y, cod;
vector<int> par;

int find(int x) {
    if(x != par[x]) {
        par[x] = find(par[x]);
        return par[x];
    } else
        return x;
}

// int f_union(int x, int y) {
//     x = find(x);
//     y = find(y);
// }

int main() {
    f >> n >> m;
    par.resize(n + 1);
    for(int i = 1; i <= m; i++) {
        f >> cod >> x >> y;
        if(cod == 1) {
            par[find(x)] = find(y);
        } else {
            if(find(x) == find(y))
                g << "DA" << endl;
            else 
                g << "NU" << endl;
        }
    }

}