Problem:


Score : 400 points

Problem Statement

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

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

Now, the following Q operations will be performed:

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

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

Constraints

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

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

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

100 110 111 110

The tree in this input is as follows:

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

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

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

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

Sample Input 2

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

Sample Output 2

20 20 20 20 20 20



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


Sample output:
100 110 111 110

Submission status: Runtime Error

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

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

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

def dfs(i, cnt, ans, memo):
    cnt += cost[i]
    ans[i] = cnt
    if not tree[i]:
        return

    for x in tree[i]:
        if x in memo:
            continue
        memo.append(x)
        dfs(x, cnt, ans, memo)

dfs(0, 0, ans, memo)

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)

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

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

def dfs(i, cnt, ans):
    cnt += cost[i]
    ans[i] = cnt
    if not tree[i]:
        return

    for x in tree[i]:
        dfs(x, cnt, ans)

dfs(0, 0, ans)

print(*ans)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
N,Q = map(int,readline().split())
abpx = list(map(int,read().split()))
ab = iter(abpx[:N+N-2])
px = iter(abpx[N+N-2:])
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for a,b in zip(ab,ab):
    dic[a].append(b)
    dic[b].append(a)
for p,x in zip(px,px):
    dic2[p] += x
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    tg_list = dic[now_node][:]
    for elem in tg_list:
        dic[elem].remove(now_node)
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200001)
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) ]
dic = {}
dic2=[0]*N
dic3=[0]*N

for i in range(N-1):
    dic[i+1] = []
    dic[ab[i][0]].append(ab[i][1])
dic[N] = []
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1-1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node-1]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200001)
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) ]
dic = {}
dic2={}
dic3={}

for i in range(N):
    dic[i+1] = []
    dic2[i+1] = 0
for i in range(N-1):
    tmp1 = ab[i][0]
    tmp2 = ab[i][1]
    dic[tmp1].append(tmp2)
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
ab = [ list(map(int,input().split())) for i in range(N-1) ]
px = [ list(map(int,input().split())) for i in range(Q) ]
a = [ab[i][0] for i in range(N-1)]
b = [ab[i][1] for i in range(N-1)]
dic = {}
dic2={}
dic3={}

for i in range(N):
    dic[i+1] = []
    dic2[i+1] = 0
for i in range(N-1):
    tmp1 = a[i]
    tmp2 = b[i]
    dic[tmp1].append(tmp2)
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Runtime Error

Solution code:
# インプット
n, q = map(int, input().split())
nlist = [list(map(int, input().split())) for _ in range(n-1)]
qlist = [list(map(int, input().split())) for _ in range(q)]

# ｛親(1~N):子以下のノード番号｝辞書型のモデル作成
tree = {}
for i in range(1,n+1):
    tree[i] = set()
for a, b in nlist:
    tree[a].add(b)
for i in reversed(tree.keys()):
    if tree[i] == {}: continue
    for j in tree[i]:
        tree[i] = tree[i] | tree[j]

# カウンターの値計算
real_tree = [0]*n
for j, x in qlist:
    real_tree[j-1] += x
    for jj in tree[j]:
        real_tree[jj-1] += x
        
print(*real_tree, sep=" ")

Submission status: Runtime Error

Solution code:
# インプット
n, q = map(int, input().split())
nlist = [list(map(int, input().split())) for _ in range(n-1)]
qlist = [list(map(int, input().split())) for _ in range(q)]

# ｛親(1~N):子以下のノード番号｝辞書型のモデル作成
tree = {}
for i in range(1,n+1):
    tree[i] = []
for a, b in nlist:
    tree[a].append(b)
for i in reversed(tree.keys()):
    if tree[i] == []: continue
    for j in tree[i]:
        tree[i].extend(tree[j])

# カウンターの値計算
real_tree = [0]*n
for j, x in qlist:
    real_tree[j-1] += x
    for jj in tree[j]:
        real_tree[jj-1] += x
print(*real_tree, sep=" ")

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
nlist = [list(map(int, input().split())) for _ in range(n-1)]
qlist = [list(map(int, input().split())) for _ in range(q)]

