Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Runtime Error

Solution code:
def partial_add(p, x, tree, value):
    value[p] += x
    if p in tree.keys():
        for root in tree[p]:
            partial_add(root, x, tree, value)
    return


N, Q = map(int, input().split())
tree = {}
value = [0] * (N+1)

for _ in range(N-1):
    a, b = map(int, input().split())
    try:
        tree[a].append(b)
    except KeyError:
        tree[a] = [b]

for i in range(Q):
    p, x = map(int, input().split())
    partial_add(p, x, tree, value)

print(*value[1:])

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
graph = [[] for _ in range(N)]
#グラフをリストで表現
for _ in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)#リストの番号で頂点を表示するため-1
    graph[b-1].append(a-1)
point = [0]*N
for _ in range(Q):
    a,b = map(int,input().split())
    point[a-1] += b#根にあたる部分にだけコストをプラス


#再帰定義
def dfs(now, prev = -1):#prev,now,nextの三段階を扱う
    for next in graph[now]:#今見ている頂点に連結されているものを見る
        if next == prev:#親だったら飛ばす
            continue
        point[next] += point[now]#子の得点をプラス
        dfs(next,now)#nextが親だけになると自分を呼ぶ前に飛ばされて終わる

dfs(0)#根頂点を指定してスタート
print(*point)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,q = map(int,input().split())
ki = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    ki[a-1].append(b-1)
    ki[b-1].append(a-1)
score = [0]*n

for i in range(q):
    p,x = map(int,input().split()) 
    score[p-1] += x
#print(ki,score)
ans = [0]*n

def dfs(v,p,value): #p:parent of v
    value +=score(v)
    #print(score[v],value)
    ans[v] = value
    for i in ki[v]:
        if i == p:
            continue
        dfs(i,v,value)
dfs(0,-1,0)

print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
lis = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)
c_plus = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    c_plus[p] += x
ans = [-1 for _ in range(n)]
def dfs(inp):
    now = inp[0]
    count = inp[1]
    count += c_plus[now]
    ans[now] = count
    for next in lis[now]:
        if ans[next] == -1:
            dfs([next, count])
dfs([0,0])
for i in range(n):
    ans[i] = str(ans[i])
print(" ".join(ans))

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
    roots[i].append(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  if r:
      for j in r:
          counter[j] += x
print(*counter)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
print(c*)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
for c in counter:
    print(c)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N-1,-1,-1):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
for c in counter:
    print(c)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n

def dfs(idx):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        dfs(i)
    return

dfs(0)
print(*costs)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        dfs(i)
    return

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n
dfs(0)
print(*costs)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx,costs,visited):
    visited[idx]=1
    for i in g[idx]:
        if visited[i]==1:
            continue
        costs[i]+=costs[idx]
        costs = dfs(i,costs,visited)
    return costs

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
visited = [0]*n
costs = dfs(0,costs,visited)
print(*costs)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)
    g[b - 1].append(a - 1)

def dfs(idx,c,res,visited):
    visited[idx]=1
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        if visited[i]==1:
            continue
        res = dfs(i,c,res,visited)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
visited = [0]*n
res = dfs(0,0,res,visited)
print(*res)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)

