Cod sursa(job #2837566)

Utilizator teofilotopeniTeofil teofilotopeni Data 22 ianuarie 2022 11:47:29
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <vector>
using namespace std;

vector<int> elements;

int root(int nod) {
    while (elements[nod]) {
        nod = elements[nod];
    }
    return nod;
}

int main() {
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int nr, operations;
    cin >> nr >> operations;
    elements = vector<int>(nr + 1, 0);
    for (; operations; --operations) {
        int cod, x, y;
        cin >> cod >> x >> y;
        if (cod == 1) {
            elements[root(x)] = y;
        }
        else {
            string result[] = { "NU\n", "DA\n" };
            cout << result[root(x) == root(y)];
        }
    }
    return 0;
}