Hdu 2860 Regroup 并查集

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1430    Accepted Submission(s): 377


Problem Description
When ALPC42 got to a panzer brigade, He was asked to build software to help them regroup the battalions or companies.
As the tradition of army, soldiers are rated according his or her abilities, taking the rate as an integer. The fighting capacity of a company is determined by the soldier in this company whose rate is lowest. Now the recruits those rated are coming and join to their companies according to the order form HQ.
With the coming of new recruits, a big regroup action reached, asking to merge some companies into one. The designation of a company, however, will not be canceled, but remain for memorialize what the company is done, means the designation of the company is still exist, but the company is gone, so it is unable to ask recruits to join this company, or merge the company into others.
A strange thing is, the orders sometimes get wrong, send newbie to a company which is already merged into another, or mentioned some only-designation-existed companies. Such order could be rejected.
The brigadier wants to know every change of each order, so the program should able to report the status of every order, telling whether it is accept, and can query the fighting capacity of specified company. (To simplify, companies are numbered from 0 to n-1
 

 

Input
There may be several test cases.
For each case, the integers in first line, n, k, m, telling that there are n companies, k soldiers already, and m orders needs be executed. (1<=n ,k ,m<=100000).
Then k lines with two integers R and C for each, telling a soldier with rate R is now in company C
Then m lines followed, containing 3 kinds of orders, in upper case:
  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)
 

 

Output
For each order there is exact one line to report the result.
For AP and MG order, print “Accept” if it is able to be done, and execute it, or “Reject” if it is an illegal order.
For GT order, if company x is still exist (not merged into others), print as “Lowest rate: y.” which y is the minimal rate of soldiers in this company. If there is no one in this company, tell "Company x is empty." If company x is already merged into others, print "Company x is a part of company z." z is the company where the company x is in.
Print a blank line after each case
 

 

Sample Input
5 5 10 5 0 5 1 5 2 5 1 5 0 GT 0 GT 3 AP 3 3 GT 3 GT 4 MG 3 4 GT 4 MG 1 3 GT 4 GT 1
 

 

Sample Output
Lowest rate: 5. Company 3 is empty. Accept Lowest rate: 3. Company 4 is empty. Accept Company 4 is a part of company 3. Accept Company 4 is a part of company 1. Lowest rate: 3.
 

 

Source
 

 

Recommend
gaojie
 

很水的一道并查集,维护最小的权值即可

巨坑1:是如果x==y,那么输出"Reject"

巨坑2:如果有的军团既没有士兵又已经与别的军团合并,按已合并的情况输出

代码:

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map> 
const long long inf = 1e13;
struct DSU{
    long long *father;
    long long *min_able;
    DSU(long long n){//初始化一个节点数为n+1(编号0-n)的集合 
        father = new long long[n+1];
        min_able = new long long[n+1];
        /*把每一个节点设置为根节点,
        也就是每个节点都是一个独立的集合*/
        for(long long i=0;i<=n;i++)
            father[i]=i,min_able[i]=inf;
    }
    long long Get_root(long long x,long long k=inf){//得到x节点的根节点 
        min_able[x] = (k<min_able[x])?k:min_able[x];
        if(father[x]==x)//如果某节点的父亲依然是这个节点
            return x;//那么这个点就是根节点
        //如果不是根节点 
        long long root = Get_root(father[x],min_able[x]);//那么递归地找它父亲的根节点 
        father[x] = root;//路径压缩 
        return root;//返回根节点的值 
    }
    void Union(long long y,long long x){//合并两个集合 
        long long root_x = Get_root(x);
        long long root_y = Get_root(y);
        father[root_x] = root_y;//x的根节点接在y的树上
        Get_root(x);    
    }
    bool Judge(long long x,long long y){//判断两个节点是否是一个集合的
         //等价于根节点是否相同 
         return Get_root(x)==Get_root(y);    
    }
    void Min(long long x){
     
     if(Get_root(x)!=x){
         printf("Company %lld is a part of company %lld.\n",x,Get_root(x));
         return;
     }  
     if(min_able[Get_root(x)]==inf){
         printf("Company %lld is empty.\n",x);
         return;
     }             
     printf("Lowest rate: %lld.\n",min_able[Get_root(x)]);
    }
};
long long n,m,k;
DSU* s;
int main(){
    while(scanf("%lld%lld%lld",&n,&m,&k)!=EOF){
        s = new DSU(n);//声明一个节点数为n+1的并查集 
        for(long long i=0;i<m;i++){
            long long p,q;
            scanf("%lld%lld",&p,&q);
            s->min_able[q] = min(s->min_able[q],p);
        }
        char ss[10];
        for(long long i=0;i<k;i++){//有几个集合就看有几个节点的父节点是本身 
            scanf("%s",ss);
            if(ss[0]=='G'){
                long long p;
                scanf("%lld",&p);
                s->Min(p);
            } 
            else if(ss[0]=='A'){
                long long p,q;
                scanf("%lld%lld",&p,&q);
                if(s->Get_root(q)!=q){
                    printf("Reject\n");    
                    continue;
                }
                s->min_able[q] = min(s->min_able[q],p);
                s->Get_root(q);
                printf("Accept\n");
            }
            else if(ss[0]=='M'){
                long long p,q;
                scanf("%lld%lld",&p,&q);
                if(s->Get_root(q)!=q||s->Get_root(p)!=p||p==q){
                    printf("Reject\n");    
                    continue;
                }
                s->Union(p,q);
                printf("Accept\n");
            }
        }
        printf("\n");    
    }
    return 0;
}

 

posted @ 2018-02-02 15:06  晓风微微  阅读(178)  评论(0编辑  收藏  举报