Cod sursa(job #1926946)

Utilizator AkrielAkriel Akriel Data 14 martie 2017 20:16:21
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

#define N 100010

using namespace std;

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

int root[N];

int totalNodes, totalRelations;

int getRoot(int node){
    if ( root[node] != node ){
        root[node] = getRoot(root[node]);
    }
    return root[node];
}

inline void connectThings(int first, int second){
    first = getRoot(first);
    second = getRoot(second);
    root[first] = root[second];
}

inline void doesItExist(int first, int second){
    first = getRoot(first);
    second = getRoot(second);
    if ( first == second )
        fout << "DA" << "\n";
    else
        fout << "NU" << "\n";
}

int main(){
    fin >> totalNodes >> totalRelations;

    for ( int index = 1; index <= totalNodes; index++ )
        root[index] = index;

    int first, second, question;
    for ( int index = 1; index <= totalRelations; index++ ){
        fin >> question >> first >> second;
        switch (question){
            case 1:
                connectThings(first, second);
                break;
            case 2:
                doesItExist(first, second);
                break;
        }
    }
}