Cod sursa(job #3314697)

Utilizator SkibidiCezarCezar Bolba SkibidiCezar Data 10 octombrie 2025 18:50:52
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m, x, y, q;
int p[100005];
void fnd(int nod){
    if(p[nod] != nod){
        fnd(p[nod]);
        p[nod] = p[p[nod]];
    }
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= n; i++){
        p[i] = i;
    }
    for(int i = 1; i <= m; i++){
        fin >> q >> x >> y;
        fnd(x);
        fnd(y);
        if(q == 1){
            p[p[y]] = p[x];
        }
        else if(p[x] == p[y]){
            fout << "DA \n";
        }
        else{
            fout << "NU \n";
        }
    }
    return 0;
}