Problem p02936 (python)

Fixed submission:
N,Q=map(int, input().split())
node_lst=[list(map(int, input().split())) for i in range(N-1)]
ope_lst=[list(map(int, input().split())) for i in range(Q)]
cnt_lst=[0 for i in range(N)]
for ope in ope_lst:
    p, x = ope[0], ope[1]
    cnt_lst[p-1]+=x
node_lst=sorted(node_lst, key=lambda x:x[0])
for l in node_lst:
    src, dst = l[0]-1, l[1]-1
    cnt_lst[dst]+=cnt_lst[src]
print(" ".join([str(c) for c in cnt_lst]))

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
to = [[] for _ in range(n)]
score = [0 for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    to[a].append(b)
    to[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    score[p] += x

def dfs(v, p=-1):
    for u in to[v]:
        if u == p:
            continue
        score[u] += score[v]
        dfs(u, p=v)

dfs(0)
print(*score)

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
ab=[list(map(int, input().split())) for _ in range(N-1)]
px=[list(map(int, input().split())) for _ in range(Q)]
to=[[] for _ in range(N)]
co=[0 for _ in range(N)]

for a, b in ab:
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)
for p, x in px:
    p-=1
    co[p]+=x

def dfs(v, p=-1):
    for u in to[v]:
        if p==u:
            continue
        co[u]+=co[v]
        dfs(u, v)

dfs(0)
print(*co)

Buggy submission (verdict: Runtime Error):
N, Q=map(int, input().split())
ab=[list(map(int, input().split())) for _ in range(N-1)]
px=[list(map(int, input().split())) for _ in range(Q)]
to=[[] for _ in range(N)]
co=[0 for _ in range(N)]

for a, b in ab:
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)
for p, x in px:
    p-=1
    co[p]+=x

def dfs(v, p=-1):
    for u in to[v]:
        if p==u:
            continue
        co[u]+=co[v]
        dfs(u, v)

dfs(0)
print(co)

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
node_lst=[list(map(int, input().split())) for i in range(N-1)]
ope_lst=[list(map(int, input().split())) for i in range(Q)]
cnt_lst=[0 for i in range(N)]
child_rel={}
parent_rel={}
for l in node_lst:
    src, dst = l[0], l[1]
    if src not in child_rel.keys():
        child_rel[src]=[dst]
    else:
        child_rel[src].append(dst)
    if src in parent_rel.keys():
        for e in parent_rel[src]:
            child_rel[e].append(dst)
    if dst not in parent_rel.keys():
        parent_rel[dst]=[src]
    else:
        parent_rel[dst].append(src)
print(child_rel)
print(parent_rel)
for ope in ope_lst:
    p, x = ope[0], ope[1]
    cnt_lst[p-1]+=x
    if p in node_rel.keys():
        for e in node_rel[p]:
            cnt_lst[e-1]+=x
print(" ".join([str(c) for c in cnt_lst]))

Buggy submission (verdict: Runtime Error):
N,Q=map(int, input().split())
node_lst=[list(map(int, input().split())) for i in range(N-1)]
ope_lst=[list(map(int, input().split())) for i in range(Q)]
cnt_lst=[0 for i range(N)]
node_rel={}
for l in node_lst:
    src, dst = l[0], l[1]
    if src not in node_rel.keys():
        node_rel[src]=[dst]
    else:
        node_rel[src].append(dst)
    for k, v in node_rel.items():
        if src in v:
            node_rel[k].append(dst)
for ope in ope_lst:
    p, x = ope[0], ope[1]
    cnt_lst[p-1]+=x
    if p in node_rel.keys():
        for e in node_rel[p]:
            cnt_lst[e-1]+=x
print(" ".join([str(c) for c in cnt_lst]))