Cod sursa(job #1563630)

Utilizator AetheryonStefan Bereghici Aetheryon Data 6 ianuarie 2016 13:26:58
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;
const char* IN = "disjoint.in";
const char* OUT = "disjoint.out";
const int MAX_SIZE = 100013;

class DisjointSets {
    private :
        int number;
        int root[MAX_SIZE];
    public :
        DisjointSets(int nr){
            number = nr;
            for (int i=1;i<=nr;++i)
                root[i] = i;
        }
        ~DisjointSets(){};

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

        void mergeRoots(int node1,int node2){
            root[getRoot(node1)] = root[getRoot(node2)];
        }
};

int n,q,op,a,b;
int main(void){
    ifstream cin(IN);
    ofstream cout(OUT);
    cin>>n>>q;
    DisjointSets* Set = new DisjointSets(n);
    while(q--){
        cin>>op>>a>>b;
        if (op==1) Set->mergeRoots(a,b);
         else {
            if (Set->getRoot(a)==Set->getRoot(b)) cout<<"DA\n";
                else cout<<"NU\n";
         }
    }
    return 0;
}