Cod sursa(job #3215672)

Utilizator maiaauUngureanu Maia maiaau Data 15 martie 2024 11:31:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;

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

const int N = 1e5+3;

int comp[N], getroot(int);

int main()
{
    int n, m; fin >> n >> m;
    for (int i = 1; i <= n; i++) comp[i] = i;
    while (m--){
        int task, a, b; fin >> task >> a >> b;
        a = getroot(a); b = getroot(b);
        if (task == 1) comp[a] = b;
        else fout << (a == b ? "DA\n" : "NU\n");
    }

    return 0;
}

int getroot(int nod){
    if (comp[nod] == nod) return nod;
    return (comp[nod] = getroot(comp[nod]));
}


//11:28