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:
import sys
sys.setrecursionlimit(4000)

def dfs(now):
  v[now]=0
  for i in edge[now]:
    if v[i]:
      point[i]+=point[now]
      dfs(i)

n,q=map(int,input().split())
edge=[[] for _ in range(n)]
point=[0]*n
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)
for i in range(q):
  p,x=map(int,input().split())
  point[p-1]+=x
v=[1]*n
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(4000)

def dfs(now):
  for i in edge[now]:
    point[i]+=point[now]
    dfs(i)

n,q=map(int,input().split())
edge=[[] for _ in range(n)]
point=[0]*n
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
for i in range(q):
  p,x=map(int,input().split())
  point[p-1]+=x
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

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

def dfs(m):
    if m in vdic:
        for i in range(len(vdic[m])):
            point[vdic[m][i]] += point[m]
            dfs(vdic[m][i])

point = [0]*(n+1)

vdic = {}
for i in range(n-1):
    if ab[i][0] not in vdic:
        vdic[ab[i][0]] = []
    vdic[ab[i][0]].append(ab[i][1])

for i in range(q):
    point[px[i][0]] += px[i][1]
'''
for v in vdic:
    for c in vdic[v]:
        point[c] += point[v]
'''
dfs(1)

for i in range(1,len(point)):
    print(point[i],end = ' ')

Submission status: Runtime Error

Solution code:
def dfs(v, p=-1):
    for nx in G[v]:
        if nx==p:
            continue
        ans[nx] += ans[v]
        dfs(nx, v)

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

ans = [0]*n
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:
def dfs(v, p=-1):
    for nx in G[v]:
        if nx==p: continue
        ans[nx] += ans[v]
        dfs(nx, v)

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

ans = [0]*n
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:
def dfs(v, p, ans, G):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        ans = dfs(g, v, ans, G)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

ans = dfs(0, -1, ans, G)

print(*ans)

Submission status: Runtime Error

Solution code:
def dfs(v, p, ans):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        dfs(g, v, ans)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

ans = dfs(0, -1, ans)

print(*ans)

Submission status: Runtime Error

Solution code:
def dfs(v, p, ans):
    if p!=-1: ans[v] += ans[p]
    for g in G[v]:
        if g==p: continue
        dfs(g, v, ans)
    return ans

n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)
    G[b].append(a)

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

dfs(0, -1, ans)

print(*ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ans = [0]*n
AB = [[] for _ in range(n-1)]
for i in range(n-1):
    AB[i] = list(map(lambda x: x-1, map(int, input().split())))
AB.sort(key=lambda x: x[1])

for (a,b) in AB:
    T[a] = T[a].union(T[b])

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

for (a,b) in AB:
    ans[b] += ans[a]

print(*ans)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
root=[[] for _ in range(n)]
f=lambda x:int(x)-1
 
for i in range(n-1):
  a,b=map(f,input().split())
  root[a].append(b)
  root[b].append(a)
 
score=[0]*n
for j in range(q):
  p,x=map(int,input().split())
  score[p-1]+=x
 
visit = [False]*n
def dfs(now):
  visit[now] = True
  for go in root[now]:
    if visit[go]:
      continue
    score[go] += score[now]
    dfs(go)

dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for _ in range(n)]
f=lambda x:int(x)-1
 
for i in range(n-1):
  a,b=map(f,input().split())
  l[a].append(b)
  l[b].append(a)
 
score=[0]*n
for j in range(q):
  p,x=map(int,input().split())
  score[p-1]+=x
 
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)

dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for _ in range(n)]
f=lambda x:int(x)-1
 
for i in range(n-1):
  a,b=map(f,input().split())
  l[a].append(b)
  l[b].append(a)
 
score=[0]*n
for j in range(q):
  p,x=map(int,input().split())
  score[p-1]+=x
 
visit=set()
def dfs(now):
  visit.add(now)
  for t in l[now]:
    if t not in visit:
      visit.add(t)
      score[t]+=score[now]
      dfs(t)
dfs(0)
print(*score)

Submission status: Runtime Error

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

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

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

visit=set()
def dfs(now):
  visit.add(now)
  for t in l[now]:
    if t not in visit:
      visit.add(t)
      score[t]+=score[now]
      dfs()
dfs(0)
print(*score)

Submission status: Runtime Error

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

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)

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

ans = [0] * n

def dfs(u, parent=None):
    ans[u] = ans[parent] + xs[u]
    for v in tree[u]:
        if not v == parent:
            dfs(v, u)

