Cod sursa(job #1301525)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 26 decembrie 2014 01:17:25
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

vector <int> v;

int x, y, i, a, b, op, n, m;

int find_root(int x){
    if(v[x]<0)  return x;
    else{
        v[x] = find_root(v[x]);
        return v[x];
    }
}

void quick_union(int x, int y){
    if(v[x] > v[y]){
        v[y] += v[x];
        v[x] = y;
    }
    else{
        v[x] += v[y];
        v[y] = x;
    }
}

int main(){
    fin >> n >> m;
    for(i=0; i<=n; i++) v.push_back(-1);

    for(i=1; i<=m; i++){
        fin >> op >> x >> y;
        a= find_root(y);
        b= find_root(x);
        if(op==1) quick_union(a, b);
        else{
            if(a==b) fout << "DA\n";
            else    fout << "NU\n";
        }

    }
    return 0;
}