Cod sursa(job #3309440)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 4 septembrie 2025 17:09:49
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 100000;

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

int N, M;
int T[N_MAX + 5];

int find(int node) {
    if(node == T[node]) {
        return node;
    }
    return find(T[node]);
}

void unite(int a, int b) {
    a = find(a);
    b = find(b);
    T[a] = b;
}

int main()
{
    fin >> N >> M;
    
    for(int i = 1; i <= N; i++) {
        T[i] = i;
    }
    
    while(M--) {
        int t, a, b;
        fin >> t >> a >> b;
        if(t == 1) {
            unite(a, b);
        }
        if(t == 2) {
            if(find(a) == find(b)) {
                fout << "DA\n";
            }
            else {
                fout << "NU\n";
            }
        }
    }
    return 0;
}