首页 > 其他 > 详细

hdu 2860 Regroup(并查集)

时间:2015-03-12 16:42:37      阅读:308      评论:0      收藏:0      [点我收藏+]

题意:

  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.\n",x,fx);
                }
                else{
                    if(ability[x]==inf){
                        printf("Company %d is empty.\n",x);
                    }else{
                        printf("Lowest rate: %d.\n",ability[x]);
                    }
                }
            }
        }
        puts("");

    }

    return 0;
}

 

hdu 2860 Regroup(并查集)

原文:http://www.cnblogs.com/fish7/p/4332537.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!