• 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;
    }
  • 相关阅读:
    Alpha项目冲刺_博客链接合集
    项目系统设计
    项目需求分析
    项目选题
    项目展示
    原型设计 + 用户规格说明书
    测试与优化
    结对作业1
    MathExam6317
    js入门基础
  • 原文地址:https://www.cnblogs.com/fish7/p/4332537.html
Copyright © 2020-2023  润新知