Problem p02936 (python)

Fixed submission:
'''
pythonの再帰は10^5timesですら厳しい
import sys
sys.setrecursionlimit(10000000)

def dfs(v,p,G,c):
    for u in G[v]:
        if u!=p:
            c[u]+=c[v]
            dfs(u,v,G,c) 
'''
from collections import deque

N,Q = list(map(int,input().split()))

G=[]
for i in range(N):
    G.append([])
for i in range(N-1):
    a,b = list(map(int,input().split()))
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)

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

stack=deque([0])
parent = [-1]*N

while(stack):
    v = stack.pop()
    for u in G[v]:
        if u!=parent[v]:
            c[u]+=c[v]
            parent[u]=v
            stack.appendleft(u)


ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
import sys

#N,Q = list(map(int,input().split()))
N,Q = [int(i) for i in sys.stdin.readline().split()]
G=[]
for i in range(N):
    G.append([])

for i in range(N-1):
    #a,b = list(map(int,input().split()))
    a,b = [int(j) for j in sys.stdin.readline().split()]
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)

p=[0]*Q
x=[0]*Q
c=[0]*N

for i in range(Q):
    #p,x = list(map(int,input().split()))
    p,x = [int(j) for j in sys.stdin.readline().split()]
    p-=1
    c[p] += x

stack=deque([0])
parent = [-1]*N

while(stack):
    v = stack.pop()
    for u in G[v]:
        if u!=parent[v]:
            c[u]+=c[v]
            parent[u]=v
            stack.append(u)


ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
#再帰を使って各ノードのカウンタの値を決定
def dfs(v,p,G,c):#vは0始まり
    for u in G[v]:
        if u!=p:
            c[u]+=c[v]
            dfs(u,v,G,c) 

N,Q = list(map(int,input().split()))

G=[]
for i in range(N):
    G.append([])
for i in range(N-1):
    a,b = list(map(int,input().split()))
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)

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

dfs(0,-1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
#再帰を使って各ノードのカウンタの値を決定
def dfs(v,p,G,c):#vは1始まり
    for u in G[v]:
        if u!=p:
            c[u-1]+=c[v-1]
            dfs(u,v,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])
    G[b[i]].append(a[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

dfs(1,-1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    for u in G[v]:
        print(u)
        c[u-1]+=c[v-1]
        decide_count(u,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    for u in G[v]:
        c[u-1]+=c[v-1]
        uu=u
        decide_count(uu,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Buggy submission (verdict: Runtime Error):
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    if len(G[v])==0:
        return
    for u in G[v]:
        c[u-1]+=c[v-1]
        decide_count(u,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)