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,M=list(map(int,input().split()))
nodes=[None]*(N+1)
class tree():
    def __init__(self,i):
        self.node=i
        self.left=None
        self.right=None
for i in range(N-1):
    a,b=list(map(int,input().split()))
    if nodes[b] is None:
        ko=tree(b)
    else:ko=nodes[b]
    if nodes[a] is None:
        oya=tree(a)
        oya.left=ko
        nodes[oya.node] = oya
        nodes[b] = ko
    else:
        oya=nodes[a]
        if oya.left is not None:
            oya=oya.left
            while(oya.right!=None):
                oya=oya.right
            oya.right=ko
        else:
            oya.left=ko
        nodes[oya.node]=oya
        nodes[b]=ko
oya=nodes[1]
def bfs(n,ten):
    if n.left != None:
        n = n.left
        score[n.node] += ten
        k=n
        while(k.right!=None):
            k=k.right
            score[k.node] += ten
            bfs(k,ten)
        bfs(n, ten)

score=[0]*(N+1)
for i in range(M):
    n,ten=list(map(int,input().split()))
    score[n]+=ten
    n=nodes[n]
    bfs(n,ten)
print(*score[1:])
n=nodes[3]

Submission status: Runtime Error

Solution code:
# ABC138D

import sys
sys.setrecursionlimit(1000000000000)
def dfs(kol,adj,v,p):
    for nv in adj[v]:
        if nv==p:
            continue
        else:
            dfs(kol,adj,nv,v)
    kol[v]+=[v]
    if p!=-1:
        kol[p]+=kol[v]
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]+=[b-1]
    adj[b-1]+=[a-1]
    
ct=[0]*N
kol=[[] for i in range(N)]

dfs(kol,adj,0,-1)

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

print(ct[0], end="")
for i in range(1,N):
    print(" {}".format(ct[i]),end="")
print("")

Submission status: Runtime Error

Solution code:
# ABC138D
def dfs(kol,adj,v,p):
    for nv in adj[v]:
        if nv==p:
            continue
        else:
            dfs(kol,adj,nv,v)
    kol[v]+=[v]
    if p!=-1:
        kol[p]+=kol[v]
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]+=[b-1]
    adj[b-1]+=[a-1]
    
ct=[0]*N
kol=[[] for i in range(N)]

dfs(kol,adj,0,-1)

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

print(ct[0], end="")
for i in range(1,N):
    print(" {}".format(ct[i]),end="")
print("")

Submission status: Runtime Error

Solution code:
def dfs(v):
    seen[v] = 1
    ans[v] += counter[v]

    for next_v in graph[v]:
        if seen[next_v]: continue
        ans[next_v] += ans[v]
        dfs(next_v)

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)

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

seen = [0] * n
ans = [0] * n
dfs(0)

print(*ans)

Submission status: Runtime Error

Solution code:
# 早くなるおまじない
import sys
input = sys.stdin.buffer.readline

# 再帰関数の呼び出し制限
sys.setrecursionlimit(3 * 10 ** 5)

# 深さが一つ前のやつのresの値を足す
# dfsはおk，ここで累積和を使う発想が出てこなかった
def dfs(node, prev):
    visited.add(node)
    if prev != 0:
        res[node-1] += res[prev-1]
    for i in dic[node]:
        if i not in visited:
            prev = node
            dfs(i, prev)


def main():
    n,q = map(int,input().split())

    dic = {}
    for i in range(n-1):
        aa, bb = map(int,input().split())

        if aa not in dic:
            dic[aa] = set()
            dic[aa].add(bb)
        else:
            dic[aa].add(bb)

        if bb not in dic:
            dic[bb] = set()
            dic[bb].add(aa)
        else:
            dic[bb].add(aa)


    p = []
    x = []
    for _ in range(q):
        s, t = map(int,input().split())
        p.append(s)
        x.append(t)

    res = [0]*n
    for i in range(q):
        res[p[i]-1] += x[i]

    visited = set()

    dfs(1, 0)
    print(*res, sep=" ")

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

sys.setrecursionlimit(1000000)

