Cod sursa(job #3158666)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 19 octombrie 2023 16:39:34
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, i, op, x, y;
int t[100002];

static inline int tata(int a) {
    if(t[a] == a) return a;
    else return t[a] = tata(t[a]);
}

static inline void uniune(int x, int y) {
    x = tata(x);
    y = tata(y);

    t[x] = y;
}

int main() {
    fin >> n >> m;
    for(i = 1; i <= n; i++) t[i] = i;
    while(m--) {
        fin >> op >> x >> y;
        if(op == 1) uniune(x, y);
        else {
            int tx = tata(x);
            int ty = tata(y);
            if(tx != ty) fout << "NU\n";
            else fout << "DA\n";
        }
    }

    return 0;
}