Cod sursa(job #1684217)

Utilizator razvandRazvan Dumitru razvand Data 10 aprilie 2016 21:34:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#define MAX 100003

using namespace std;

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

int T[MAX];
int H[MAX];

int father(int x) {
    while(T[x] != x)
        x = T[x];
    return x;
}

void un(int x, int y) {

    int fath1 = father(x);
    int fath2 = father(y);
    T[fath2] = fath1;

}

int main() {

    int n,m,t,x,y;
    in >> n >> m;

    for(int i = 1; i <= n; i++)
        T[i] = i;

    for(int i = 1; i <= m; i++) {

        in >> t >> x >> y;

        if(t == 1) {

            un(x, y);

        } else {

            if(father(x) == father(y)) {
                out << "DA" << '\n';
            } else {
                out << "NU" << '\n';
            }

        }

    }

    return 0;
}