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())
ans=[0]*(n+1)
score=[0]*(n+1)
g=[[] for _ in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
for j in range(q):
    p,x=map(int,input().split())
    score[p]+=x
visited=set()
visited.add(1)
def dfs(v):
    ans[v]+=score[v]
    for i in range(len(g[v])):
        if not g[v][i] in visited:
            ans[g[v][i]]+=ans[v]
            dfs(g[v][i])
dfs(1)
ans_chr=""
for i in range(1,n+1):
    ans_chr=ans_chr+str(ans[i])+" "
print(ans_chr)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200001)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)] # paths[i] : 頂点iと繋がってる点を全て表す
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

ans = [0]*(N+1) # ans[i] : 頂点iについて答える値を格納

counters = [0]*(N+1) # そのノードの部分木に与える点数の総和を格納
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1) #dfsをする時に一度訪れた点に再訪しないための確認
def dfs(node, score, ans):
    visit[node] = 1 # 訪問したことを記録
    score += counters[node] # 自分の部分木に加点する分を加えて後世に残す
    ans[node] = score # 点数の記録
    for loof in paths[node]: # 自分と繋がってる各ノードについて
        if visit[loof] == 0: # まだ探索していなければ
            dfs(loof, score, ans) # 探索

dfs(1, 0, ans)
    

for i in range(1, N+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import sys
# sys.setrecursionlimit(100000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
    paths[b].append(a)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)
# childs = [list() for _ in range(N+1)]

# def dfs(root, node, arr, check):
#     check[node] = 1
#     arr[root].append(node)
#     for loof in paths[node]:
#         if check[loof] == 0:
#             dfs(root, loof, arr, check)

# for i in range(1, N+1):
#     visit = [0]*(N+1)
#     dfs(i, i, childs, visit)


# for op in px:
#     for child in childs[op[0]]:
#         nodes[child] += op[1]

# for i in range(1, N+1):
#     print(nodes[i])

counters = [0]*(N+1)
for x in px:
    counters[x[0]] += x[1]

visit = [0]*(N+1)
def dfs(node, score, nodes):
    visit[node] = 1
    score += counters[node]
    nodes[node] = score
    for loof in paths[node]:
        if visit[loof] == 0:
            dfs(loof, score, nodes)

dfs(1, 0, nodes)
    

for i in range(1, N+1):
    print(nodes[i])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
N, Q = map(int, input().split())
paths = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    paths[a].append(b)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)
childs = [list() for _ in range(N+1)]

def dfs(root, node, arr):
    arr[root].append(node)
    for loof in paths[node]:
        dfs(root, loof, arr)

for i in range(1, N+1):
    dfs(i, i, childs)


for op in px:
    for child in childs[op[0]]:
        nodes[child] += op[1]

for i in range(1, N+1):
    print(nodes[i])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
path = [list() for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    path[a].append(b)
# ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

nodes = [0]*(N+1)

def dfs(p, x, arr):
    arr[p] += x
    for loof in path[p]:
        dfs(loof, x, arr)

for op in px:
    dfs(op[0], op[1], nodes)

for i in range(1, N+1):
    print(nodes[i])

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
def helper(s,cost):
    if not seen[s]:
        seen[s] = True
        ans[s] += cost
        for e in g[s]:
            if not seen[e]:
                helper(e,cost+d[e])
for i in range(1,n+1):
    helper(i,d[i])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
g = [[] for _ in range(n+1)]
d = [0]*(n+1)
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a].append(b)
for _ in range(q):
    p,x = map(int,input().split())
    d[p] += x
ans = [0]*(n+1)
seen = [False]*(n+1)
def helper(s,cost):
    if not seen[s]:
        seen[s] = True
        ans[s] += cost
        for e in g[s]:
            if not seen[e]:
                helper(e,cost+d[e])
helper(1,d[1])
res = []
for a in ans[1:]:
    res.append(str(a))

print(' '.join(res))

Submission status: Runtime Error

Solution code:
import copy
import math
import time
import statistics
import math
import itertools
import bisect
from decimal import *

# a = get_int()
def get_int():
    return int(input())
# a = get_string()
def get_string():
    return input()
# a_list = get_int_list()
def get_int_list():
    return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
    return input().split()