def go_down_tree(tree, current, current_score, scores):
    current_score = current_score + scores[current]
    children = tree[current]
    for child in children:
        for c, s in go_down_tree(tree, child, current_score, scores):
            yield c, s
    yield current, current_score

def main():
    #N, M = map(int, input().split())
    #N, K = map(int, input().split())

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

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

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

    counter = {}
    current = 1
    for current, count in go_down_tree(tree, current, 0, scores):
        counter[current] = count
    print(" ".join(map(str, [counter[i+1] for i in range(N)])))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import sys
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

sys.setrecursionlimit(100000)

def go_down_tree(tree, current, current_score, scores, counter):
    current_score = current_score + scores[current]
    counter[current] = current_score
    children = tree[current]
    for child in children:
        go_down_tree(tree, child, current_score, scores, counter)

def main():
    #N, M = map(int, input().split())
    #N, K = map(int, input().split())

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

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

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

    counter = defaultdict(int)
    current = 1
    go_down_tree(tree, current, 0, scores, counter)
    print(" ".join(map(str, [counter[i+1] for i in range(N)])))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor


def go_down_tree(tree, current, current_score, scores, counter):
    current_score = current_score + scores[current]
    counter[current] = current_score
    children = tree[current]
    for child in children:
        go_down_tree(tree, child, current_score, scores, counter)

def main():
    #N, M = map(int, input().split())
    #N, K = map(int, input().split())

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

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

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

    counter = defaultdict(int)
    current = 1
    go_down_tree(tree, current, 0, scores, counter)
    print(" ".join(map(str, [counter[i+1] for i in range(N)])))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
cnt=[0]*n
used=[0]*n
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
def f(now):
    for to in  edges[now]:
        if used[to]:continue
        cnt[to]+=cnt[now]
        used[to]=1
        f(to)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

used[0]=1
f(0)
print(*cnt)

Submission status: Runtime Error

Solution code:
import collections

n, q = map(int, input().split())
edge = collections.deque([tuple(map(int, input().split())) for _ in range(n - 1)])
query = [tuple(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n)]
flag = [False] * n
flag[0] = True
while len(edge) != 0:
    a, b = edge.popleft()
    a, b = a - 1, b - 1
    if flag[a] or flag[b]:
        edge.append((a, b))
        continue
    if flag[a]:
        tree[a].append(b)
    else:
        tree[b].append(b)

p = [0] * n
for x, y in query:
    p[x - 1] += y
counter = [0] * n

que = collections.deque([0])
while len(que) != 0:
    i = que.popleft()
    counter[i] += p[i]
    for j in tree[i]:
        counter[j] = counter[i]
        que.append(j)

print(' '.join([str(c) for c in counter]))

Submission status: Runtime Error

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

n, q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
query = [tuple(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n)]
for a, b in edge:
    tree[a - 1].append(b - 1)
p = [0] * n
for x, y in query:
    p[x - 1] += y

counter = [0] * n

def solve(i=0, c=0):
    c += p[i]
    counter[i] = c
    for j in tree[i]:
        solve(j, c)
    return

solve()
for c in counter:
    print(c, end=' ')

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
query = [tuple(map(int, input().split())) for _ in range(q)]

tree = [[] for _ in range(n)]
for a, b in edge:
    tree[a - 1].append(b - 1)
p = [0] * n
for x, y in query:
    p[x - 1] += y

counter = [0] * n

def solve(i=0, c=0):
    c += p[i]
    counter[i] = c
    for j in tree[i]:
        solve(j, c)
    return

solve()
for c in counter:
    print(c, end=' ')
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)
n , q = map(int, input().split())
r = [[]for i in range(n)]
s = [0]*n
v = [0]*n
for i in range(n-1):
    a , b = map(int, input().split())
    r[a-1].append(b-1)
    r[b-1].append(a-1)
for i in range(q):
    p , x = map(int, input().split())
    s[p-1]+=x

def dfs(now):
    v[now]=1
    for i in r[now]:
        if v[i]==0:
            s[i]+=s[now]
            dfs(i)
    return
dfs(0)
print(' '.join(list(map(str,s))))