dfs(0, 0)
print(' '.join([str(i) for i in 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)

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

ans = [0] * n

def dfs(u, parent=None):
    ans[u] = ans[parent] + xs[u]
    for v in tree[u]:
        if not v == parent:
            dfs(v, u)

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

Submission status: Runtime Error

Solution code:
"""
深さ優先探索(DFS: Depth First Search)

AtCoder ABC138 D問題に対応
https://atcoder.jp/contests/abc138/tasks/abc138_d

(example input)
4 3
1 2
2 3
2 4
2 10
1 100
3 1

"""

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)

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

ans = [0] * n

def dfs(u, parent=None):
    ans[u] = ans[parent] + xs[u]
    for v in tree[u]:
        if v != parent:
            dfs(v, u)

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

Submission status: Runtime Error

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

visited = [False for _ in range(n)]


def dfs(current):
    for c in ab[current]:
        if visited[c]:
            continue
        ans[c] += ans[current]
        visited[c] = True
        dfs(c)


ans = px
visited[0] = True
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N, Q = list(map(int, input().split()))
a, b = [], []
for n in range(N - 1):
    a_n, b_n = list(map(int, input().split()))
    a.append(a_n)
    b.append(b_n)
p, x = [], []
for q in range(Q):
    p_q, x_q = list(map(int, input().split()))
    p.append(p_q)
    x.append(x_q)

# グラフの作成
g = [list() for n in range(N)]
for a_n, b_n in zip(a, b):
    g[a_n - 1].append(b_n - 1)

c = [0 for n in range(N)]


def dfs(seen: list, v: int, x: int) -> None:
    seen[v] = True
    c[v] += x
    for w in g[v]:
        if not seen[w]:
            dfs(seen, w, x)


# 求解処理
for p_q, x_q in zip(p, x):
    seen = [False for n in range(N)]
    dfs(seen, p_q - 1, x_q)
ans = " ".join(map(str, c))

# 結果出力
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)
 
X = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    X[p-1] += x
 
ans = [0]*N
 
def dfs(u, parent=None):
    ans[u] = ans[parent] + X[u]
    for v in tree[u]:
        if v != parent:
            dfs(v, u)
 