# a, b = get_int_multi()
def get_int_multi():
    return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
    return list(str(input()))
# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():

    idx += 1
'''

def main():

    start = time.time()

    n, q = get_int_multi()

    a = [[] for _ in range(n+1)]
    for _ in range(n-1):
        s, e = get_int_multi()
        a[s].append(e)
        a[e].append(s)

    point = [0] * (n+1)
    for _ in range(q):
        p, x = get_int_multi()
        point[p] += x

    def tasu(now, prev):
        for next in a[now]:
            if next == prev:
                continue
            point[next] += point[now]
            tasu(next, now)

    tasu(1, -1)
    point = point[1:] #１つ目は0なので除去（inputは1始まりなので、0は使わない）
    print(*point) # * は引数展開　listの中身を引数にする。例えばprint(*[1,2,3])はprint(1,2,3)になり 1 2 3 が出力される


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
#累積和を木に拡張する
N, Q = map(int, input().split())

graph = [[] for i in range(N)]
score = [0 for _ in range(N)]

def dfs(now, prev):

    for adj in graph[now]:
        if adj == prev:
            continue
        score[adj] += score[now]
        dfs(adj, now)

    return

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

query = [0] * N
for i in range(Q):
    p, x = map(int, input().split())

    score[p-1] += x

dfs(0, -1)

for p in score:
    print(p)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
from collections import deque
g = [[] for _ in range(n)]
fi = [0]*n
bt = [0]*n
for _ in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)
ls = [list(map(int,input().split())) for _ in range(q)]
def bfs(u):
    q = deque([u])
    d = [None]*n
    d[u] = 0
    while q:
        v = q.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                q.append(i)
    return d
ans = 0
bt[0] = bfs(0)
for j in range(q):
    if bt[ls[j][0]-1] == []:
        bt[ls[j][0]-1] = bfs(ls[j][0]-1)
    di = list(map(lambda x,y:x-y,bt[0],bt[ls[j][0]-1]))
    for k in range(len(di)):
        if di[k] == bfs(0)[ls[j][0]-1]:
            fi[k] += ls[j][1]                           
print(*fi)

Submission status: Runtime Error

Solution code:
import math
import sys

sys.setrecursionlimit(10000)

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(200005)]
ans = [0 for _ in range(n + 1)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans[:n]:
    print(a)

Submission status: Runtime Error

Solution code:
import math

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(200005)]
ans = [0 for _ in range(n + 1)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans[:n]:
    print(a)

Submission status: Runtime Error

Solution code:
import math

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(200005)]
ans = [0 for _ in range(n)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import math

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(2 * 10**6)]
ans = [0 for _ in range(n)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import math

n, q = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(n - 1)]
pxL = [list(map(int, input().split(" "))) for _ in range(q)]

to = [[] for _ in range(n)]
ans = [0 for _ in range(n)]


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


for a, b in abL:
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for p, x in pxL:
    p -= 1
    ans[p] += x

dfs(0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())

graph = [[] for i in range(N)]

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

def dfs(now, prev, addVal, visited):
    if graph[now] == prev:
        score[now]+= addVal
        return

    score[now] += addVal

    for adj in graph[now]:
        if adj < now:
            continue
        if adj == prev:
            continue
        if visited[adj]:
            continue
        visited[adj] = True

        dfs(adj, now, addVal, visited)


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

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

    visited = [False for _ in range(N)]
    dfs(p-1, -1, x, visited)

for i in range(N):
    print(score[i])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

w = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    w[A].append(B)
    w[B].append(A)

cnt = [0]*(N+1)
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P] += X

flag = [False]*(N+1)
cnt_sum = [0]*(N+1)

def f(i, n):
    if flag[i]:
        return
    flag[i] = True
    cnt_sum[i] = n+cnt[i]
    for t in w[i]:
        f(t, cnt_sum[i])

f(1, 0)

print(" ".join([str(c) for c in cnt_sum[1:]]))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

w = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    w[A].append(B)

cnt = [0]*(N+1)
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P] += X

cnt_sum = [0]*(N+1)

def f(i, n):
    cnt_sum[i] = n+cnt[i]
    for t in w[i]:
        f(t, cnt_sum[i])
    return

f(1, 0)

print(" ".join([str(c) for c in cnt_sum[1:]]))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

w = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    w[A].append(B)

cnt = [0]*(N+1)
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P] += X

cnt_sum = [0]*(N+1)

def f(i, n):
    cnt_sum[i] = n+cnt[i]
    for t in w[i]:
        f(t, cnt_sum[i])

f(1, 0)

print(" ".join([str(c) for c in cnt_sum[1:]]))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

w = [[] for _ in range(N+1)]
for _ in range(N-1):
    A, B = map(int, input().split())
    w[A].append(B)

cnt = [0]*(N+1)
for _ in range(Q):
    P, X = map(int, input().split())
    cnt[P] += X

cnt_sum = [0]*(N+1)

def f(i, n):
    cnt_sum[i] = n+cnt[i]
    for t in w[i]:
        f(t, n+cnt[i])

f(1, 0)

print(" ".join([str(c) for c in cnt_sum[1:]]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

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

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

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

def dfs(v):
    for i in g[v]:
        cnt[i] += cnt[v]
        dfs(i)

dfs(0)
print(*cnt)

Submission status: Runtime Error

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

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

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

def dfs(v):
    for i in g[v]:
        cnt[i] += cnt[v]
        dfs(i)

dfs(0)
print(*cnt)

Submission status: Runtime Error

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

road = [[] for _ in range(n)]
cnt = [0 for _ in range(n)]

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

for i in range(q):
    idx, v = map(int, input().split())
    cnt[idx - 1] += v

dist = [-1] * n
que = deque()
dist[0] = 0
que.append(0)

while(len(que) != 0):
    # BFS/DFS
    v = que.pop()
    for r in road[v]:
        if dist[r] != -1:
            continue
        # print(r, v)
        dist[r] = dist[v] + 1
        cnt[r] += cnt[v]
        que.append(r)
print(*cnt)

Submission status: Runtime Error

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

road = [[] for _ in range(n)]
cnt = [0 for _ in range(n)]

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

for i in range(q):
    idx, v = map(int, input().split())
    cnt[idx - 1] += v

dist = [-1] * n
que = deque()
dist[0] = 0
que.append(0)

while(len(que) != 0):
    # BFS/DFS
    v = que.pop()
    for r in road[v]:
        if dist[r] != -1:
            continue
        # print(r, v)
        dist[r] = dist[v] + 1
        print(cnt)
        cnt[r] += cnt[v]
        que.append(r)
print(*cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())
G = [[] * 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)

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

visited = [False] * N
que = deque([0])

while que:
    now = que.pop()
    visited[now] = True
    for nxt in G[now]:
        if visited[nxt]:
            continue
        count[nxt] += count[now]
        que.appendleft(nxt)

print(*count)

Submission status: Runtime Error

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

road = [[] for _ in range(n)]
cnt  = [0 for _ in range(n)]

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

for i in range(q):
    idx,v = map(int,input().split())
    cnt[idx-1] = v

dist = [-1]*n
que  = deque()
dist[0] = 0
que.append(0)

while(len(que)!=0):
    # BFS/DFS
    v = que.pop()
    for r in road[v]:
        if dist[r]!=-1:
            continue
        dist[r] = dist[v] + 1
        cnt[r] += cnt[v]
        que.append(r)

print(*cnt)

Submission status: Runtime Error

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

def dfs(now, prev = -1):
    for nx in connect[now]:
        if nx == prev: continue
        score[nx] += score[now]
        dfs(nx, now)

dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
connect = {i:set() for i in range(1, N+1)}
for _ in range(N-1):
    a, b = map(int, input().split())
    connect[a].add(b)
    connect[b].add(a)
score = {i:0 for i in range(1, N+1)}
for _ in range(Q):
    p, x = map(int, input().split())
    score[p] += x

def dfs(now, prev = -1):
    for nx in connect[now]:
        if nx == prev: continue
        score[nx] += score[now]
        dfs(nx, now)

dfs(1)
print(*score.values())

Submission status: Runtime Error

Solution code:
N,Q = (int(x) for x in input().split())
connection ={i:set() for i in range(1,N+1)}
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b)
    connection[b].add(a)
#point =OrderedDict({i:0 for i in range(1,N+1)})
point ={i:0 for i in range(1,N+1)}
for i in range(Q):
    p,x = (int(x) for x in input().split())
    point[p] +=x
 
def DFS(now,prev = -1):
    for next in connection[now]:
        if next == prev:
            continue
        point[next] +=point[now] 
        DFS(next,now)
 
DFS(1)
print(*point.values())

Submission status: Runtime Error

Solution code:
def dfs(now, prev = -1):
    for nx in connect[now]:
        if nx == prev: continue
        score[nx] += score[now]
        dfs(nx, now)

N, Q = map(int, input().split())
connect = {i:set() for i in range(1, N+1)}
for _ in range(N-1):
    a, b = map(int, input().split())
    connect[a].add(b)
    connect[b].add(a)
score = {i:0 for i in range(1, N+1)}
for _ in range(Q):
    p, x = map(int, input().split())
    score[p] += x

dfs(1)
print(*score.values())

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self):
        self.N, self.Q = map(int, input().split())
        self.list = [0 for i in range(self.N+1)]
        self.under = [None for i in range(self.N+1)]
        self.count = [0 for i in range(self.N+1)]
        for i in range(self.N-1):
            a, b = map(int, input().split())
            self.list[b] = a
        self.__count__()
        for i in range(1, self.N+1):
            self.__count_under__(i)
        self.__print__()
    def __count__(self):
        for i in range(self.Q):
            p, x = map(int, input().split())
            self.count[p] += x
    def __count_under__(self, n):
        if n == 1:
            self.under[1] = self.count[1]
        else:
            self.under[n] = self.count[n] + self.under[self.list[n]]
    def __print__(self):
        for i in range(1, self.N+1):
            print(self.under[i])
g = Node()

Submission status: Runtime Error

Solution code:
def dfs(u, cnt):
    cnt += point[u]
    counter[u] = cnt
    color[u] = GRAY
    for v in M[u]:
        if color[v] == WHITE:
            dfs(v, cnt)
    color[u] = BLACK

import sys
sys.setrecursionlimit(100000)

n, q = map(int, input().split())
M = [[] for _ in range(n)]
point = [0] * n
for i in range(n-1):
    a, b = map(int, input().split())
    M[a-1].append(b-1)
    M[b-1].append(a-1)
for i in range(q):
    p, x = map(int, input().split())
    point[p-1] += x
#print(M, point)
    
WHITE, GRAY, BLACK = 0, 1, 2
color = [WHITE] * n

counter = [0] * n
dfs(0, 0)

print(*counter)

Submission status: Runtime Error

Solution code:
def dfs(u, cnt):
    cnt += point[u]
    counter[u] = cnt
    color[u] = GRAY
    for v in M[u]:
        if color[v] == WHITE:
            dfs(v, cnt)
    color[u] = BLACK


n, q = map(int, input().split())
M = [[] for _ in range(n)]
point = [0] * n
for i in range(n-1):
    a, b = map(int, input().split())
    M[a-1].append(b-1)
    M[b-1].append(a-1)
for i in range(q):
    p, x = map(int, input().split())
    point[p-1] += x
#print(M, point)
    
WHITE, GRAY, BLACK = 0, 1, 2
color = [WHITE] * n

counter = [0] * n
dfs(0, 0)

print(*counter)

Submission status: Runtime Error

Solution code:
def dfs(u, cnt):
    cnt += point[u]
    counter[u] = cnt
    color[u] = GRAY
    for v in M[u]:
        if color[v] == WHITE:
            dfs(v, cnt)
    color[u] = BLACK


n, q = map(int, input().split())
M = [[] for _ in range(n)]
point = [0] * n
for i in range(n-1):
    a, b = map(int, input().split())
    M[a-1].append(b-1)
for i in range(q):
    p, x = map(int, input().split())
    point[p-1] += x
#print(M, point)
    
WHITE, GRAY, BLACK = 0, 1, 2
color = [WHITE] * n

counter = [0] * n
dfs(0, 0)

print(*counter)

Submission status: Runtime Error

Solution code:
from collections import deque

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)

c = [0] * n

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

used = [0] * n
used[0] = 1

def dfs(x):
    cost=c[x]
    for nx in g[x]:
        if used[nx]:
            continue
        used[nx]=1
        c[nx]+=cost
        dfs(nx)
dfs(0)
print(*c)

Submission status: Runtime Error

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

C=[0]*n

def dfs(p,x):
    C[p]+=x
    for i in X[p]:
        dfs(i,x)


for i in range(q):
    p,x=map(int,input().split())
    dfs(p-1,x)

print(*C)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
ab=[[_] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    ab[max(a,b)-1].append(min(a,b))
ab[0].append(-1)
ans=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    ans[p-1]+=x
ab.sort(key=lambda x: x[1])
for i in range(1,n):
    for j in range(1,len(ab[i])):
        ans[ab[i][0]]+=ans[ab[i][j]-1]
print(*ans)

Submission status: Runtime Error

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

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

#kが木の頂点、vがそこからきた頂点
bit = [0 for _ in range(n)]
def dfs(k):
    bit[k] = 1
    for i in l[k]:
        if bit[i]==0:
            value[i] += value[k]
            dfs(i)

dfs(0)
print(*value)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit = 2 * 10 ** 6
 
n, q = map(int, input().split())
 
connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)
 
counter = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    counter[p-1] += x
 
ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)
 
dfs(0, -1, 0)
 
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit = 2 * 10 ** 6

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

connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit = 2 * 10 ** 5

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

connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Submission status: Runtime Error

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

connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

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

ans = [0] * n
def dfs(me, parent, cum):
    cum += counter[me]
    ans[me] = cum
    for child in connect[me]:
        if child != parent:
            dfs(child, me, cum)

dfs(0, -1, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[29]:


def func():
    N,Q = map(int, input().split())
    ab = []
    nodes = {}
    for _ in range(N-1):
        ab.append(list(map(int, input().split())))
    ab = sorted(ab,key=lambda x:x[0])
    graph = {}
    for a,b in ab:
        if a not in graph:
            graph[a] = [b]
        else:
            graph[a].append(b)
    cnt = [0]*N
    for j in range(Q):
        p,x = map(int, input().split())
        calc(graph,p,cnt,x)
    print(*cnt)


# In[30]:


def calc(graph,node,cnt,x):
    cnt[node-1] += x
    if node in graph:
        for n in graph[node]:
            calc(graph,n,cnt,x)
    return cnt


# In[31]:


if __name__ == "__main__":
    func()


# In[ ]:

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[23]:


def func():
    N,Q = map(int, input().split())
    ab = []
    nodes = {}
    for _ in range(N-1):
        ab.append(list(map(int, input().split())))
    ab = sorted(ab,key=lambda x:x[0])
    graph = {}
    for a,b in ab:
        if a not in graph:
            graph[a] = [b]
        else:
            graph[a].append(b)
    cnt = [0]*N
    for j in range(Q):
        p,x = map(int, input().split())
        calc(graph,p,cnt,x)
    print(cnt)


# In[26]:


def calc(graph,node,cnt,x):
    cnt[node-1] += x
    if node in graph:
        for n in graph[node]:
            calc(graph,n,cnt,x)
    return cnt


# In[28]:


if __name__ == "__main__":
    func()


# In[ ]:

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())
  a-=1;b-=1 #頂点を0indexに
  G[a].append(b);G[b].append(a)
query = {}
for i in range(Q):
  p,x = map(int,input().split())
  p-=1
  if p not in query:
    query[p] = x
  else:
    query[p] += x
ans = [0 for _ in range(N)]
def dfs(v,par): #vが今いる頂点、parが親。
  if v in query:
    ans[v] += query[v]
  for u in G[v]:
    if u != par:
      ans[u] += ans[v]
      dfs(u,v)
dfs(0,-1)
print(*ans)

Submission status: Runtime Error

Solution code:
class Tree:
    def __init__(self, n):
        self.n = n
        self.node = [0] * self.n
        self.subtree = [set() for i in range(self.n)]

    def build(self, edges):
        self.child = {i: set() for i in range(self.n)}

        for t in edges:
            self.child[t[0]-1].add(t[1]-1)

    def add(self, p, x):
        self.node[p-1] += x

    def ans(self, i):
        a = self.node[i]
        for k in self.child[i]:
            self.node[k] += a
            self.ans(k)

n, q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(n-1)]

tree = Tree(n)
tree.build(edges)

for i in range(q):
    p, x = map(int, input().split())
    tree.add(p, x)


tree.ans(0)
print(" ".join(map(str, tree.node)))