Submission status: Runtime Error

Solution code:
n , q = map(int, input().split())
r = [[]for i in range(n)]
s = [0]*n
v = [0]*n
for i in range(n-1):
    a , b = map(int, input().split())
    r[a-1].append(b-1)
    r[b-1].append(a-1)
for i in range(q):
    p , x = map(int, input().split())
    s[p-1]+=x

def dfs(now):
    v[now]=1
    for i in r[now]:
        if v[i]==0:
            s[i]+=s[now]
            dfs(i)
    return
dfs(0)
print(' '.join(list(map(str,s))))

Submission status: Runtime Error

Solution code:
n , q = map(int, input().split())
r = [[]for i in range(n)]
s = [0]*n
v = [0]*n
for i in range(n-1):
    a , b = map(int, input().split())
    r[a-1].append(b-1)
    r[b-1].append(a-1)
for i in range(q):
    p , x = map(int, input().split())
    s[p-1]+=x

def dfs(now):
    v[now]=1
    for i in r[now]:
        if v[i]==0:
            s[i]+=s[now]
            dfs(i)
dfs(0)
print(' '.join(list(map(str,s))))

Submission status: Runtime Error

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

# dfsをします。
def dfs(x, parent):
    point[x] += point[parent]
    for i in range(len(graph[x])):
        if graph[x][i] == parent:
            continue
        dfs(graph[x][i], x)
dfs(1, 0)
point_str = [str(n) for n in point]
ans = ' '.join(point_str[1:])
print(ans)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
adj = [ [] for _ in range(N) ]
for _ in range(N-1):
  u,v = map(int,input().split())
  u -= 1
  v -= 1
  adj[u].append(v)
scores = [0] * N
for _ in range(Q):
  u,p = map(int,input().split())
  u -= 1
  scores[u] += p

res = [0] * N
def dfs(n, p, s):
  res[n] = s
  for nei in adj[n]:
    if nei != p:
      dfs(nei, n, s+scores[nei])

dfs(0,-1,scores[0])

print(" ".join(map(str, res)))

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
def main():
    N, Q = map(int,input().split())
    G = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int,input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    V = [0]*N
    for _ in range(Q):
        p, x = map(int,input().split())
        V[p-1] += x

    def dfs(mae,ima):
        for tsugi in G[ima]:
            if tsugi != mae:
                V[tsugi] += V[ima]
                dfs(ima,tsugi)
    dfs(-1,0)
    print(*V)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
input() = sys.stdin.readline()
sys.setrecursionlimit(500000)

n, q = map(int, input().split())
res = [0] * n
edge = [[] for _ in res]

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

def dfs(v, p):
    for j in edge[v]:
        if j != p:  # 親は弾く
            res[j] += res[v]
            dfs(j, v)

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

dfs(0, -1)

print(*res)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n, q = map(int, input().split())
res = [0] * n
edge = [[] for _ in res]

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

def dfs(v, p):
    for j in edge[v]:
        if j != p:  # 親は弾く
            res[j] += res[v]
            dfs(j, v)

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

dfs(0, -1)

print(*res)

Submission status: Runtime Error

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

tree_index = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  tree_index[a].append(b) # a<bとは限らないため、双方向にする
  tree_index[b].append(a)
ans = [0] * n
for i in range(q):
  p, x = map(int, input().split())
  ans[p-1] += x
def dfs(cur, par):
    for chl in tree_index[cur]:
      if chl == par: #parentとchildが等しい時はスキップ
        continue
      ans[chl] += ans[cur] 
      dfs(chl, cur)
dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x
def dfs(c, parent):
    for child in tree[c]:
        if child == parent:
            continue
        else:
            ans[child] += ans[c]
            dfs(child, c)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x
def dfs(c, parent):
    for child in tree[c]:
        if child == parent:
            continue
        ans[child] += ans[c]
        dfs(child, c)
dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x
def dfs(c, parent):
    for child in tree[c]:
        if child == parent:
            continue
        else:
            ans[child] += ans[c]
            dfs(child, c)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x