# 親子辞書型の木モデル作成
tree = {}
for i in range(1,n+1):
    tree[i] = []
for a, b in nlist:
    tree[a].append(b)
for j in reversed(tree.keys()):
    if tree[j] == []: continue
    for k in tree[j]:
        tree[j].extend(tree[k])

# 実際に計算に使う木を作成
real_tree = [0]*n
for j, x in qlist:
    real_tree[j-1] += x
    for jj in tree[j]:
        real_tree[jj-1] += x
print(*real_tree, sep=" ")

Submission status: Runtime Error

Solution code:
n, q = (int(x) for x in input().split())
ab = []
px = []
ans = [0 for _ in range(n)]
d = {}
for _ in range(n-1):
    ab.append([int(x) for x in input().split()])
for _ in range(q):
    px.append([int(x) for x in input().split()])
for i in ab:
    key = str(i[1])
    if key not in d.keys():
        d[key] = []
    d[key].append(i[0])
for i in px:
    ans[i[0] - 1] += i[1]
for i in range(2, n+1):
    ans[i - 1] += ans[d[str(i)][0] - 1]
for ans_i in ans: print(ans_i)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

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

cnt=[-1]*n
cnt[0]=t_cnt[0]

seen=([0])

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 for i in tree[x]:
#  if cnt[i]==-1:
#  if i in seen:
#   continue
#  else:
   if i not in seen:
    seen.add(i)
    dq.append(i)
    cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

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

cnt=[-1]*n
cnt[0]=t_cnt[0]

#seen=([0])
seen=[0]

dq=deque()
dq.append(0)

while dq:
 x=dq.popleft()
 for i in tree[x]:
#  if cnt[i]==-1:
   if i in seen:
    continue
   else:
    seen.add(i)
    dq.append(i)
    cnt[i]=cnt[x]+t_cnt[i]

print(*cnt)

Submission status: Runtime Error

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

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

ans=[0 for _ in range(n)]
def dfs(vertex,pt):
    pt_v=pt+point[vertex]
    ans[vertex]=pt_v
    child_v=child[vertex]
    for i in range(len(child_v)):
        dfs(child_v[i],pt_v)


dfs(0,0)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2 * 10**5)
def input():
    return sys.stdin.readline()


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

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)

Submission status: Runtime Error

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

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

counter=[0 for i in range(N+1)]
for i in range(Q):
  counter[px[i][0]]+=px[i][1]
  
def f(num, prenum=-1, p=0):
  counter[num]+=p
  for postnum in link[num]:
    if postnum==prenum:
      continue
    f(postnum, num, counter[num])
    
f(1)

print(*counter[1:])

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import sys
input=sys.stdin.readline
sys.setrecursionlimit(1000000)

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

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

ketsu=[[]*(N) for i in range(N)]
point=[0]*N
ans=[0]*N

def dfs(num,value):
    ans[num]+=value
    for item in ketsu[num]:
        dfs(item,value+point[item])
            

for _ in range(N-1):
    a,b=map(int,input().split())
    ketsu[a-1].append(b-1)
    #ketsu[b-1].append(a-1)
    root.remove(b-1)
    
for _ in range(Q):
    p,x=map(int,input().split())
    point[p-1]+=x
#print(point)

for item in root:
    dfs(item,point[item])
    
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
# AB = []

G = []
for i in range(N):
    G.append([])

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

# PX = []
c = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    # PX.append([p, x])
    c[p-1] += x

# r = [0] * N

# print(G)
# print(c)

visited = [False] * N

def dfs(v):
    visited[v] = True
    childs = G[v]
    if not childs:
        return
    
    val = c[v]
    for n in childs:
        c[n] += val
        dfs(n)

