1034 Head of a Gang (30 分)
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.
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.
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.
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
2
AAA 3
GGG 3
8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
0
题目中要求找出总联系时间大于k,并且联系的圈子人数大于2人的圈子,输出圈子中联系时间最长的人以及圈子的人数,按照圈子人数联系时间最长的人姓名排序
首先可以想到,将相互联系的人建成一棵树,并且联系时间最长的要放在树根,那么最简单的就是使用并查集来实现了,只能实现离线操作,因为如果是在线操作,整棵树上可以找到相应树的父亲节点,但是找不到当前节点的儿子节点,无法更新根部(联系时间最长的节点),
尽量将联系时间长的人放在根部,那么他可以在Union函数中实现,即判断两个节点的大小关系,在更新per函数即可,实现还算是比较简单的,只是有种样例考虑了很久
3 50
aa bb 20
bb cc 20
cc dd 20
这种情况下,aa节点权值为40,bb权值40,cc权值40,所以最后到底是输出aa、bb、还是cc呢?我的程序输出是cc,测试样例中应该是没有这种样例,因为总是感觉输出aa会比较好一些
#include
#include
#include
#include
#include
#include
#include