Problem p02936 (python)

Fixed submission:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
n,q = [int(i) for i in readline().split()]

g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = [int(i) for i in readline().split()]
    g[a-1].append(b-1)
    g[b-1].append(a-1)

add = [0]*n
for _ in range(q):
    p,x = [int(i) for i in readline().split()]
    add[p-1] += x

ans = [0]*n
#ans[0] = add[0]

def dfs(v,p,value): #p: parent of v
    value += add[v]
    ans[v] = value
    for c in g[v]:
        if c == p: continue
        dfs(c,v,value)

dfs(0,-1,0)

#print(add)

print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


n,q=[int(_) for _ in input().split()]
A=[[] for i in range(n)]
node=[0]*n

for i in range(n-1):
    a,b=[int(_) for _ in input().split()]
    a -=1;b -=1
    A[a].append(b)
    A[b].append(a)

for i in range(q):
    p,x=[int(_) for _ in input().split()]
    node[p-1] +=x

calculate(0)
print(*node)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
line=sys.stdin.readline

n,q=map(int,line().split())
A=[[] for i in range(n)]
node=[0]*n

for i in range(n-1):
    a,b=map(int,line().split())
    a -=1;b -=1
    A[a].append(b)
    A[b].append(a)

for i in range(q):
    p,x=map(int,line().split())
    node[p-1] +=x


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline

n,q=map(int,input().split())
A=[[] for i in range(n)]
node=[0]*n

for i in range(n-1):
    a,b=map(int,input().split())
    a -=1;b -=1
    A[a].append(b)
    A[b].append(a)

for i in range(q):
    p,x=map(int,input().split())
    node[p-1] +=x


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(100000)

n,q=map(int,input().split())
A=[[] for i in range(n)]
node=[0]*n

for i in range(n-1):
    a,b=map(int,input().split())
    a -=1;b -=1
    A[a].append(b)
    A[b].append(a)

for i in range(q):
    p,x=map(int,input().split())
    node[p-1] +=x


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
A=[[] for i in range(n)]
node=[0]*n

for i in range(n-1):
    a,b=map(int,input().split())
    a -=1;b -=1
    A[a].append(b)
    A[b].append(a)

for i in range(q):
    p,x=map(int,input().split())
    node[p-1] +=x


def calculate(num,pre=-1):
    for i in A[num]:
        if i==pre:continue
        node[i] +=node[num]
        calculate(i,num)


calculate(0)
print(*node)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)


def rootedTree(node,parent,root,point):
    B.append([node,parent,root,point])
    for i in root:
        rootedTree(i,node,tree[i],point+C[i])


n,q=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n-1)]
B=[]
C=[0 for j in range(n+1)]
for i in range(q):
    add,point=map(int,input().split())
    C[add] +=point

tree=[[] for i in range(n+1)]
for i,j in A:tree[i] +=[j]

rootedTree(1,-1,tree[1],C[1])
ans=[0]*n
for i in B:
    ans[i[0]-1]=i[3]
print(*ans)

Buggy submission (verdict: Runtime Error):
def Constrcuted(A,C,n):
    tree=[[] for i in range(n+1)]
    for i,j in A:
        tree[i] +=[j]
    top=[k+1 for k in range(n)]
    top=top[0]
    rootedTree(top,-1,tree[top],tree,C[top])


def rootedTree(node,parent,root,tree,point):
    B.append([node,parent,tree[node],point])
    for i in root:
        rootedTree(i,node,tree[i],tree,point+C[i])


n,q=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n-1)]
B=[]
C=[0 for i in range(n+1)]
for i in range(q):
    add,point=map(int,input().split())
    C[add] +=point

Constrcuted(A,C,n)
ans=[0]*n
for i in B:
    ans[i[0]-1]=i[3]
print(*ans)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
box=[list(map(int,input().split())) for i in range(n-1)]
point=[list(map(int,input().split())) for i in range(q)]
tree={i+1:[] for i in range(n)}
answer=[0]*n
for x,y in box:tree[x] +=[y]

def tree_sum(tree,point_x,point_y):
    answer[point_x-1] +=point_y
    for i in tree[point_x]:
        if len(tree[i])!=0:
            tree_sum(tree,i,point_y)
        else:answer[i-1] +=point_y
    return tree
for x,y in point:
    tree_sum(tree,x,y)

print(*answer)