def dfs(idx,c,res):
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        res = dfs(i,c,res)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
res = dfs(0,0,res)
print(*res)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g=[[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    g[a-1].append(b-1)

def dfs(idx,c,res):
    c+=costs[idx]
    res[idx]=c
    for i in g[idx]:
        res = dfs(i,c,res)
    return res

costs = [0]*n
for i in range(q):
    p, x = map(int, input().split())
    costs[p-1]+=x
res = [0]*n
res = dfs(0,0,res)
output=""
for i in range(n):
    output+=(str(res[i])+" ")
print(output[:-1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**７)

def main():
    n, q = map(int, input().split())
    # バラバラに持ちたいなら
    # for i in range(n-1):
    #     a, b = map(int, input().split())
    # for i in range(q):
    #     q, x = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    # print(ab)
    # print(qx)
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # print(adj)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x

    def dfs(cur, parent):
        for chl in adj[cur]:
            if chl == parent: 
                continue
            v[chl] += v[cur] 
            dfs(chl, cur)

    dfs(1, 0)
    ans = ' '.join(map(str,v[1:]))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10**７)

def main():
    n, q = map(int, input().split())
    # バラバラに持ちたいなら
    # for i in range(n-1):
    #     a, b = map(int, input().split())
    # for i in range(q):
    #     q, x = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    # print(ab)
    # print(qx)
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # print(adj)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x

    def dfs(cur, parent):
        for chl in adj[cur]:
            if chl == parent: 
                continue
            v[chl] += v[cur] 
            dfs(chl, cur)

    dfs(1, 0)
    ans = ' '.join(map(str,v[1:]))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, q = map(int, input().split())
    # バラバラに持ちたいなら
    # for i in range(n-1):
    #     a, b = map(int, input().split())
    # for i in range(q):
    #     q, x = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    # print(ab)
    # print(qx)
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # print(adj)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
        
    # 伝播で足されるものを処理
    def dfs(cur, parent):
        for chl in adj[cur]:
            if chl == parent: 
                continue
            v[chl] += v[cur] 
            dfs(chl, cur)

    dfs(1, 0)
    ans = ' '.join(map(str,v[1:]))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

def dfs(now, parent, total):
    result[now] = total + counter[now]
    for child in tree[now]:
        if child == parent:
            continue
        dfs(child, result[now])

        
N, Q = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

tree = [[] for _ in range(N+1)]
counter = [0 for _ in range(N+1)]

for a,b in edge:
    tree[a].append(b)
    tree[b].append(a)

for p,x in P:
    counter[p] += x

result = [0]*(N+1)
dfs(1, 0, 0)

print(' '.join(map(str, result[1:])))

Submission status: Runtime Error

Solution code:
#ABC138D
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    ab = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    
    edge = [[] for _ in range(N)]
    #有向グラフ
    for row in ab:
        edge[row[0]-1].append(row[1]-1)
    #print(edge)
    point_list = [0 for i in range(N)]
    for row in px:
        point_list[row[0]-1] += row[1]
    #print(point_list)
    
    sys.setrecursionlimit(100000)
    visited = [-1 for _ in range(N)]
    visited[0] = 0
    def dfs(p):
        for c in edge[p]:
            if visited[c]==-1:
                visited[c]=0
                point_list[c] +=point_list[p]
                dfs(c)
            
    
    dfs(0)
    #print(visited)
    print(*point_list)
        



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#ABC138D
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    ab = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    
    edge = [[] for _ in range(N)]
    #有向グラフ
    for row in ab:
        edge[row[0]-1].append(row[1]-1)
    #print(edge)
    point_list = [0 for i in range(N)]
    for row in px:
        point_list[row[0]-1] += row[1]
    #print(point_list)
    
    sys.setrecursionlimit(1000)
    visited = [-1 for _ in range(N)]
    visited[0] = 0
    def dfs(p):
        for c in edge[p]:
            if visited[c]==-1:
                visited[c]=0
                point_list[c] +=point_list[p]
                dfs(c)
            
    
    dfs(0)
    #print(visited)
    print(*point_list)
        



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#ABC138D
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    ab = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    
    edge = [[] for _ in range(N)]
    #有向グラフ
    for row in ab:
        edge[row[0]-1].append(row[1]-1)
    #print(edge)
    point_list = [0 for i in range(N)]
    for row in px:
        point_list[row[0]-1] += row[1]
    #print(point_list)
    
    sys.setrecursionlimit(1000)
    visited = [-1 for _ in range(N)]
    visited[0] = 0
    def dfs(p):
        for c in edge[p]:
            if visited[c]==-1:
                visited[c]=0
                point_list[c] +=point_list[p]
                dfs(c)
            
    
    dfs(0)
    #print(visited)
    print(point_list)
        



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque

n, q = map(int, input().split())
# 隣接リストを作成
adj = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)

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

ans = [0 for i in range(n)]

# スタックによるdfs(位置, 上からの加算, 親の位置)
st = deque([(0,0,-1)])
# stが空になるまで操作
while st:
    l = st.pop()
    ans[l[0]] = l[1] + queries[l[0]]
    for v in adj[l[0]]:
        if v != l[2]:
            st.append((v, ans[l[0], l[0]]))
print(*ans)

Submission status: Runtime Error

Solution code:
# input
N, Q = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
scores = [0 for _ in range(N)]
for _ in range(Q):
    p, x = map(int, input().split())
    scores[p-1] += x

# DFS
def dfs(i, p):
    for j in graph[i]:
        if j != p:
            scores[j] += scores[i]
            dfs(j, i)

dfs(0, -1)
print(" ".join(map(str, scores)))

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = list(map(int,input().split()))
A=[]
B=[]

nodes = {}
for n in range(N):
    a,b = list(map(int,input().split()))
    nodes[a] +=[b]
    nodes[b] +=[a]

querys ={}
for q in range(Q):
    p, x = list(map(int, input().split()))

    querys[p] = querys[p] + x if querys.get(p) else x

counter =[0]*N

def dfs(start, cnt):

    stack  = deque([start])
    seen = []
    while stack:
        s = stack.pop()
        seen.append(s)
        counter[s-1] += cnt
        for v in nodes[s]:
            if v in seen or v in stack:
                continue
            stack.append(v)


for i, x in querys.items():
    dfs(i,x)


print(" ".join(counter))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N - 1)]
px = [list(map(int, input().split())) for i in range(Q)]

path = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    path[a].append(b)
    path[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(point, result):
    next_arr = path[point]
    for i in range(len(next_arr)):
        next_p = next_arr[i]
        path[next_p].remove(point)
        result[next_p] = result[next_p] + result[point]
        if len(path[next_p]) != 0:
            dfs(next_p, result)


dfs(1, result)

print(*result[1:])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N - 1)]
px = [list(map(int, input().split())) for i in range(Q)]

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    p[a].append(b)
    p[b].append(a)

for i in range(Q):
    result[px[i][0]] = result[px[i][0]] + px[i][1]


def dfs(point, result):
    for i in range(len(p[point])):
        next = p[point][i]
        p[next].remove(point)
        result[next] = result[next] + result[point]
        if len(p[next]) != 0:
            dfs(next, result)
    return result


result = dfs(1, result)

print(*result[1:])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N - 1)]
px = [list(map(int, input().split())) for i in range(Q)]

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]
flag = [0 for i in range(N + 1)]

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    p[a].append(b)
    p[b].append(a)

for i in range(Q):
    result[px[i][0]] = result[px[i][0]] + px[i][1]


def dfs(point, result):
    flag[point] = 1
    for i in range(len(p[point])):
        next = p[point][i]
        if flag[next] != 1:
            result[next] = result[next] + result[point]
            dfs(next, result)
    return result


result = dfs(1, result)

print(*result[1:])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N - 1)]
px = [list(map(int, input().split())) for i in range(Q)]

ab.sort()

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    p[a].append(b)


def dfs(point, score, result):
    result[point] = result[point] + score
    for i in range(len(p[point])):
        next = p[point][i]
        dfs(next, score, result)
    return result


for i in range(Q):
    result = dfs(px[i][0], px[i][1], result)

print(*result[1:])

Submission status: Runtime Error

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

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)]

tree = [[] for _ in range(N)]
count = [0] * N

for a, b in ab:
    tree[a - 1].append(b - 1)

for p, x in px:
    count[p - 1] += x

d: deque = deque([])


def dfs(p):
    d.append(p)

    while d:
        p = d.pop()

        for np in tree[p]:
            count[np] += count[p]
            d.append(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Runtime Error

Solution code:
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)]

tree = [[] for _ in range(N)]
count = [0] * N

for a, b in ab:
    tree[a - 1].append(b - 1)


def dfs(p, x):
    count[p] += x

    for np in tree[p]:
        dfs(np, x)


for p, x in px:
    dfs(p - 1, x)

for c in count:
    print(c, end=" ")

print()

Submission status: Runtime Error

Solution code:
def dfs(v, cur):
    seen[v] = 1
    s[v] = cur + plus[v]
    for i in G[v]:
        if not seen[i]:
            dfs(i, s[v])


n, q = map(int, input().split())
G = [[] for i in range(n)] #G(i): 頂点iと隣接する頂点のlist
m = n-1
a = [0] * m
b = [0] * m
for i in range(m):
    a[i], b[i] = map(int, input().split())
    G[a[i]-1].append(b[i]-1) #入力は 1-indexed とする
    G[b[i]-1].append(a[i]-1) #無向グラフの場合

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

s = [0] * n
seen = [0] * n

dfs(0, 0)
print(' '.join([str(i) for i in s]))

Submission status: Runtime Error

Solution code:
import sys
import io
#sys.stdin = io.StringIO(input_string)
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def resolve():
	from collections import deque
	
	n, q = map(int, input().split())
	adj = [[] for i in range(n+1)]
	for i in range(n-1):
		a, b = map(int, input().split())
		adj[a-1].append(b-1)
		adj[b-1].append(a-1)
	ans = [0 for i in range(n)]
	for i in range(q):
		p, x = map(int, input().split())
		p -= 1
		ans[p] += x
