Cod sursa(job #1071253)

Utilizator IeewIordache Bogdan Ieew Data 2 ianuarie 2014 20:22:42
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>

using namespace std;

#define DEBUG false

#define MAXN 100001

#if DEBUG
#include <iostream>
#define INFILE "/Users/biordach/Documents/Xcode Projects/training/training/disjoint.in"
#define __OUT cout
#else
#define INFILE "disjoint.in"
#define OUTFILE "disjoint.out"
ofstream __OUT(OUTFILE);
#endif

void readInput();
void solve();
void printOutput();

int n, m;
int height[MAXN], father[MAXN];


int main(int argc, const char * argv[])
{
    readInput();
//  solve();
//  printOutput();

#if DEBUG == false
    __OUT.close();
#endif
    
    return 0;
}

int getFather(int x){
    if(father[x] == x){
        return x;
    }
    int f = getFather(father[x]);
    father[x] = f;
    return f;
}

void readInput(){
    ifstream in(INFILE);
    in>>n>>m;
    int op, a, b;
    for(int i=1;i<=n;i++){
        height[i] = 1;
        father[i] = i;
    }
    
    for(int i = 0;i<m;i++){
        in>>op>>a>>b;
        int ta = getFather(a), tb = getFather(b);

        if(op == 1){
            if (height[ta] > height[tb]){
                father[tb] = ta;
                height[ta] += height[tb];
            } else {
                father[ta] = tb;
                height[tb] += height[ta];
            }
        } else {
            if(ta == tb){
                __OUT<<"DA\n";
            } else __OUT<<"NU\n";
        }
    }
    
    in.close();
}

void solve(){
}

void printOutput(){
}