dfs(0)

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
X = [0 for _ in range(N)]
P = [0 for _ in range(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())
    p -= 1
    X[p] += x


def dfs(p, x, parent):

    P[p] += x + X[p]
    for node in G[p]:
        if node == parent:
            continue
        dfs(node, x + X[p], p)


dfs(0, 0, -1)

print(' '.join([str(p) for p in P]))

Submission status: Runtime Error

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

v = [[] for i in range(N)]
score = [0]*N

def dfs(cur,par):
  for chi in v[cur]:
    if chi == par:
      continue
    score[chi] += score[cur]
    dfs(chi, cur)
      
for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  v[a].append(b)
  v[b].append(a)

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

dfs(0,-1)
score_int=[str(a) for a in score]
output= ' '.join(score_int)
print(output)

Submission status: Runtime Error

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

v = [[] for i in range(N)]
score = [0]*N

def dfs(p):
  for i in v[p-1]:
    if p < i:
      score[i-1] += score[p-1]
      dfs(i)
      
for i in range(N-1):
  a, b = map(int, input().split())
  v[a-1].append(b)
  v[b-1].append(a)

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

dfs(1)
score_int=[str(a) for a in score]
output= ' '.join(score_int)
print(output)

Submission status: Runtime Error

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

v = [[] for i in range(N)]
score = [0]*N

def dfs(p, x):
  for i in v[p-1]:
    if p < i:
      score[i-1] += x
      dfs(i, x)
      
for i in range(N-1):
  a, b = map(int, input().split())
  v[a-1].append(b)
  v[b-1].append(a)

for j in range(Q):
  p, x = map(int, input().split())
  score[p-1] += x
  dfs(p, x)
score_int=[str(a) for a in score]
output= ' '.join(score_int)
print(output)

Submission status: Runtime Error

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

v = [[] for i in range(N)]
score = [0]*N

def dfs(p, x):
  for i in v[p-1]:
    if p < i:
      score[i-1] += x
      dfs(i, x)
      
for i in range(N-1):
  a, b = map(int, input().split())
  v[a-1].append(b)
  v[b-1].append(a)

for j in range(Q):
  p, x = map(int, input().split())
  score[p-1] += x
  dfs(p, x)
print(score)

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)
  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 child in tree_index[cur]:
    if child == par:
      continue
    ans[child] += ans[cur]
    dfs(child, cur)
dfs(0, -1)
print(*ans)

Submission status: Runtime Error

Solution code:
import copy

N, Q = map(int, input().split())
graph = [[] for i in range(N)]

def dfs(v, d, p_list=[0]):
    seen[v] = True
    depth[v] = d
    parents_list[v] = copy.copy(p_list)
    for i in graph[v]:
        if seen[i] == True:
            continue
        p_list.append(i)
        dfs(i, d+1, p_list)
        p_list.pop()

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

seen = [False for i in range(N)]
depth = [0 for i in range(N)]
parents_list = [[] for i in range(N)]
dfs(0, 0)

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

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

    for j, parent in enumerate(parents_list):
        if p in parent:
            ans[j] += x

print(*ans)

Submission status: Runtime Error

Solution code:

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

# visited = [False for _ in range(N)]
# def dfs(v): # vertices & parent idx
#     visited[v]=True
#     for child in to[v]:
#         if visited[child]==True:
#             continue
#         ans[child]+=ans[v]
#         dfs(child)
def dfs(v,p=-1): # vertices & parent idx
    for child in to[v]:
        if child==p:
            continue
        ans[child]+=ans[v]
        dfs(child,v)
        


to=[[] for j in range(N)] # N points
ans=[0]*N # N points

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

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

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

Submission status: Runtime Error

Solution code:
import copy 
import sys

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

V = []
V_point = [0]*N
seen = []
seen = [False]*N
seen_copy = copy.deepcopy(seen)
P = []

for _ in range(N):
    V.append(set())

for _ in range(N-1):
    *l, = map(int, input().split())
    V[l[0]-1].add(l[1]-1)

for _ in range(Q):
    *l, = map(int, input().split())
    P.append([l[0], l[1]])

def dfs(node, point):
    if seen[node]==True:
        return
    seen[node]=True
    V_point[node] += point
    if len(V[node])==0:
        return
    for next_node in V[node]:
        dfs(next_node, point)

for p,x in P:
    dfs(p-1,x)
    seen = copy.deepcopy(seen_copy)

print(*V_point)

Submission status: Runtime Error

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

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

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

def dfs(v, p=-1):
    for i in to[v]:
        if i == p: continue
        ans[i] += ans[v]
        dfs(i, v)
        