#	print(f"{adj=}\n{ans=}")

	def dfs(now, prv):
		for next in adj[now]:
#			print(f"{now=}, {next=}")
			if nextinput = sys.stdin.readline == prv:
				continue
			ans[next] += ans[now]
			dfs(next, now)

	dfs(0, 0)
	print(*ans)

resolve()

Submission status: Runtime Error

Solution code:
def input():
  	import sys
	return sys.stdin.readline()[:-1]

def resolve():
	from collections import deque
	
	n, q = map(int, input().split())
	adj = [[] for i in range(n+1)]
	for i in range(n-1):
		a, b = map(int, input().split())
		adj[a].append(b)
		adj[b].append(a)
	p = [list(map(int, input().split())) for i in range(q)]
	
	ans = [0 for i in range(n+1)]
	tmp = [0 for i in range(n+1)]
	for i in range(q):
		tmp[p[i][0]] += p[i][1]

	for start, point in enumerate(tmp):
		q = deque([start])
		while q:
			node = q.popleft()
			ans[node] += tmp[start]
			for i in list(filter(lambda x: x>node, adj[node])):
				q.append(i)

	for i in ans[1:]:
		print(i, end=" ")

#resolve()

Submission status: Runtime Error

Solution code:
#%%
import sys
sys.setrecursionlimit(10 ** 9)
from collections import deque

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        

def main():
    global instance_list
    N, Q = [int(i) for i in input().split()]
    instance_list = [Node() for _ in range(N)]

    for i in range(N - 1):
        temp1, temp2 = [int(i) - 1 for i in input().split()]
        instance_list[temp1].children.append(temp2)
        instance_list[temp2].parent = temp1

    for i in range(Q):
        start, value = [int(i) for i in input().split()]
        start -= 1
        instance_list[start].value += value
        
    que = deque()
    que.append([instance_list[0], 0])
    while len(que) > 0 :
        node, cost = que.popleft()
        instance_list[node].value += cost
        for i in instance_list[node].children:
            que.append([i, cost])
    

    ans = [instance_list[i].value for i in range(N)]
    print(" ".join(map(str, ans)))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#%%
class Node():
    
    def __init__(self):
        self.parent = -1
        self.left = -1
        self.right = -1
        self.value = 0
        
    @classmethod
    def get_parent_children(cls, node_data):
        
        if instance_list[node_data[0]].left == -1:
            instance_list[node_data[0]].left = node_data[1]
        elif instance_list[node_data[0]].right == -1:
            instance_list[node_data[0]].right = node_data[1]
            
        instance_list[node_data[1]].parent = node_data[0]
        
def DFS_138(u, value):
    if u == -1:
        return 
    
    instance_list[u].value = instance_list[u].value + value
    DFS_138(instance_list[u].left, value)
    DFS_138(instance_list[u].right, value)
    
#%%
N, Q = [int(i) for i in input().split()]
node_data = [[] for _ in range(N - 1)]
for i in range(N - 1):
    node_data[i] = [int(i) - 1 for i in input().split()]
    
value_data = [[] for _ in range(Q)]
for i in range(Q):
    value_data[i] = [int(i) for i in input().split()]
    value_data[i][0] -= 1
#%%
instance_list = [Node() for _ in range(N)]

for i in node_data:
    Node.get_parent_children(i)
    
for i in value_data:
    DFS_138(i[0], i[1])
#%%
ans = [instance_list[i].value for i in range(N)]
print(" ".join(map(str, ans)))

Submission status: Runtime Error

Solution code:
f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")
import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, f.readline().split())
ab = [list(map(int, f.readline().split())) for _ in range(N-1)]
px = [list(map(int, f.readline().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    counter = counter + counters[node]
    results[node] = counter
    candidates = branch[node]
    for candidate in candidates:
        if candidate != pre_node:
            repeat(candidate, counter, node)

repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(int(1e+10))
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)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    counter += counters[node]
    for candidate in branch[node]:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter
    return
    
repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

Submission status: Runtime Error

Solution code:
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)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    candidates = branch[node]
    counter += counters[node]
    
    for candidate in candidates:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter
    return
    
repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

Submission status: Runtime Error

Solution code:

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)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    candidates = branch[node]
    counter += counters[node]
    
    for candidate in candidates:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter
    return
    
repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))