dfs(0, 0)
print(' '.join([str(i) for i in 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)

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

ans = [0]*N

def dfs(u, parent=None):
    ans[u] = ans[parent] + X[u]
    for v in tree[u]:
        if v != parent:
            dfs(v, u)

dfs(0, 0)
print(' '.join([str(i) for i in 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)

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

ans = [0]*N

def dfs(u, parent=None):
    """
    u: 子ノード
    parent: 親ノード
    """
    ans[u] = ans[parent] + X[u]
    for v in tree[u]:
        if v != parent:
            dfs(v, u)

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

tree = [0] * (N + 1)
for p, x in Y:
    tree[p] += x

graph = [[] for _ in range(N + 1)]
for a, b in X:
    graph[a].append(b)
    graph[b].append(a)

visited = [False] * (N + 1)
visited[1] = True

def dfs(x,d,s): # x:計算する頂点（上側）, d:計算済み(0)か否か(1), s:頂点xの値
  for y in v[x]: # 頂点xとつながる各頂点yの計算を行う
    if d[y]: # 頂点yが計算未了の場合
      s += f[y] # 頂点xの値sに頂点yの値を足す
      ans[y] = s # 頂点yの値はsに確定
      d[y] = 0 # 頂点yの計算は終わったのでd[y]=0とする
      dfs(y,d,s) # 頂点yの下層についても再帰的に計算
      s -= f[y] # 頂点yの下層の計算が終わったので、sを頂点xの値に戻す

def DFS(pos,visited):
    for i in graph[pos]:
        if not visited[i]:
            visited[i] = True
            tree[i] += tree[pos]
            DFS(i,visited)#下層を計算
            return 0
    return 0

DFS(1,visited)            
"""
stack = deque()
stack.append(1)
while stack:
    u = stack.popleft()
    print(u)
    for v in graph[u]:
        if not visited[v]:
            visited[v] = True
            tree[v] += tree[u]
            stack.append(v)
"""            
print(*tree[1:])

Submission status: Runtime Error

Solution code:
from collections import deque

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

tree = [0] * (N + 1)
for p, x in Y:
    tree[p] += x

graph = [[] for _ in range(N + 1)]
for a, b in X:
    graph[a].append(b)
    graph[b].append(a)

visited = [False] * (N + 1)
visited[1] = True

def DFS(pos,visited):
    for i in graph[pos]:
        if not visited[i]:
            visited[i] = True
            tree[i] += tree[pos]
            DFS(i,visited)
DFS(1,visited)            
"""
stack = deque()
stack.append(1)
while stack:
    u = stack.popleft()
    for v in graph[u]:
        if not visited[v]:
            visited[v] = True
            tree[v] += tree[u]
            stack.append(v)
"""            
print(*tree[1:])

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 = [0] * (N + 1)#もう点数がもらえることが自明の場所
for p, x in px:
    tree[p] += x

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

counter=[False]*(N+1)
counter[1]=True

#根、計算済みかどうか
def DFS(pos,counter):
    #根につながる場所を探索
    for i in graph[pos]:
        #まだ訪れていない場合
        if not counter[i]:
            tree[i]+=tree[pos]#根の値を葉に足す
            counter[i]=True#探索済み
            DFS(i,counter)#葉を根とする
#一番上の根からスタート
DFS(1,counter)

print(*tree[1:])

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 = [0] * (N + 1)#もう点数がもらえることが自明の場所

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

#場所、得点、計算済みかどうか
def DFS(pos,x):
    tree[pos]+=x
    for i in graph[pos]:
        if i>pos:
            DFS(i,x)
for p,x in px:
    DFS(p,x)

print(*tree[1:])

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 = [0] * (N + 1)#もう点数がもらえることが自明の場所

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


#場所、得点、計算済みかどうか
def DFS(pos,x):
    tree[pos]+=x
    if len(graph[pos])>0:
        for i in graph[pos]:
            if i>pos:
                DFS(i,x)
for p,x in px:
    DFS(p,x)
print(tree[1:])

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def dfs(u, add):
    cnt[u] += add
    for nu in graph[u]:
        dfs(nu, add)


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

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

cnt = [0] * (n + 1)


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

print(*cnt[1:])

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
c = [0] * n
l = {}
for i in range(n - 1):
    a, b = map(int, input().split())
    l[b] = a
for i in range(q):
    p, x = map(int, input().split())
    c[p - 1] += x
for i in range(2, n + 1):
    c[i - 1] += c[l[i] - 1]
print(*c)

Submission status: Runtime Error

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

from collections import defaultdict
counter = defaultdict(int)
for _ in range(Q):
  p,x = map(int,input().split())
  p -= 1
  counter[p] += x

weight = [-1] * N
def dfs(node, wei):
  weight[node] = wei

  for nei in adj[node]:
    if weight[nei] == -1:
      dfs(nei, wei+counter[nei])

dfs(0, counter[0])
print(" ".join(map(str,weight)))

Submission status: Runtime Error

Solution code:
def main():
    n,q=map(int,input().split())
    ab=[[] for _ in range(n)]
    for i in range(n-1):
        a,b=map(int,input().split())
        ab[a-1].append(b-1)
        ab[b-1].append(a-1)
    visit=[False]*n
    cnt=[0]*n
    for i in range(q):
        p,x=map(int,input().split())
        cnt[p-1]+=x

    def solve(s,x):
        visit[s]=True
        cnt[s]+=x
        x=cnt[s]
        for i in ab[s]:
            if visit[i]:
                continue
            solve(i,x)
        return
    
    solve(0,0)
    print(*cnt, sep=' ')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def MI():  return map(int, input().split())
N,Q=MI()
graph=[[] for i in range(N)]
for i in range(1,N):
  a,b=MI()
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)
point=[0]*N  
for i in range(Q):
  p,x=MI()
  point[p-1]+=x
def dfs(now,pre=-1):  
  for next in graph[now]:
    if next==pre:
      continue
    point[next]+=point[now]
    dfs(next,now)
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g = [[] for _ in range(n)]
val = [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)

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

seen = [False] * n
def dfs(v):
    seen[v] = True
    for nv in g[v]:
        if seen[nv]:
            continue
        val[nv] += val[v]
        dfs(nv)

dfs(0)
print(*val)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0] * N

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

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

visit = [False] * N
def dfs(n):
    visit[n] = True
    for go in root[n]:
        if visit[go]:
            continue
        ans[go] += ans[n]
        dfs(go)

dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

MAP=np.zeros([N,N],dtype=int)

for n in range(N-1):
    p=ab[n][0]-1
    s=ab[n][1]-1
    MAP[p,p]=1
    MAP[s,s]=1
    MAP[p,s]=1
    if s>=1 and p>=1:
        MAP[:p,s]=MAP[:p,p]

cnt=np.array([0 for n in range(N)])
for q in range(Q):
    p=px[q][0]-1
    x=px[q][1]
    cnt+=MAP[p,:]*x

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  root[a].append(b)
  root[b].append(a)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(N)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root[a-1].append(b-1)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
visit = [False]*N
def dfs(n):
  visit[n] = True
  for go in root[n]:
    if visit[go]:
      continue
    ans[go] += ans[n]
    dfs(go)
    
dfs(0)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())
root = [[] for i in range(5)]
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root[a-1].append(b)
  
for i in range(Q):
  p, x = map(int, input().split())
  ans[p-1] += x
  
now = 1
visit = deque([1])
while len(visit) < N:
  if root[now-1] == []:
    now = visit[visit.index(now)-1]
  else:
    go = root[now-1][0]
    root[now-1].pop(0)
    visit.append(go)
    ans[go-1] += ans[now-1]
    now = go
    
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g = [[] for _ in range(n)]
value = [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)
for _ in range(q):
    p, x = map(int, input().split())
    value[p-1] += x

seen = [False] * n
def dfs(v):
    seen[v] = True
    for nv in g[v]:
        if seen[nv]:
            continue
        value[nv] += value[v]
        dfs(nv)
dfs(0)
print(*value)

Submission status: Runtime Error

Solution code:
def dfs(i, x):
    ans[i] = x
    for v in G[i]:
        if ans[v]==0:
            dfs(v, x+num[v])

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)

