Cod sursa(job #3273516)

Utilizator vladm98Munteanu Vlad vladm98 Data 2 februarie 2025 15:30:33
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

int tata[100005];

void reset(int n) {
    for (int i = 1; i <= n; i++) {
        tata[i] = i;
    }
}

int getRoot(int x) {
    while (x != tata[x]) {
        x = tata[x];
    }
    return x;
}

void unite(int x, int y) {
    x = getRoot(x);
    y = getRoot(y);

    tata[y] = x;
}

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

    int n, m;
    cin >> n >> m;
    reset(n);

    for (int i = 1; i <= m; i++) {
        int op, x, y;
        cin >> op >> x >> y;

        if (op == 1) {
            if (getRoot(x) == getRoot(y)) {
                cout << "DA" << '\n';
            } else {
                cout << "NU" << '\n';
            }
        } else {
            unite(x, y);
        }
    }
    return 0;
}