dfs(0)        
print(' '.join([str(x) for x in ans[0:]]))

Submission status: Runtime Error

Solution code:
#AtCorder用　自動木取得コード

#１行目がノードの数、２行目以降が２つの数字でノードを繋ぐ辺を表すとき、使える

#変数
#node_n :ノードの数
#edge   :エッジを、入力の順番でリストとして提供
#【注意】ノードクラスのリストは関数で取り出さないと、連動して変わってしまう


#関数
#・getroot()で根を取り出す
#・getleaf()で葉を取り出す
#・childに子のノードクラスのリストが格納
#・parentに親のノードクラスが格納






#ノードクラス
class Node():
    def __init__(self):
        self.name=-1
        self.parent=-1
        self.child=[]
        self.counter=0
        
        
        
        
    #ノードに対する子供の数を取得
    def childnum(self):
        return len(self.child)

    

#木クラス
class Tree():
    
    
    #作成した瞬間、入力の数だけノードを作る。
    def __init__(self,n):
        self.tree=[]
        self.node_n=n
        for i in range(self.node_n):
            a=Node()
            a.name=i
            self.tree.append(a)
        self.edge=[]
        for i in range(self.node_n-1):
            self.edge.append([int(j) for j in input().split()])
        checkedge=deque()
        for edge in self.edge:
            first=self.tree[edge[0]-1]
            second=self.tree[edge[1]-1]
            if first.parent!=-1 or first.name==0:
                first.child.append(second)
                second.parent=first
            elif second.parent!=-1:
                second.child.append(first)
                first.parent=second
            else:
                checkedge.append(edge)
        while checkedge:
            edge=checkedge.popleft()
            first=self.tree[edge[0]-1]
            second=self.tree[edge[1]-1]
            if first.parent!=-1 or first.name==0:
                first.child.append(second)
                second.parent=first
            elif second.parent!=-1:
                second.child.append(first)
                first.parent=second
            else:
                checkedge.append(edge)
       
     
    #根ノードを取得
    def getroot(self):
        return self.tree[0]
    
    #葉ノードのリストを取得
    def getleaf(self):
        leaf=[]
        for i in self.tree:
            if i.child==[]:
                leaf.append(i)
        return leaf
    
    
    #全ノードリストを取得
    def gettree(self):
        return self.tree[:]
    
    
n,q=[int(i) for i in input().split()]
tree=Tree(n)
nodes=tree.tree
opelist=[]
for i in range(q):
    opelist.append([int(j) for j in input().split()])



for ope in opelist:
    nodes[ope[0]-1].counter+=ope[1]

    

node=tree.getroot()

def dpm(node):
    children=node.child
    if children!=[]:
        for child in children:
            child.counter+=node.counter
            dpm(child)
            
dpm(node)

for i in range(n):
    if i==n-1:
        print(nodes[i].counter)
    else:
        print(nodes[i].counter,end=(" "))

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

  idx = [n[-1] for n in tree_list].index(a)
  tree_list[idx].append(b)
  tree_list.append(tree_list[idx][0:-1])

zero_list = np.array([[0]*len(tree_list[j]) for j in range(len(tree_list))])

for k in range(Q):
  p, q = map(int, input().split())
  for j, tree_val in enumerate(tree_list):
    for l in range(len(tree_val)):
      if tree_val[l] == p:
        zero_list[j] += np.array([0]*(l) + [q]*(len(tree_val)-l))
        continue

key = sum(tree_list, [])
# val = sum([i for i in zero_list], [])
try:
  val = sum([i.tolist() for i in zero_list], [])
except AttributeError:
  val = sum(zero_list, [])
tree = sorted(dict(zip(key, val)).items(), key=lambda x:x[0])

for vals in tree:
  print(vals[1], end=' ')

Submission status: Runtime Error

Solution code:
import numpy as np

N, Q = map(int, input().split())
tree_list = [[1]]

for i in range(N-1):
  a, b = map(int, input().split())
  # if a in [n[-1] for n in tree_list]:
  idx = [n[-1] for n in tree_list].index(a)
  tree_list[idx].append(b)
  tree_list.append(tree_list[idx][0:-1])

