PAT 甲级 1034 Head of a Gang
发布日期:2021-07-01 03:09:04 浏览次数:2 分类:技术文章

本文共 2881 字,大约阅读时间需要 9 分钟。

1034 Head of a Gang (30 point(s))

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threthold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10

Sample Output 1:

2AAA 3GGG 3

Sample Input 2:

8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10

Sample Output 2:

0

经验总结:

emmmm   英文实在写的太不爽,而且自己水平有限还词不达意,以后还是用中文啦~   程序猿何苦为难程序猿嘛~

这一题,首先要注意,人家说的是 N<=1000,所以总共可能的最大人数为2000(所有行的两个人都不一样),其他的,就....还有一点吧,这一题可以使用map<string, struct> 的形式进行并查集搜索,也可以将string 转换成对应的唯一的号码进行常规的并查集搜索,当然,因为STL的速度显然没有数组快,所以后者的速度会更快一些~

AC代码

#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn=2010;int n,k,num[maxn],total[maxn];struct person{ int time; int father; person():time(0){}}P[maxn];map
mp,rec;map
rmp;int findFather(int x){ int a=x; while(x!=P[x].father) x=P[x].father; while(a!=P[a].father) { int temp=a; a=P[a].father; P[temp].father=x; } return x;}void Union(int a,int b){ int a1=findFather(a); int b1=findFather(b); if(a1!=b1) { if(P[a1].time
> ope; for(int i=0;i
>a>>b>>t; if(mp.count(a)==0) { rmp[no]=a; mp[a]=no++; } if(mp.count(b)==0) { rmp[no]=b; mp[b]=no++; } P[mp[a]].time+=t; P[mp[b]].time+=t; ope.push_back(make_pair(mp[a],mp[b])); } for(int i=0;i
2&&total[i]/2>k) rec[rmp[i]]=num[i]; } printf("%d\n",rec.size()); for(map
::iterator it=rec.begin();it!=rec.end();++it) { printf("%s %d\n",it->first.c_str(),it->second); } return 0;}

 

转载地址:https://meteorrain.blog.csdn.net/article/details/86617494 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT 甲级 1035 Password
下一篇:PAT 甲级 1033 To Fill or Not to Fill

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月27日 20时02分22秒