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:
N, Q = map(int,input().split())
tree = [[0] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    if tree[a] == [0]:
        tree[a] = [b]
    else:
        tree[a].append(b)
counter = [0 for i in range(N+1)]
def dfs(p,x):
    counter[p] += x
    for i in tree[p]:
        if i != 0:
            dfs(i,x)
               
    
for i in range(Q):
    p,x = map(int,input().split())
    dfs(p,x)
counter = [str(i) for i in counter]
ans = ' '.join(counter[1:])
print(ans)

Submission status: Runtime Error

Solution code:

N, Q = map(int, input().split())
T = {}
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a in T:
        T[a].append(b)
    else:
        T[a] = [b]
    if b in T:
        T[b].append(a)
    else:
        T[b] = [a]

d = [0] * N

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

def dfs(u, v = -1):
    global d
    if u in T:
        for p in T[u]:
            if p != v:
                d[p] += d[u]
                dfs(p, u)

dfs(0)


print(' '.join(map(str, d)))

Submission status: Runtime Error

Solution code:
N, Q = map(int,input().split())
tree = [[0] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    if tree[a] == [0]:
        tree[a] = [b]
    else:
        tree[a].append(b)
counter = [0 for i in range(N+1)]
def dfs(p,x):
    if p == 0:
        return
    counter[p] += x
    for i in tree[p]:
        dfs(i,x)   
    
for i in range(Q):
    p,x = map(int,input().split())
    dfs(p,x)
counter = [str(i) for i in counter]
ans = ' '.join(counter[1:])
print(ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int,input().split())
tree = [[0] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    if tree[a] == [0]:
        tree[a] = [b]
    else:
        tree[a].append(b)
counter = [0 for i in range(N+1)]
def dfs(p,x):
    if p == 0:
        return
    counter[p] += x
    for i in tree[p]:
        dfs(i,x)   
    
for i in range(Q):
    p,x = map(int,inupt().split())
    dfs(p,x)
counter = [str(i) for i in counter]
ans = ' '.join(counter[1:])
print(ans)

Submission status: Runtime Error

Solution code:

N, Q = map(int, input().split())
T = {}
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a in T:
        T[a].append(b)
    else:
        T[a] = [b]
    if b in T:
        T[b].append(a)
    else:
        T[b] = [a]

d = [0] * N

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

def dfs(u, v = -1):
    if u in T:
        for p in T[u]:
            if p != v:
                d[p] += d[u]
                dfs(p, u)

dfs(0)


print(' '.join(map(str, d)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(4000)

n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = [[] for i in range(n)]

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


ans = [0]*n
for v in add_list:
    ans[v[0]-1] += v[1]
#print(add_value)


def dfs(index, parent):
    if parent != -1:
        ans[index] += ans[parent]
    for i in dir_edge[index]:
        if (parent != i):
            dfs(i,index)

dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2000)

n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = [[] for i in range(n)]

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


ans = [0]*n
for v in add_list:
    ans[v[0]-1] += v[1]
#print(add_value)


def dfs(index, parent):
    if parent != -1:
        ans[index] += ans[parent]
    for i in dir_edge[index]:
        if (parent != i):
            dfs(i,index)

dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int,input().split())
tree = [[0] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    if tree[a] == [0]:
        tree[a] = [b]
    else:
        tree[a].append(b)
counter = [0 for i in range(N+1)]
def dfs(p,x):
    if p == 0:
        return
    counter[p] += x
    for i in tree[p]:
        dfs(i,x)   
    
for i in range(Q):
    p,x = map(int,inupt().split())
    dfs(p,x)
print(counter[1:])

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = []
for i in range(n):
    dir_edge.append([])

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


ans = [0]*n
for v in add_list:
    ans[v[0]-1] += v[1]
#print(add_value)


def dfs(index, parent):
    if parent != -1:
        ans[index] += ans[parent]
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index)
        

dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
a = [list(map(int, input().split())) for _ in range(N-1)]
b = [list(map(int, input().split())) for _ in range(M)]

ans=[0]*N
trees = {}
for word in a:
    trees.setdefault(word[0], []).append(word[1])
    trees.setdefault(word[1], []).append(word[0])

for i in b:
    ans[i[0]-1] += i[1]


def sumup(ne,parent):
    global ans
    if ne in trees:    
        for k in trees[ne]:
            if k != parent:
                ans[k-1] += ans[ne-1]
                sumup(k,ne)

sumup(1,0)
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = []
for i in range(n):
    dir_edge.append([])

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


ans = [0]*n
for v in add_list:
    ans[v[0]-1] += v[1]
#print(add_value)


def dfs(index, parent,value):
    if parent != -1:
        ans[index] += ans[parent]
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index,value)
        

dfs(0, -1, 0)
print(*ans)

Submission status: Runtime Error

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

n,q = map(int, input().split())
#長さn個のリストを入れ子にしたリストの作成：adj
adj = [[] for i in range(n)]

#adjに繋がっている要素を上流下流関わらず全て加える。
for j in range(n-1):
    a,b=map(int, input().split())
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)
print(adj) 

#加算する値を合計したリストの作成
queries = [0 for i in range(n)]
for j in range(q):
    p, x = map(int, input().split())
    queries[p-1] += x
print(queries)

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

#aの値，直前の値，直前のaの値
st = [(0, 0, -1)] 

print('answer roop')

#つながってる奴の一番後ろから処理していく。
#０スタート→1,2繋がってるなら2にいく。2に3が繋がってるなら次は3。
#3に繋がってるのがなければ1に戻る。
#この際，aの直前の値をタプルで保持したままリストに挿入して保管しておく。
while st:
    #stの先頭を取り出す。初期値は末尾のため-1
    l = st.pop()  
    print(l)
    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:
n, q = [int(x) for x in input().split()]
vs   = []
xs   = []
	
for _ in range(0, n + 1):
	vs.append([])
	xs.append(0)

for _ in range(0, n - 1):
	a, b = [int(x) for x in input().split()]
	vs[a].append(b)

for _ in range(0, q):
	p, x   = [int(x) for x in input().split()]
	xs[p] += x
	
def calc(v, x, vs, xs):
	xs[v] += x
	for u in vs[v]:
		calc(u, xs[v], vs, xs)

calc(1, 0, vs, xs)

print(" ".join(str(x) for x in xs[1:]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = []
for i in range(n):
    dir_edge.append([])

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


add_value = [0]*n
for v in add_list:
    add_value[v[0]-1] += v[1]
#print(add_value)


ans = [-1] *n
def dfs(index, parent,value):
    value += add_value[index]
    ans[index] = value
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index,value)
        

dfs(0, -1, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = [[] for i in range(n)]

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


add_value = [0]*n
for v in add_list:
    add_value[v[0]-1] += v[1]
#print(add_value)


ans = [-1] *n
def dfs(index, parent,value):
    value += add_value[index]
    ans[index] = value
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index,value)
        

dfs(0, -1, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = [[] for i in range(n)]

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
print(dir_edge)


add_value = [0]*n
for v in add_list:
    add_value[v[0]-1] += v[1]
print(add_value)



def dfs(index, parent,value):
    value += add_value[index]
    dp[index] = value
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index,value)
        

dfs(0, -1, 0)
print(*dp)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

#print(n,q)
edge = []
for i in range(n-1):
    a = list(map(int, input().split()))
    edge.append(a)

add_list = []
for i in range(q):
    p = list(map(int, input().split()))
    add_list.append(p)

#print(edge,add_list)


dir_edge = [[] for i in range(n)]

for e in edge:
    dir_edge[e[0]-1].append(e[1]-1)
    dir_edge[e[1]-1].append(e[0]-1)
#print(dir_edge)


add_value = [0]*n
for v in add_list:
    add_value[v[0]-1] += v[1]
#print(add_value)


dp = [-1] * n

def dfs(index, parent,value):
    if (dp[index] != -1):
        return dp[index]
    value += add_value[index]
    dp[index] = value
    for i in dir_edge[index]:
        if (parent == i):
            continue
        dfs(i,index,value)
        

dfs(0, -1, 0)
print(dp)

Submission status: Runtime Error

Solution code:
class TreeNode:
    def __init__(self):
        self.val = 0
        self.vertex = []


def make_trees(n, A, B):
    trees = []
    for i in range(n):
        trees.append(TreeNode())
    for i in range(len(A)):
        a = A[i]-1
        b = B[i]-1
        trees[a].vertex.append(b)
    return trees

def dfs(node: TreeNode, carry: int, trees):
    if node:
        node.val += carry
        for i in node.vertex:
            dfs(trees[i], node.val, trees)


def sol(n, q, A, B, P, X):
    trees = make_trees(n, A, B)
    for i in range(len(P)):
        p = P[i]-1
        x = X[i]
        t = trees[p]
        # dfs, give point x
        t.val += x
    # accmulative run
    dfs(trees[0], 0, trees)
    #
    return " ".join([str(t.val) for t in trees])
    #return n, q, A, B, P, X



do_submit = True
#do_submit = False

def input_parse(input_str):
    lines = [x.strip() for x in input_str.split("\n") if x.strip()]
    parsed_lines = [list(map(str, line.split())) for line in lines]
    print(parsed_lines)
    n = int(parsed_lines[0][0])
    q = int(parsed_lines[0][1])
    A = []
    B = []
    P = []
    X = []
    for i in range(1, n-1+1):
        a = int(parsed_lines[i][0])
        b = int(parsed_lines[i][1])
        A.append(a)
        B.append(b)
    for i in range(n, 1+n+q-1):
        p = int(parsed_lines[i][0])
        x = int(parsed_lines[i][1])
        P.append(p)
        X.append(x)

    # S = parsed_lines[1][0]
    # return n, k, S
    return n, q, A, B, P, X


if not do_submit:
    n, q, A, B, P, X = input_parse("""
    4 3
    1 2
    2 3
    2 4
    2 10
    1 100
    3 1
    """)
    print(sol(n, q, A, B, P, X))

    n, q, A, B, P, X = input_parse("""
        6 2
        1 2
        1 3
        2 4
        3 6
        2 5
        1 10
        1 10
    """)
    print(sol(n, q, A, B, P, X))

    n, q, A, B, P, X = input_parse("""
        10 5
        1 2
        1 3
        1 4
        2 5
        2 6
        3 7
        4 8
        4 9
        7 10
        7 3
        3 100
        1 2
        5 10
        4 11
    """)
    print(sol(n, q, A, B, P, X))

else:
    # n = int(input().strip())
    # q = int(input().strip())
    n, q = list(map(int, input().split()))
    A = []
    B = []
    P = []
    X = []
    for i in range(n-1):
        a, b = list(map(int, input().split()))
        # a = int(input().strip())
        # b = int(input().strip())
        A.append(a)
        B.append(b)
    for i in range(q):
        p, x = list(map(int, input().split()))
        # p = int(input().strip())
        # x = int(input().strip())
        P.append(p)
        X.append(x)
    print(sol(n, q, A, B, P, X))

Submission status: Runtime Error

Solution code:
n, q = [int(x) for x in input().split()]
ns   = []
vs   = []
ps   = []
xs   = []
	
for _ in range(0, n + 1):
	ns.append([])
	vs.append(0)

for _ in range(0, n - 1):
	a, b = [int(x) for x in input().split()]
	ns[a].append(b)
	
def calc(i, x, ns, vs):
	vs[i] += x
	for v in ns[i]:
		calc(v, x, ns, vs)

for _ in range(0, q):
	p, x = [int(x) for x in input().split()]
	calc(p, x, ns, vs)

print(" ".join(str(x) for x in vs[1:]))

Submission status: Runtime Error

Solution code:
N, Q = [int(i) for i in input().split()]
edges = [None for _ in range(N + 1)]
result = [0] * (N + 1)

for _ in range(N - 1):
    a, b = [int(i) for i in input().split()]
    edges[b] = a
    
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    result[p] += x

for i in range(2, N + 1):
    result[i] += result[edges[i]]

for r in result[1:N]:
    print("%d " % r, end="")

print(result[N])

Submission status: Runtime Error

Solution code:
N, Q = [int(i) for i in input().split()]
edges = [None for _ in range(N + 1)]
result = [0] * (N + 1)

for _ in range(N - 1):
    a, b = [int(i) for i in input().split()]
    edges[b] = a
    
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    result[p] += x

for i in range(2, N + 1):
    result[i] += result[edge[i]]

for r in result[1:N]:
    print("%d " % r, end="")

print(result[N])

Submission status: Runtime Error

Solution code:
MAX = 200005
G = [[] for i in range(MAX)]
adds = [0] * MAX
ans = [0] * MAX

def dfs(c, p, count):
    count += adds[c]
    ans[c] += count
    for i in range(len(G[c])):
        if (G[c][i] != p):
            dfs(G[c][i], c, count)

n, q = map(int, input().split())
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
               
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    adds[p] += x

dfs(0, -1, 0)
for i in range(n):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
cnt = [0] * (n+1)
import sys
sys.setrecursionlimit(10**6)
import numpy as np
tree = np.zeros((n+1, n+1))
child = [0] * (n+1)
for i in range(n-1):
    a, b = map(int,input().split())
    tree[a][b] = 1
    tree[b][a] = 1
def dfs(i):
    global child
    child[i] = []
    if np.all(tree[i] == 0):
        return
    for j in range(n+1):
        if tree[i][j] == 1:
            tree[j][i] = 0
            child[i].append(j)
            dfs(j)
    return

dfs(1)
#print(child)
for _ in range(q):
    p,x = map(int,input().split())
    cnt[p] += x
def plus(c):
    global cnt
    if child[c] == []:
        return
    for target in child[c]:
        cnt[target] += cnt[c]
        plus(target)
    return
plus(1)
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
cnt = [0] * (n+1)
import sys
sys.setrecursionlimit(10**9)
import numpy as np
tree = np.zeros((n+1, n+1))
child = [0] * (n+1)
for i in range(n-1):
    a, b = map(int,input().split())
    tree[a][b] = 1
    tree[b][a] = 1
def dfs(i):
    global child
    child[i] = []
    if np.all(tree[i]):
        return
    for j in range(n+1):
        if tree[i][j] == 1:
            tree[j][i] = 0
            child[i].append(j)
            dfs(j)
    return

dfs(1)
#print(child)
for _ in range(q):
    p,x = map(int,input().split())
    cnt[p] += x
def plus(c):
    global cnt
    if child[c] == []:
        return
    for target in child[c]:
        cnt[target] += cnt[c]
        plus(target)
    return
plus(1)
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys

sys.setrecursionlimit(20000)

def forward(matrix, plus, pos, counter):
    for i in range(pos+1,N):
        val = matrix[pos,i]
        if val == 1:
            counter[i] += plus
            save = pos
            forward(matrix, plus, i, counter)

if __name__ == '__main__':
    N, Q = map(int, input().split())

    matrix = np.zeros((N,N))
    counter = []
    for i in range(N):
        counter.append(0)

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

    P, X = [], []
    for i in range(Q):
        p, x = map(int, input().split())
        P.append(p-1)
        X.append(x)

    for p, x in zip(P,X):
        counter[p] += x
        forward(matrix, x, p, counter)

    out = str(counter[0])
    for i in range(len(counter)-1):
        out = out + ' ' + str(counter[i+1])

    print(out)

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
cnt = [0] * (n+1)
import sys
sys.setrecursionlimit(10**6)
import numpy as np
tree = np.zeros((n+1, n+1))
child = [0] * (n+1)
for i in range(n-1):
    a, b = map(int,input().split())
    tree[a][b] = 1
    tree[b][a] = 1
def dfs(i):
    global child
    child[i] = []
    if np.all(tree[i]):
        return
    for j in range(n+1):
        if tree[i][j] == 1:
            tree[j][i] = 0
            child[i].append(j)
            dfs(j)
    return

dfs(1)
#print(child)
for _ in range(q):
    p,x = map(int,input().split())
    cnt[p] += x
def plus(c):
    global cnt
    if child[c] == []:
        return
    for target in child[c]:
        cnt[target] += cnt[c]
        plus(target)
    return
plus(1)
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
cnt = [0] * (n+1)
import sys
sys.setrecursionlimit(10**6)
import numpy as np
tree = np.zeros((n+1, n+1))
child = [0] * (n+1)
for i in range(n-1):
    a, b = map(int,input().split())
    tree[a][b] = 1
    tree[b][a] = 1
def dfs(i):
    global child
    child[i] = []
    if np.all(tree[i]):
        return
    for j in range(n+1):
        if tree[i][j] == 1:
            tree[j][i] = 0
            child[i].append(j)
            dfs(j)
    return

dfs(1)
print(child)
for _ in range(q):
    p,x = map(int,input().split())
    cnt[p] += x
def plus(c):
    global cnt
    if child[c] == []:
        return
    for target in child[c]:
        cnt[target] += cnt[c]
        plus(target)
    return
plus(1)
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
br = []
for i in range(n-1):
  a,b=map(int,input().split())
  br.append([a,b])
br.sort()
tree=[0 for i in range(n)]
for i in range(q):
  p,x=map(int,input().split())
  tree[p-1]+=x
for i in range(len(br)):
  tree[br[i][1]-1]+=br[[i][0]-1]
print(tree)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
br = []
for i in range(n-1):
  a,b=map(int,input().split())
  br.append([a,b])
br.sort()
tree=[0 for i in range(n)]
for i in range(q):
  p,x=map(int,input().split())
  tree[p-1]+=x
for i in range(n-1):
  tree[br[i][1]-1]+=br[[i][0]-1]
print(tree)