Cod sursa(job #3300329)

Utilizator vladm98Munteanu Vlad vladm98 Data 14 iunie 2025 20:05:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;

int tata[100005];

int getRoot(int node) {
    int root = node;
    while (root != tata[root]) {
        root = tata[root];
    }
    while (node != root) {
        int temp = tata[node];
        tata[node] = root;
        node = temp;
    }
    return root;
}

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

    tata[rootX] = rootY;
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        tata[i] = i;
    }
    for (int i = 1; i <= m; ++i) {
        int type, x, y;
        cin >> type >> x >> y;
        if (type == 1) {
            unite(x, y);
        } else {
            if (getRoot(x) == getRoot(y)) {
                cout << "DA\n";
            } else {
                cout << "NU\n";
            }
        }
    }
    return 0;
}