Cod sursa(job #3361112)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 20 iulie 2026 15:30:09
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb

#include <bits/stdc++.h>
using namespace std;

const int NMAX = 1e5;
int p[NMAX + 1], sz[NMAX + 1];

int find(int x) {
    int r = x;
    while(r != p[r]) {
        r = p[r];
    }
    int y = x;
    while(y != r) {
        int t = p[y];
        p[y] = r;
        y = t;
    }
    return r;
}

void join(int x, int y) {
    int px = find(x);
    int py = find(y);
    if(sz[px] > sz[py]) {
        swap(px, py);
    }
    p[px] = py;
    sz[py] += sz[px];
    sz[px] = 0;
}

int query(int x, int y) {
    return find(x) == find(y);
}

int main() {
    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");
	int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        p[i] = i;
        sz[i] = 1;
    }
    for(int i = 1; i <= m; i++) {
        int op, x, y;
        cin >> op >> x >> y;
        if(op == 1) {
            join(x, y);
        } else {
            if(query(x, y) == 1) {
                cout << "DA\n";
            } else {
                cout << "NU\n";
            }
        }
    }
}