2770Head of a Gang

2770   Head of a Gang

题目描述

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

说明

这一题需要解决的问题是给定通话记录,找出各党派。输出首领以及党派里的成员数目。    首先想到的是并查集,但考研数据结构并没有涉及到并查集。那就使用图论里的搜索。对,就dfs了。但无论是深搜还是广搜,在一个二维数组里是比较方便的。当然题目中每个人之间的联系是能够构成一个二维数组,但人名是用字符串标志的,而非整数。这样对使用dfs造成了一定的难度。于是我们可以将姓名与整数一一对应。首先想到的解决方案是使用STL中的map,但似乎又是不必要的。于是就直接使用一个字符串数组来记录姓名了,这样姓名就与下标一一对应了。

2
5
通过提交
时空限制1000ms/32mb
题目来源
评测方式在线评测
题目类型
难        度