def dfs(c, parent):
    for child in tree[c-1]:
        if child == parent:
            continue
        else:
            ans[child-1] += ans[c-1]
            dfs(child, c)
dfs(1, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

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

def dfs(v, p):
    if p != -1:
        count[v] += count[p]
    for next_v in G[v]:
        if next_v == p: continue
        dfs(next_v, v)

dfs(0, -1)
for i in range(N):
    print(count[i], end=" ")

Submission status: Runtime Error

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

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


def dfs(xs, now=0):
    print(xs, now)
    for x in xs:
        nodes[x] += now
        if childs[x] != []:
            dfs(childs[x], nodes[x])
    return


dfs(childs[0], nodes[0])
print(*nodes)

Submission status: Runtime Error

Solution code:
# ソートが悪かったりします？
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
# ab = sorted(ab, key=lambda x: x[0])
px = [[0, 0]] * (N - 1)
for i in range(Q):
    px[i] = list(map(int, input().split()))
# px = sorted(px, key=lambda x: x[0])
nodes = [0] * (N)
for p, x in px:
    nodes[p - 1] += x
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ab = [[0, 0]] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: x[0])
px = [[0, 0]] * (N - 1)
for i in range(Q):
    px[i] = list(map(int, input().split()))
px = sorted(px, key=lambda x: x[0])
nodes = [0] * (N)
for p, x in px:
    nodes[p - 1] += x
for a, b in ab:
    nodes[b - 1] += nodes[a - 1]
print(' '.join(map(str, nodes)))

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
ab = [[int(x) for x in input().split()]for _ in range(n-1)]
px = [[int(x) for x in input().split()]for _ in range(q)]

gr = [[]for _ in range(n+1)]
for a,b in ab:
    gr[a].append(b)
    gr[b].append(a)

value = [0]*(n+1)
for p,x in px:
    value[p] += x

def dfs(v,parent,add):
    value[v] += add
    for x in gr[v]:
        if x ==parent:
            continue
        dfs(x,v,value[v])
dfs(1,0,0)

ans = ' '.join(map(str,value[1:]))
print(ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
ab = [[int(x) for x in input().split()]for _ in range(n-1)]
px = [[int(x) for x in input().split()]for _ in range(q)]

gr = [[]for _ in range(n+1)]
for a,b in ab:
    gr[a].append(b)
    gr[b].append(a)

value = [0]*(n+1)
for p,x in px:
    value[p] += x

def dfs(v,parent,add):
    value[v] += add
    for x in gr[v]:
        if x ==parent:
            continue
        dfs(x,v,value[v])
dfs(1,0,0)

ans = ' '.join(map(str,value[v]))
print(ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().spliot())
ki = [0]*(n+1)
for i in range(n-1):
    a,b = map(int,input().split())
    ki[b] = a

c = [0]*(n+1)
for i in range(q):
    p,x = map(int,input().split())
    c[p] += x
for i in range(1,n+1):
    c[i] += c[ki[i]]
print("".join(map(str,c[1:])))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
import sys
sys.setrecursionlimit(10**6)

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

graph = defaultdict(deque)

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

arr = [0] * (n + 1)

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


visited = [False] * (n + 1)


def dfs(curr):
    stack = graph[curr]

    while stack:
        leaf = stack.pop()
        if visited[leaf]:
            continue
        arr[leaf] += arr[curr]
        visited[leaf] = True
        dfs(leaf)


print(*ans[1:])

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
G=[[] for i 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)

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

ans=[0]*n

check=[False]*n
temp=0
def dfs(v):
    global temp

    check[v]=True
    temp+=add[v]
    ans[v]=temp

    for next in G[v]:
        if check[next]:
            continue
        dfs(next)
    
    temp-=add[v]

dfs(0)

print(*ans)

Submission status: Runtime Error

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,q = readInts()
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(lambda x: int(x) - 1,input().split())
    G[a].append(b)
    G[b].append(a)
val = [0] * n
for i in range(q):
    p,x = readInts()
    p -= 1
    val[p] += x

def dfs(v,p):
    if p != -1:
        val[v] += val[p]
    for e in G[v]:
        if e == p:
            continue
        dfs(e,v)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
def main():
    N, Q = map(int,input().split())
    G = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int,input().split())
        G[a-1].append(b-1)

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

    def dfs(ima):
        for tsugi in G[ima]:
            V[tsugi] += V[ima]
            dfs(tsugi)
    dfs(0)
    print(*V)
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
graph = [[] for i in range(N - 1)]
for i in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
for i in graph:
    if len(i):
        for j in i:
            if len(graph[j - 1]):
                for k in graph[j]:
                    i.append(k)
