Cod sursa(job #1365685)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 28 februarie 2015 14:20:28
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <cassert>

using namespace std;

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

const int maxn = 100005;

int n, m, father[maxn];

inline int _find(int x) {
    if(father[x] != x)
        father[x] = _find(father[x]);
    return father[x];
}

inline void unite(int x, int y) {
    x = _find(x);
    y = _find(y);
    assert(x != y);
    father[x] = y;
}

int main() {
    fin >> n >> m;
    for(int i = 0 ; i < n ; ++ i)
        father[i] = i;
    for(int i = 1 ; i <= m ; ++ i) {
        int op, x, y;
        fin >> op >> x >> y;
        -- x; -- y;
        if(op == 1)
            unite(x, y);
        else
            if(_find(x) == _find(y))
                fout << "DA\n";
            else
                fout << "NU\n";
    }
}