问题 2035 --Head of a Gang

2035: Head of a Gang

时间限制: 1 Sec  内存限制: 32 MB
提交: 61  解决: 19
[提交][状态][讨论版][数据上传:][下载FPS1元][下载测试数据1元][60kb]

题目描述

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,但似乎又是不必要的。于是就直接使用一个字符串数组来记录姓名了,这样姓名就与下标一一对应了。


来源

[提交][状态]