Cod sursa(job #3326815)

Utilizator radu._.21Radu Pelea radu._.21 Data 30 noiembrie 2025 17:29:53
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define ll unsigned long long

using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
int tata[100001];
int root(int nod){
    while(tata[nod]>0)
        nod = tata[nod];
    return nod;
}
void unite(int x, int y){
    if(-tata[x] < -tata[y])
        swap(x,y); // y trb sa fie arborele mai mic
    tata[x]+=tata[y]; // dimensiunea arborelui
    tata[y] = x; // schimb tatal radacinii lui y
}
int main(){
    fin>>n>>m;
    for(int i = 1; i <=n; i++){
        tata[i] = -1;
    }
    while(m--){
        int op ,x ,y;
        fin>>op>>x>>y;
        if(op == 1){
            // leg x de y
            int rootx = root(x);
            int rooty = root(y);
            if(rootx != rooty){
                unite(rootx,rooty);
            }
        } else {
            int rootx = root(x);
            int rooty = root(y);
            if(rootx == rooty)
                fout<<"DA"<<'\n';
            else
                fout<<"NU"<<'\n';
        }
    }
   return 0;
}