Cod sursa(job #2627921)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 13 iunie 2020 13:44:32
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

int N, M, cod, x, y, t[100000];

int Find(int i) {
    if (t[i] == i)
        return i;
    return t[i] = Find(t[i]);
}

void Union(int i, int j) {
    int ti, tj;
    ti = Find(i);
    tj = Find(j);
    t[tj] = ti;
}

int main() {
    fin >> N >> M;
    for (int i = 0; i < N; i++) t[i] = i;
    while (M--) {
        fin >> cod >> x >> y;
        x--, y--;
        if (cod == 1) Union(x, y);
        else {
            int tx = Find(x),
                ty = Find(y);
            fout << ((tx == ty) ? "DA\n" : "NU\n");
        }
    }
    fin.close();
    fout.close();
    return 0;
}