ans = [0] * n
num = [0] * n

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

dfs(0, num[0])

print(' '.join([str(i) for i in ans]))

Submission status: Runtime Error

Solution code:
N,Q = map(int, input().split())
childlist = [[] for _ in range(N+1)]

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

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

def DFS(v):
    if not checkedlist[v]:
        checkedlist[v] = True
        
        global point
        point += pointlist[v]
        for child in childlist[v]:
            DFS(child)
        
        totallist[v] = point
        point -= pointlist[v]
        
totallist = [0]*(N+1)
checkedlist = [False]*(N+1)
point = 0
for i in range(1,N+1):
    if not checkedlist[i]:
        DFS(i)

print(*totallist[1:])

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ab = [[] for _ in range(n)]
px = [0 for _ in range(n)]
ans = [0 for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    ab[a - 1].append(b - 1)
for i in range(q):
    p, x = map(int, input().split())
    px[p - 1] += x


def dfs(current, v):
    vv = v + px[current]
    ans[current] = vv
    for c in ab[current]:
        dfs(c, vv)


dfs(0, 0)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2000)
n,q=map(int,input().split())
e=[[] for _ in range(n)]
cnt=[0]*n
for _ in range(n-1):
  a,b=map(int,input().split())
  e[a-1].append(b-1)
  e[b-1].append(a-1)
for _ in range(q):
  x,y=map(int,input().split())
  cnt[x-1]+=y

def dfs(now,vis):
  vis.append(now)
  for i in e[now]:
    if i not in vis:
      cnt[i]+=cnt[now]
      dfs(i,vis)
dfs(0,[])
print(' '.join(map(str,cnt)))

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
e=[[] for _ in range(n)]
cnt=[0]*n
for i in range(n-1):
  a,b=map(int,input().split())
  e[a-1].append(b-1)
  e[b-1].append(a-1)
for _ in range(q):
  x,y=map(int,input().split())
  cnt[x-1]+=y

def dfs(now,vis):
  vis.append(now)
  for i in e[now]:
    if i not in vis:
      cnt[i]+=cnt[now]
      dfs(i,vis)
dfs(0,[])
print(' '.join(map(str,cnt)))

Submission status: Runtime Error

Solution code:
import sys


sys.setrecursionlimit(100000)

to = [[] for _ in range(200005)]
ans: list


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


def main():
    N, Q = map(int, input().split())
    for _ in range(N-1):
        a, b = map(int, input().split())
        to[a-1].append(b-1)
        to[b-1].append(a-1)
    global ans
    ans = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        ans[p-1] += x
    dfs(0)
    print(*ans)


if __name__ == "__main__":
    main()

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())
    a-=1
    b-=1
    tree[a].append(b)
    tree[b].append(a)
cnt=[0]*n
for _ in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

