Cod sursa(job #3372937)

Utilizator VladPislaruPislaru Vlad Rares VladPislaru Data 5 noiembrie 2026 17:08:48
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;


ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

int n, m;
int t[100005];

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

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



int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int task, x, y;
        fin >> task >> x >> y;
        if (task == 1) {
            int tata_x = Find(x);
            int tata_y = Find(y);
            if (tata_x!=tata_y) {
                Union(tata_x, tata_y);
            }
        }
        else {
            int ok = (Find(x) == Find(y));
            if (ok)
                fout << "DA\n";
            else fout << "NU\n";
        }
    }
    return 0;
}