HDU 2860 Regroup(并查集)

28 篇文章 0 订阅
10 篇文章 0 订阅

Regroup

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


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

题目大意:

    有多个公司,公司可以招人,公司之间也可以合并,每一个公司的战斗力等于该公司员工能力值得最小值,给出公司一开始的成员能力值和一些操作指令,对于每个指令输出相应的值。


解题思路:

    根据题目描述,使用并查集维护。将每一个公司看做一个集合,由于这个集合的属性只有战斗力一项,所以我们只需要维护每个集合的员工能力值的最小值即可。对于公司之间的合并,我们可以调用并查集中的Merge操作然后更新战斗力。本题需要注意公司不可以和自己合并。


附AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int n,k,m;
struct Node{
    int fa;
    LL v;
}father[100005];

void init(int n)
{
    for(int i=0;i<=n;i++)
    {father[i].fa=i;father[i].v=1e18;}
}

int Find(int x)
{
    int r=x;
    while(father[x].fa!=x)x=father[x].fa;
    father[r].fa=x;
    return x;
}

void Merge(int x,int y)
{
    if(x!=y)
        father[x].fa=y;
}

int main()
{
    while(~scanf("%d %d %d",&n,&k,&m))
    {
        init(n);
        for(int i=1;i<=k;i++)
        {
            int r,c;
            scanf("%d %d",&r,&c);
            father[c].v=min(father[c].v,(LL)r);
        }
        char str[2];
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%s",str);
            if(strcmp(str,"AP")==0)
            {
                scanf("%d %d",&x,&y);
                if(father[y].fa!=y)printf("Reject\n");
                else
                {father[y].v=min(father[y].v,(LL)x);puts("Accept");}
            }
            else if(strcmp(str,"MG")==0)
            {
                scanf("%d %d",&x,&y);
                if(x==y)puts("Reject");
                else if(father[x].fa!=x||father[y].fa!=y)puts("Reject");
                else {father[x].v=min(father[y].v,father[x].v);
                    Merge(y,x);puts("Accept");}
            }
            else if(strcmp(str,"GT")==0)
            {
                scanf("%d",&x);
                if(father[x].fa!=x){
                    int fx=Find(x);
                    printf("Company %d is a part of company %d.\n",x,fx);
                }
                else if(father[x].v==1e18)printf("Company %d is empty.\n",x);
                else printf("Lowest rate: %lld.\n",father[x].v);
            }
        }
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值