Cod sursa(job #2947028)

Utilizator readyitzooDilirici Mihai readyitzoo Data 25 noiembrie 2022 16:34:31
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n, m;
int x, y, op;

vector < int > tata;

int getRoot(int &a) {
    if (tata[a] == a) {
        return a;
    }

    return getRoot(tata[a]);
}

int main() {
    f >> n >> m;
    tata.resize(n + 1);

    for (int i = 1; i <= n; i++)
        tata[i] = i;

    for (int i = 0; i < m; i++)
    {
        f >> op >> x >> y;
        if (op == 1) {
            tata[getRoot(y)] = getRoot(x);
        } else {
            if (getRoot(x) == getRoot(y)) {
                g << "DA" << '\n';
            } else {
                g << "NU" << '\n';
            }
        }
    }
}