Cod sursa(job #3225485)

Utilizator maiaauUngureanu Maia maiaau Data 17 aprilie 2024 18:12:36
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using ll = long long;
#define pb push_back

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

const int N = 1e5+5;

int n, m, comp[N];

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

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

    return 0;
}


//18:06