题外话
本题不推荐我的写法,一开始的时候没有做很好的构思,在实际操作过程中修修补补导致代码的可读性非常低。希望二刷的时候可以做进一步的改进。同学推荐晴神《算法笔记》的解题思路,有需要可自行查阅。
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 threshold 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:
1 | 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.
浅析
Q:找出联通子图,其结点数大于 2,边的总权值大于 k
Sample Input 1:
1 | 8 59 |
Sample Output 1:
1 | 2 |
Sample Input 2:
1 | 8 70 |
Sample Output 2:
1 | 0 |
Code
1 |
|