def DFS(v,p_v):
    for next_v in tree[v]:
        if next_v==p_v:
            continue
        cnt[next_v]+=cnt[v]
        DFS(next_v,v)

DFS(0,-1)
print(*cnt)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ab = [[] for _ in range(n)]
pq = [0 for _ in range(n)]
ans = [0 for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    ab[a - 1].append(b - 1)
for i in range(q):
    p, q = map(int, input().split())
    pq[p - 1] += q


def dfs(parent, v):
    vv = v + pq[parent]
    ans[parent] = vv
    for c in ab[parent]:
        dfs(c, vv)


dfs(0, 0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
class node:
  def __init__(self):
    self.next = []
    self.point = 0

  def add_node(self,node_num):
    self.next.append(node_num-1)
  
  def add_point(self, point):
    self.point += point

    for i in self.next:
      lst[i].add_point(point)


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

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

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

for i in lst:
  print(i.point, end=' ')

Submission status: Runtime Error

Solution code:
# 解法 累積和とdfsを使うと探索が減ってなんかうまくいく．
N,Q = map(int,input().split())
E=[[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    E[a-1].append(b)
    E[b-1].append(a)
dic = {}
for _ in range(Q):
    p,x = map(int,input().split())
    if not p-1 in dic:
        dic[p-1] = x
        continue
    dic[p-1] += x

res = [0 for _ in range(N)]
def dfs(p,s,ans):
    global res
    # 終わり条件　最後まで行ったら，つまりE[k] = []になったら.
    # 再帰 累積和を計算して，自分がつながっているノードに接続している
    if s-1 in dic:
        ans += dic[s-1]

    res[s-1] = str(ans)
    if E[s-1] == [p]:
        return
    for e in E[s-1]:
        if e == p:
            continue
        dfs(s,e,ans)
dfs(1,1,0)
print(' '.join(res))

Submission status: Runtime Error

Solution code:
# 解法 累積和とdfsを使うと探索が減ってなんかうまくいく．
N,Q = map(int,input().split())
E=[[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    E[a-1].append(b)
    E[b-1].append(a)
dic = {}
for _ in range(Q):
    p,x = map(int,input().split())
    if not p-1 in dic:
        dic[p-1] = x
        continue
    dic[p-1] += x

res = [0 for _ in range(N)]
def dfs(p,s,ans):
    global res
    # 終わり条件　最後まで行ったら，つまりE[k] = []になったら.
    # 再帰 累積和を計算して，自分がつながっているノードに接続している
    if s-1 in dic:
        ans += dic[s-1]

    res[s-1] = ans
    if E[s-1] == [p]:
        return
    for e in E[s-1]:
        if e == p:
            continue
        dfs(s,e,ans)
dfs(1,1,0)
print(res)

Submission status: Runtime Error

Solution code:
N, Q = [int(_) for _ in input().split()]
edge = [[] for _ in range(N)]

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


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


def dfs(n, parent):
    for to in edge[n]:
        if to == parent:
            continue
        vertex[to] += vertex[n]
        dfs(to, n)


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

Submission status: Runtime Error

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

def dfs(v, pv):
    for next_v in rl[v]:
        if next_v == pv:
            continue
        cnt[next_v] += cnt[v]
        dfs(next_v, v)
dfs(0, -1)
print(cnt)

Submission status: Runtime Error

Solution code:
#%%
N, Q = [int(i) for i in input().split()]
AB = [[] for _ in range(N - 1)]
PX = [[] for _ in range(Q)]
for i in range(N - 1):
    AB[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    PX[i] = [int(i) for i in input().split()]
    PX[i][0] -= 1
#%%

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

def DFS(node_id, value, list_tree):
    list_tree[node_id].value += value
    for i in list_tree[node_id].children:
        DFS(i, value, list_tree)

#%%     
list_node = [tree(i) for i in range(N)]
for i, j in AB:
    list_node[i].children.append(j)
    list_node[j].parent = i

for i, j in PX:
    DFS(i, j, list_node)
    
for i in list_node:
    print(i.value, end=" ")

Submission status: Runtime Error

Solution code:
to = [[] for _ in range(200005)]
ans: list


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


def main():
    N, Q = map(int, input().split())
    for _ in range(N-1):
        a, b = map(int, input().split())
        to[a-1].append(b-1)
        to[b-1].append(a-1)
    
    global ans
    ans = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        ans[p-1] += x
    dfs(0)
    print(*ans)


if __name__ == "__main__":
    main()