Cod sursa(job #1238375)

Utilizator FayedStratulat Alexandru Fayed Data 6 octombrie 2014 20:40:47
Problema Paduri de multimi disjuncte Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>
#define NMAX 100000

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

int root(int node){

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

return node;
}

void unite(int node1, int node2){

    node1 = root(node1);
    node2 = root(node2);

    if(height[node1] > height[node2])

        father[node2] = node1;

    else if(height[node1] < height[node2])

        father[node1] = node2;

    else { father[node1] = node2;
                height[node2]++;
    }
}

void solve(){

    int op,x,y;

    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);

    scanf("%d%d",&n,&m);

    for(register int i=1;i<=m;++i){

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

        if(op == 1)
            unite(x,y);

        else{
            if(root(x) == root(y))
                    printf("DA\n");
            else printf("NU\n");
    }
}
}

int main(){

    solve();

return 0;
}