hdu 2860 Regroup(并查集)

题意:

  AP x y
A recruit with ability rate x were asked to join company y. (0<=x<2^31, 0<=y<n)

  MG x y
Company x and company y is merged. The new company is numbered as x. (0<=x, y<n)

  GT x
Report the fighting capacity of company x. (0<=x<n)

思路:

并查集,判断好各种可能的情况。

代码:

int n,k,m;
int fa[100005], ability[100005];



int findFa(int x){
    if(fa[x]==x)
        return fa[x];
    int t=fa[x];
    fa[x]=findFa(fa[x]);
    ability[x] = min( ability[x],ability[t] );
    return fa[x];
}


int main(){

    while(scanf("%d%d%d",&n,&k,&m)!=EOF){
        rep(i,0,n-1){
            fa[i]=i;
            ability[i]=inf;
        }

        rep(i,1,k){
            int rate,belongTo;
            scanf("%d%d",&rate,&belongTo);
            ability[belongTo]=min( ability[belongTo],rate );
        }
        while(m--){
            char ope[5];
            scanf("%s",ope);
            if(ope[0]=='A'){
                int x,y;
                scanf("%d%d",&x,&y);
                int faTemp=findFa(y);
                if(y!=faTemp){
                    puts("Reject");
                }else{
                    ability[faTemp]=min( ability[faTemp],x );
                    puts("Accept");
                }
            }
            else if(ope[0]=='M'){
                int x,y;
                scanf("%d%d",&x,&y);
                int fx=findFa(x);
                int fy=findFa(y);
                if(fx!=x || fy!=y || fx==fy){
                    puts("Reject");
                }else{
                    ability[fx]=min( ability[fx],ability[fy] );
                    fa[fy]=fx;
                    puts("Accept");
                }
            }
            else{
                int x;
                scanf("%d",&x);
                int fx=findFa(x);
                if(x!=fx){
                    printf("Company %d is a part of company %d.
",x,fx);
                }
                else{
                    if(ability[x]==inf){
                        printf("Company %d is empty.
",x);
                    }else{
                        printf("Lowest rate: %d.
",ability[x]);
                    }
                }
            }
        }
        puts("");

    }

    return 0;
}
原文地址:https://www.cnblogs.com/fish7/p/4332537.html