Cod sursa(job #3217488)

Utilizator sireanu_vladSireanu Vlad sireanu_vlad Data 23 martie 2024 11:05:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
using namespace std;

#define NMAX 100000

int n, m;
int r[NMAX + 1];
int t[NMAX + 1];

int rad(int x) {
    while (t[x] != 0) {
        x = t[x];
    }
    return x;
}

void join(int x, int y) {
    t[y] = x;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);

    cin >> n >> m;

    for (int i = 1; i <= n; i += 1) {
        r[i] = i;
        t[i] = 0;
    }

    for (int i = 0; i < m; i += 1) {
        int cod, x, y;
        cin >> cod >> x >> y;
        if (cod == 1) {
            join(rad(x), rad(y));
        } else {
            if (rad(x) == rad(y)) {
                cout << "DA\n";
            } else {
                cout << "NU\n";
            }
        }
    }

    return 0;
}