Cod sursa(job #3350422)

Utilizator eric_dragosDragos Eric eric_dragos Data 7 aprilie 2026 19:03:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define N 100005
int n,m;
int root[N];

int find(int x){
    if(root[x] != x) return find(root[x]);
    else return x;
}

void unire(int x, int y){
    root[find(y)] = find(x);
}


int main(){
    fin >> n >> m;
    for(int i = 1; i<=n; i++){
        root[i] = i;
    }
    while(m--){
        int c,x,y;
        fin >> c >> x >> y;
        if(c == 1) unire(x,y);
        else{
            if(find(x) == find(y)) fout << "DA\n";
            else fout << "NU\n";
        }
    }

    return 0;
}