Problem p02936 (python)

Fixed submission:
N,Q = map(int,input().split())
parents = {1:0}
for _ in range(N-1):
    a,b = map(int,input().split())
    key,val = max(a,b), min(a,b)
    parents[key] = val
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
results = [0]*(N+2)
for i in range(1,N+1):
    results[i] = counts[i]+results[parents[i]]
    print(results[i], end=' ')

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
childs = [[] for _ in range(N)]
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    childs[a - 1].append(b - 1)

nodes = [0] * (N)
for i in range(Q):
    num, pt = list(map(int, input().split()))
    nodes[num - 1] += pt


def dfs(xs, now=0):
    print(xs, now)
    for x in xs:
        nodes[x] += now
        if childs[x] != []:
            dfs(childs[x], nodes[x])
    return


dfs(childs[0], nodes[0])
print(*nodes)

Buggy submission (verdict: Runtime Error):
# ソートが悪かったりします？
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
# ab = sorted(ab, key=lambda x: x[0])
px = [[0, 0]] * (N - 1)
for i in range(Q):
    px[i] = list(map(int, input().split()))
# px = sorted(px, key=lambda x: x[0])
nodes = [0] * (N)
for p, x in px:
    nodes[p - 1] += x
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: x[0])
px = [[0, 0]] * (N - 1)
for i in range(Q):
    px[i] = list(map(int, input().split()))
px = sorted(px, key=lambda x: x[0])
nodes = [0] * (N)
for p, x in px:
    nodes[p - 1] += x
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
sons = {1:[]}
edges = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    if a==1:
    	sons[a] += [b]
    else:
        if a in edges.keys():
            edges[a] = edges[a]+[b]
        else:
            edges[a] = [b]
            
        if b in edges.keys():
            edges[b] = edges[a]+[b]
        else:
            edges[b] = [a]

counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
        
stack = sons[1]
count_stack = [counts[1]]
ignores = set([])
results = [0]*(N+1)
results[1] = counts[1]
next_stack = []
next_cstack = []
while stack != []:
    next_stack = []
    next_cstack = []
    for x,c in zip(stack,count_stack):
        results[x] = counts[x]+c
        sons[x] = set(edges[x])-ignores
        next_stack += sons[x]
        next_cstack += [results[x]]*len(sons[x])
        ignores = ignores | set([x])
    stack = next_stack
    count_stack = next_cstack
print(' '.join([str(results[i]) for i in range(1,N+1)]))

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
sons = {1:[]}
edges = {}
for _ in range(N-1):
    a,b = map(int,input().split())
    if a==1:
    	sons[a] += [b]
    else:
      	edges[a] = edges[a]+[b] if b in edges.keys() else [b]
        edgese[b] = edges[b]+[a] if a in edges.keys() else [a]

counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
        
stack = sons[1]
count_stack = [counts[1]]
ignores = set([])
results = [0]*(N+1)
results[1] = counts[1]
while stack != []:
  	next_stack = []
    next_cstack = []
	for x,c in zip(stack,count_stack):
      	results[x] = counts[x]+c
  		sons[x] = set(edges[x])-ignores
    	next_stack += sons[x]
        next_cstack += [results[x]]*len(sons[x])
        ignores = ignores | set([x])
        print(results[i], end=' ')
    stack = next_stack
    count_stack = next_cstack

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
parents = {1:0}
for _ in range(N-1):
    a,b = map(int,input().split())
    key,val = max(a,b), min(a,b)
    parents[key] = val
counts = [0]*(N+1)
for _ in range(Q):
    p,x = map(int,input().split())
    counts[p] += x
results = [0]*(N+2)
for i in range(1,N+1):
    results[i] = counts[i]+results[parents[i]]
    print(results[i], end=' ')