Cod sursa(job #1878776)

Utilizator savigunFeleaga Dragos-George savigun Data 14 februarie 2017 14:19:18
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
using namespace std;

int father[100001], root[100001];

int get_root(int x) {
    if (root[x] == x)
        return x;
    return get_root(father[x]);
}

void join(int x, int y) {
    int rootX = get_root(x);
    int rootY = get_root(y);

    root[y] = rootX;
    father[rootY] = rootX;
}


int main() {
    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");

    int n, m, x, y, o;
    cin >> n >> m;

    for (int i = 1; i <= n; ++i) {
        root[i] = i;
        father[i] = i;
    }

    for (int i = 1; i <= m; ++i) {
        cin >> o >> x >> y;
        if (o == 1) {
            join(x, y);
        } else {
            if (get_root(x) == get_root(y)) {
                cout << "DA" << '\n';
            } else {
                cout << "NU" << '\n';
            }
        }
    }


    return 0;
}