HDU2860Regroup(并查集)
来源:互联网 发布:mac os 10.13 vmtools 编辑:程序博客网 时间:2024/06/18 07:13
Regroup
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 660 Accepted Submission(s): 161
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
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)
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
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 105 05 15 25 15 0GT 0GT 3AP 3 3GT 3GT 4MG 3 4GT 4MG 1 3GT 4GT 1
Sample Output
Lowest rate: 5.Company 3 is empty.AcceptLowest rate: 3.Company 4 is empty.AcceptCompany 4 is a part of company 3.AcceptCompany 4 is a part of company 1.Lowest rate: 3.
看代码就明白题意。
#include<stdio.h>const int N = 100005;int fath[N],val[N],n;void init(){ for(int i=0;i<=n;i++) fath[i]=i,val[i]=-1;}int findfath(int x){ if(x!=fath[x]) fath[x]=findfath(fath[x]); return fath[x];}int main(){ int k,m,a,b; char st[20]; while(scanf("%d%d%d",&n,&k,&m)>0) { init(); while(k--) { scanf("%d%d",&a,&b); if(val[b]==-1||val[b]>a) val[b]=a; } while(m--) { scanf("%s",st); if(st[0]=='A') { scanf("%d%d",&a,&b); if(fath[b]==b) { if(val[b]==-1||val[b]>a) val[b]=a; printf("Accept\n"); } else printf("Reject\n"); } else if(st[0]=='M') { scanf("%d%d",&a,&b); if(fath[a]!=a||fath[b]!=b||a==b) { printf("Reject\n"); continue; } printf("Accept\n"); if(val[a]==-1||val[b]!=-1&&val[a]>val[b]) val[a]=val[b]; fath[b]=a; } else { scanf("%d",&a); if(fath[a]!=a) { b=findfath(a); printf("Company %d is a part of company %d.\n",a,b); } else { if(val[a]==-1) printf("Company %d is empty.\n",a); else printf("Lowest rate: %d.\n",val[a]); } } } printf("\n"); }}
0 0
- HDU2860Regroup(并查集)
- 并查集(集并查)
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- 并查集, 畅通工程(简单并查集)
- [数据结构]并查集水体POJ2236(并查集)
- 并查集(disjoint_set)
- 并查集(模板)
- 并查集(亲戚)
- POJ2524(并查集)
- POJ_2236(并查集)
- poj2492(并查集)
- HDU1272(并查集)
- 随笔(并查集)
- POJ1988(并查集)
- hdoj1272(并查集)
- 动态规划 背包问题
- Handler机制
- LeetCode Sort Colors
- vc++ 2012 文件清单 mfc
- bbed的使用--安装及初探
- HDU2860Regroup(并查集)
- .net reactor 学习系列
- bbed的使用--查看数据文件信息 & sid信息
- [uboot学习笔记(一)]u-boot.lds文件分析
- 【Qt5开发及实例】33、多线程网络时间服务器
- Scala
- 一道水题
- iOS开发- UICollectionView详解
- 『水晶报表』实现打印