ans = [[0] for i in range(N)]

for i in range(Q):
    p, q = map(int, input().split())
    p -= 1
    ans[p].append(q)
    if len(graph[p]):
        for j in graph[p]:
            ans[j].append(q)
anss = []
for i in ans:
    anss.append(str(sum(i)))
print(" ".join(anss))

Submission status: Runtime Error

Solution code:

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

l = [[] for i in range(n+1)]
score = [0]*(n+1)
for i in range(n-1):
    a,b = map(int,input().split())
    l[a-1].append(b-1)
    l[b-1].append(a-1)

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

isSearched = [False]*(n+1)
isSearched[0] = True
def dfs(now):
    ln = l[now]
    for i in ln:
        if isSearched[i]:
            continue
        score[i] += score[now]
        isSearched[i] = True
        dfs(i)

dfs(0)
w = ""
for i in range(n-1):
    w += (str(score[i]) + " ")

w += str(score[n-1])
print(w)

Submission status: Runtime Error

Solution code:

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

l = [[] for i in range(n+1)]
score = [0]*(n+1)
for i in range(n-1):
    a,b = map(int,input().split())
    l[a-1].append(b-1)
    l[b-1].append(a-1)

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

isSearched = [False]*(n+1)
isSearched[0] = True
def dfs(now):
    ln = l[now]
    for i in ln:
        if isSearched[i]:
            continue
        score[i] += score[now]
        isSearched[i] = True
        dfs(i)

dfs(0)

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

Submission status: Runtime Error

Solution code:

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

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

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

isSearched = [False]*n
isSearched[0] = True
def dfs(now):
    ln = l[now]
    for i in ln:
        if isSearched[i]:
            continue
        score[i] += score[now]
        isSearched[i] = True
        dfs(i)

dfs(0)

for i in score:
    print(i)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3


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

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


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


ans = [0] * n


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


dfs(0, -1, 0)

# print(add)

print(*ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# from collections import deque

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

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


# root_tree = [[] for _ in range(n)]

# check = [0 for _ in range(n)]

# queue = deque([0])
# while(len(queue) > 0):
#     # print(queue)
#     index = queue.pop()
#     list_ = tree[index]
#     if check[index] == 1:
#         continue
#     check[index] = 1
#     # print(list_)
#     for child in list_:
#         if check[child] == 0:
#             root_tree[index].append(child)
#             queue.append(child)

add = [0 for _ in range(n)]

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


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


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


dfs(0, -1, 0)

# print(add)

print(*ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# from collections import deque

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

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


# root_tree = [[] for _ in range(n)]

# check = [0 for _ in range(n)]

# queue = deque([0])
# while(len(queue) > 0):
#     # print(queue)
#     index = queue.pop()
#     list_ = tree[index]
#     if check[index] == 1:
#         continue
#     check[index] = 1
#     # print(list_)
#     for child in list_:
#         if check[child] == 0:
#             root_tree[index].append(child)
#             queue.append(child)

add = [0 for _ in range(n)]

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


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


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


dfs(0, -1, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque

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

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


root_tree = [[] for _ in range(n)]

check = [0 for _ in range(n)]

# queue = deque([0])
# while(len(queue) > 0):
#     # print(queue)
#     index = queue.pop()
#     list_ = tree[index]
#     if check[index] == 1:
#         continue
#     check[index] = 1
#     # print(list_)
#     for child in list_:
#         if check[child] == 0:
#             root_tree[index].append(child)
#             queue.append(child)

add = [0 for _ in range(n)]

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


root_tree = tree

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


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


dfs(0, -1, 0)

print(*ans)