Cod sursa(job #719897)

Utilizator vendettaSalajan Razvan vendetta Data 22 martie 2012 10:21:03
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#define nmax 100005

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n, m, t[nmax];

void citeste(){

    f >> n >> m;

}

int radacina(int x){
    int i;
    for(i=x; t[i]; i=t[i]);
    int j = x;
    while(j!=i){
        int aux = j;
        t[j] = i;
        j = t[aux];

    }
    return i;
}

void uneste(int x, int y){

    x = radacina(x);
    y = radacina(y);

    t[x] = y;

}

int multime(int x, int y){

    x = radacina(x);
    y = radacina(y);

    if (x == y) return 1;
    return 0;

}


void rezolva(){

    for(int i=1; i<=m; i++){
        int tip, x, y;
        f >> tip >> x >> y;
        if (tip==1){
            uneste(x,y);
        }
        else if(tip == 2){
            if (multime(x,y) > 0) g << "DA" << "\n";
            else g << "NU" <<"\n";
        }
    }

}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}