zero_list = np.array([[0]*len(tree_list[j]) for j in range(len(tree_list))])

for k in range(Q):
  p, q = map(int, input().split())
  for j, tree_val in enumerate(tree_list):
    for l in range(len(tree_val)):
      if tree_val[l] == p:
        zero_list[j] += np.array([0]*(l) + [q]*(len(tree_val)-l))
        continue

key = sum(tree_list, [])
val = sum([i.tolist() for i in zero_list], [])
tree = sorted(dict(zip(key, val)).items(), key=lambda x:x[0])

for vals in tree:
  print(vals[1], end=' ')

Submission status: Runtime Error

Solution code:
N, Q, *D = map(int, open(0).read().split())
val = [0 for _ in range(N+2)]
E = [[] for _ in range(N+2)]
D = list(zip(*[iter(D)]*2))
assert(len(D) == N+Q-1)
for i in range(N+Q-1):
  l, r = D[i][0], D[i][1]
  if i < N - 1:
  	E[l].append(r)
  	E[r].append(l)
  else:
    val[l] += r
visited = [False for _ in range(N+1)]
def dfs(c):
  visited[c] = True
  for ch in E[c]:
    if not visited[ch]:
      val[ch] += val[c]
      dfs(ch)
dfs(1)
print(' '.join(map(str, val[1:])))

Submission status: Runtime Error

Solution code:
import sys

#N,Q = list(map(int,input().split()))
N,Q = [int(i) for i in sys.stdin.readline().split()]
G=[]
for i in range(N):
    G.append([])

for i in range(N-1):
    #a,b = list(map(int,input().split()))
    a,b = [int(j) for j in sys.stdin.readline().split()]
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)

p=[0]*Q
x=[0]*Q
c=[0]*N

for i in range(Q):
    #p,x = list(map(int,input().split()))
    p,x = [int(j) for j in sys.stdin.readline().split()]
    p-=1
    c[p] += x

stack=deque([0])
parent = [-1]*N

while(stack):
    v = stack.pop()
    for u in G[v]:
        if u!=parent[v]:
            c[u]+=c[v]
            parent[u]=v
            stack.append(u)


ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
#再帰を使って各ノードのカウンタの値を決定
def dfs(v,p,G,c):#vは0始まり
    for u in G[v]:
        if u!=p:
            c[u]+=c[v]
            dfs(u,v,G,c) 

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

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

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

