PAT(Advanced Level) Practice(with python)——1053 Path of Equal Weight (最后一个测试不通过,求解)

PAT(Advanced Level) Practice(with python)——1053 Path of Equal Weight (最后一个测试不通过,求解)_第1张图片

Code

N,M,S = map(int,input().split())

weight_node = list(map(int,input().split()))
child = [[] for i in range(N)]


def dfs(node,weight,path):
    global out
    if weight==S and len(child[node])==0:
        print(path)
    elif weight>S:
        return
    else:
        child_node = child[node]
        child_dict = dict()
        for c in child_node:
            child_dict[c] = weight_node[c]
        sort_child_node = sorted(child_dict.items(),key=lambda x:x[1],reverse=True)
        for c in sort_child_node:
            dfs(c[0],weight+c[1],path+f" {c[1]}")

    
        

for m in range(M):
    root,k,*c = map(int,input().split())
    child[root] += c

out = f'{weight_node[0]}'
dfs(0,weight_node[0],out)

PAT(Advanced Level) Practice(with python)——1053 Path of Equal Weight (最后一个测试不通过,求解)_第2张图片

你可能感兴趣的:(PAT,python,开发语言,深度优先,算法)