PAT 1034 Head of a Gang (30 分)————Python

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 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:

Name1 Name2 Time

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 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

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

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

ample Output 2:

0

Code

#并查集-找根结点
def findroot(a):
    while(father[a]!=a):
        a = father[a]
    return a

#并查集-合并集合+保证根结点最小
def union_ab(a,b):
    fa = findroot(a)
    fb = findroot(b)
    if(fa <= fb):
        father[fb] = fa
    else:
        father[fa] = fb

#接收输入
line = input().split(" ")
n = int(line[0])
cmax = int(line[1]) #cmax是连通块的权重阈值

call = {}   #记录每人的累计通话记录时长
father = {} #记录每人的父结点

for i in range(n):
    line = input().split(" ")   #接受每行,并用空格分开
    a,b,t= line[0],line[1],int(line[2])
    #累计每人的通话时长
    if a not in call:
        call[a] = 0
    if b not in call:
        call[b] = 0
    call[a] += t
    call[b] += t
    #合并每人所在的集合
    if a not in father: #若首次出现,则初始化其根结点
        father[a] = a
    if b not in father:
        father[b] = b
    union_ab(a,b)

#遍历所有人,统计它们所在的连通块
gang = {}
call['AAAA'] = 0
for key,value in father.items():
    root = findroot(key)
    if root not in gang:
        gang[root] = [0,0,'AAAA']
    gang[root][0] += call[key] #连通块权重增加
    gang[root][1] += 1  #该连通块人数增加
    if call[key] >= call[gang[root][2]] :    #若该*重更大,更换队首
        gang[root][2] = key

#删除不合格的连通块
for key in list(gang.keys()):
     if gang[key][0] <= 2*cmax :
         del gang[key]
     elif gang[key][1] <= 2:
         del gang[key]

#对连通块排序
x = sorted(gang.items(),key = lambda x:(x[1][2])) #按队首名称升序,sort不会改变原序列,返回新序列

#按要求输出
print(len(x))
for i in x:
    print(i[1][2],i[1][1])

运行结果
PAT 1034 Head of a Gang (30 分)————Python

上一篇:使用并查集解决head of gang


下一篇:Find them, Catch them-POJ - 1703-并查集