dfs(0,-1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
#再帰を使って各ノードのカウンタの値を決定
def dfs(v,p,G,c):#vは1始まり
    for u in G[v]:
        if u!=p:
            c[u-1]+=c[v-1]
            dfs(u,v,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])
    G[b[i]].append(a[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

dfs(1,-1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    for u in G[v]:
        print(u)
        c[u-1]+=c[v-1]
        decide_count(u,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    for u in G[v]:
        c[u-1]+=c[v-1]
        uu=u
        decide_count(uu,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
#再帰を使って各ノードのカウンタの値を決定
def decide_count(v,G,c):#vは1始まり
    if len(G[v])==0:
        return
    for u in G[v]:
        c[u-1]+=c[v-1]
        decide_count(u,G,c) 

N,Q = list(map(int,input().split()))
a=[0]*(N-1)
b=[0]*(N-1)
G={}
for i in range(N-1):
    a[i],b[i] = list(map(int,input().split()))
for i in range(N):
    G[i+1]=[]
p=[0]*Q
x=[0]*Q
for i in range(Q):
    p[i],x[i] = list(map(int,input().split()))

#グラフの作成
for i in range(N-1):
    G[a[i]].append(b[i])

c=[0]*N
for i in range(Q):
    c[p[i]-1] += x[i]

decide_count(1,G,c)

ans = ' '.join([str(x) for x in c])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,Q=map(int,input().split())
    c=[0]*N
    d={}
    for i in range(N-1):
        a,b=map(int,input().split())
        d[b-1]=a-1
    d2={}
    for i in range(Q):
        p,x=map(int,input().split())
        if p-1 in d2:
            d2[p-1]+=x
        else:
            d2[p-1]=x
    if 0 in d2:
        c[0]+=d2[0]
    for i in range(1,N):
        if i in d2:
            c[i]+=d2[i]
        c[i]+=c[d[i]]
    print(*c)
    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, Q, *D = map(int, open(0).read().split())
val = [0 for _ in range(N+1)]
E = [[] for _ in range(N+1)]
for i in range(N+Q-1):
  l, r = D[2*i], D[2*i+1]
  if i < N - 1:
  	E[l].append(r)
  	E[r].append(l)
  else:
    val[l] += r
visited = [False for _ in range(N+1)]
def dfs(c):
  visited[c] = True
  for ch in E[c]:
    if not visited[ch]:
      val[ch] += val[c]
      dfs(ch)
dfs(1)
print(' '.join(map(str, val[1:])))

Submission status: Runtime Error

Solution code:
N, Q, *D = map(int, open(0).read().split())
val = [0 for _ in range(N+1)]
E = [[] for _ in range(N+1)]
for i in range(N-1):
  l, r = D[2*i], D[2*i+1]
  E[l].append(r)
  E[r].append(l)
D = D[-2*Q:]
for i in range(Q):
  l, r = D[2*i], D[2*i+1]
  val[l] += r
visited = [False for _ in range(N+1)]
def dfs(c):
  visited[c] = True
  for ch in E[c]:
    if not visited[ch]:
      val[ch] += val[c]
      dfs(ch)
dfs(1)
print(' '.join(map(str, val[1:])))

Submission status: Runtime Error

Solution code:
N, Q, *D = map(int, open(0).read().split())
val = [0 for _ in range(N+1)]
child = [[] for _ in range(N+1)]
for i in range(N-1):
  l, r = D[2*i], D[2*i+1]
  child[l].append(r)
D = D[-2*Q:]
for i in range(Q):
  l, r = D[2*i], D[2*i+1]
  val[l] += r
def dfs(c):
  for ch in child[c]:
    val[ch] += val[c]
    dfs(ch)
dfs(1)
print(' '.join(map(str, val[1:])))

Submission status: Runtime Error

Solution code:
def main():
    N, Q = [int(i) for i in input().split()]
    E = [tuple(int(i) for i in input().split()) for _ in range(N - 1)]
    V = [set() for _ in range(N)]
    for a, b in E:
        V[a - 1].add(b - 1)
        V[b - 1].add(a - 1)
    O = [tuple(int(i) for i in input().split()) for _ in range(Q)]
    C = [0] * N
    for p, x in O:
        C[p - 1] += x
    def add(n, x):
        C[n] += x
        for c in V[n]:
            V[c].remove(n)
            add(c, C[n])
    add(0, 0)
    print(' '.join(str(c) for c in C))

main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
tree = [list() for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
c = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    subtree = tree[p-1].copy()
    c[p-1] += x

for i, subnode in enumerate(tree):
    for s in subnode:
        c[s] += c[i]

print(*c)

Submission status: Runtime Error

Solution code:
from collections import deque

n, q = map(int, input().split())
tree = [list() for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
c = [0] * n
deq = deque()
for i in range(q):
    p, x = map(int, input().split())
    subtree = tree[p-1].copy()
    c[p-1] += x
    while subtree:
        snode = subtree.pop()
        c[snode] += x
        subtree += tree[snode]
print(*c)

Submission status: Runtime Error

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

C = [0 for _ in range(N+1)]
for p, x in PX:
    C[p] += x


def rec(v, p=0):
    for t in T[v]:
        if t == p:
            continue
        C[t] += C[v]
        rec(t, v)


rec(1)
print(*C[1:])

Submission status: Runtime Error

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

#行先
C=[[]*i for i in range(N)]
#合計値
D=[0]*N
#足跡
E=[False]*N

for i in range(N-1):
  C[A[i][0]-1].append(A[i][1])
  C[A[i][1]-1].append(A[i][0])


def f(a,b):
  D[a-1]+=b
  E[a-1]=True
  x=len(C[a-1])
  if x>0:
    for k in C[a-1]:
      if a=!k:
        f(k,b)

for l in range(Q):
  f(B[l][0],B[l][1])
  
  
print(*D)