Cod sursa(job #1736590)

Utilizator ataegasMr. HHHHHH ataegas Data 2 august 2016 04:50:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void reunion (int node_1, int node_2, vector <int> &ind_list){
    if (ind_list[node_1] < ind_list[node_2]){
        ind_list[node_1] += ind_list[node_2];
        ind_list[node_2] = node_1;
    }
    else{
        ind_list[node_2] += ind_list[node_1];
        ind_list[node_1] = node_2;
    }
}

int find_root (int node, vector <int> &ind_list){
    if (ind_list[node] < 0)
        return node;
    return find_root (ind_list[node], ind_list);
}

void dsu (){
    ifstream fin ("disjoint.in");
    ofstream fout ("disjoint.out");
    vector <int> ind_list;
    int n_nodes, n_op;
    fin >> n_nodes >> n_op;
    for (int i = 1; i <= n_nodes + 1; i++)
        ind_list.push_back (-1);
    for (int i = 1; i <= n_op; i++){
        int op, node_1, node_2;
        fin >> op >> node_1 >> node_2;
        int father_node_1 = find_root (node_1, ind_list);
        int father_node_2 = find_root (node_2, ind_list);
        if (op == 1)
            reunion (father_node_1, father_node_2, ind_list);
        else if (father_node_1 == father_node_2)
            fout << "DA\n";
        else
            fout << "NU\n";
    }
}


int main(){
    dsu ();
    return 0;
}