Cod sursa(job #1105420)

Utilizator FayedStratulat Alexandru Fayed Data 11 februarie 2014 19:59:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <cstdio>
#define NMAX 100001

int depth[NMAX];
int father[NMAX];
int n,m;

int root(int node){

    while(father[node])
        node = father[node];

return node;
}

void compose(int x,int y){

    x = root(x);
    y = root(y);

    if(depth[x] > depth[y])
        father[y] = x;

    else if(depth[x] < depth[y])
         father[x] = y;

    else {
        father[x] = y;
            ++depth[y];

    }
}

int main(){

    int x,y,code;

    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d%d",&n,&m);

        while(m){

            scanf("%d%d%d",&code,&x,&y);

                if(code == 1)
                    compose(x,y);
                 else{

                        if(root(x) == root(y))
                            printf("DA\n");
                        else printf("NU\n");
                 }
        --m;
        }
return 0;
}