Cod sursa(job #1484365)

Utilizator ataegasMr. HHHHHH ataegas Data 10 septembrie 2015 22:27:17
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#define nmax 100005
using namespace std;

int v[nmax];

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

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

void get_data(int &n){
    ifstream fin ("disjoint.in");
    ofstream fout("disjoint.out");
    int m, x, y, op;
    fin >> n >> m;
    for(int i= 1; i<=n; i++)    v[i]= -1;
    for(int i= 1; i<= m; i++){
        fin >> op >> x >> y;
        int father_x= find_father(x);
        int father_y= find_father(y);
        if(op== 1) reunion(father_x, father_y);
        if(op== 2 && father_x== father_y)   fout << "DA\n";
        else if(op== 2 && father_x != father_y) fout << "NU\n";

    }
}


int main(){
    int n;
    get_data(n);

    return 0;
}