阿里算法笔试

编程第二题想知道错在哪里,怎么都ac不了

import sys

if __name__ == "__main__":

# 读取第一行的n

h = sys.stdin.readline().strip()

n,k = map(int, h.split())

score = sys.stdin.readline().strip()

score = list(map(int, score.split()))

score = [-1000] + score

edge = [[] for _ in range(n+1)]

for i in range(n-1):

# 读取每一行

line = sys.stdin.readline().strip()

# 把每一行的数字分隔后转化成int列表

e = list(map(int, line.split()))

edge[e[0]].append(e[1])

def dfs(x):

if edge[x] == []:

return score[x]

for i in range(len(edge[x])):

score[x] += dfs(edge[x][i])

return score[x]

dfs(1)

score.sort(key=lambda x:-x)

print(score[k-1])

全部评论
我一直百分之零,自己试了几个样例也没问题啊
3 回复 分享
发布于 2023-03-26 15:44 北京
e[0]可能是e[1]的父节点,也可能是e[1]的子节点,题目中只说了1是根节点,要根据这个去推导整棵树
2 回复 分享
发布于 2023-03-26 15:50 湖北

相关推荐

七牛云头号黑子:人家是过度包装被看出来没过简历,你是包都不包啊兄弟
点赞 评论 收藏
分享
评论
点赞
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务