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:
Connection_table=[]
Int_list=[]

def add_int_to_subtree(top_number,add_number):
    Int_list[top_number-1]+=add_number
    if len(Connection_table[top_number-1])!=0:
        for branch in Connection_table[top_number-1]:
            add_int_to_subtree(branch, add_number)
#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)

#まずデータを入れるリスト作り

for n in range(N):
    Int_list.append(0)
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)
    b=int(b)
    Connection_table[a-1].append(b)

#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)
    x=int(x)
    add_int_to_subtree( p, x)

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

Submission status: Runtime Error

Solution code:
def add_int_to_subtree(connection_table,int_list,top_number,add_number):
#    print("len(int_list),top_number",len(int_list),top_number)
    int_list[top_number-1]+=add_number
    if len(connection_table[top_number-1])!=0:
        # print("len(connection_table[top_number-1])",len(connection_table[top_number-1]))
        for branch in connection_table[top_number-1]:
            add_int_to_subtree(connection_table,int_list,branch, add_number)
#最初のNとQの読み込み
N,Q=input().split(" ")
N=int(N)
Q=int(Q)

#まずデータを入れるリスト作り
Connection_table=[]
Int_list=[]
for n in range(N):
    Int_list.append(0)
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    a,b=input().split(" ")
    a=int(a)
    b=int(b)
    Connection_table[a-1].append(b)
# print("Connection_table",Connection_table)
#どんどん足していく
for q in range(Q):
    p,x=input().split(" ")
    p=int(p)
    x=int(x)
    add_int_to_subtree(Connection_table, Int_list, p, x)

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

Submission status: Runtime Error

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

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Runtime Error

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

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
#  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Runtime Error

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

#c=list(zip(a,b))
#c.sort()
#a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

#for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Runtime Error

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

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

#for i in range(N-1):
#  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Runtime Error

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

c=list(zip(a,b))
c.sort()
a,b=list(zip(*c))

K=[0 for i in range(N)]
for i in range(N-1):
  K[p[i]-1]+=q[i]
#print(K)

for i in range(N-1):
  K[b[i]-1]+=K[a[i]-1]
#print(K)

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

Submission status: Runtime Error

Solution code:
# abc138D - Ki
import numpy as np
N, Q = list(map(int,input().split()))
A = []
S = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
A = np.array(A)
def search(X,c):
    B = []
    for i in range(N-1):
        if X[i][0] == c:
            B.append(X[i][1])
    return(B)
for j in range(Q):
    x, p = list(map(int,input().split()))
    F = search(A,x)
    E = [x] + F
    while F != []:
        G = F
        F = []
        for i in G:
            F += search(A,i)
            E += F
    else:
        E = list(set(E))
        for i in E:
            S[i-1] += p
for i in N:
    print(S[i],end=" ")

Submission status: Runtime Error

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

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

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

point=[0]*N

beginpoint=[0]*N

def search(v,sum,parent):
  if v>=N:
    return
  else:
    herepoint=beginpoint[v]
    point[v]=sum+herepoint
    for j in range(len(tree[v])):
      if tree[v][j]==parent:
        return
      search(tree[v][j],point[v],v)

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

search(0,0,-1)
  
print(*point)

Submission status: Runtime Error

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

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

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

point=[0]*N

beginpoint=[0]*N

def search(v,sum):
  if v>=N:
    return
  else:
    herepoint=beginpoint[v]
    point[v]=sum+herepoint
    for j in range(len(tree[v])):
      search(tree[v][j],point[v])

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

search(0,0)
  
print(*point)

Submission status: Runtime Error

Solution code:
6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Submission status: Runtime Error

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

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

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

point=[0]*N

def search(p,add):
  if p>=N:
    return
  else:
    point[p]=point[p]+add
    for j in range(len(tree[p])):
      search(tree[p][j],add)

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

Submission status: Runtime Error

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

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

point=[0]*N

def search(p,add):
  if p>=N:
    return
  else:
    point[p]=point[p]+add
    for j in range(len(tree[p])):
      search(tree[p][j],add)

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

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
tree=[None]*N
for i in range(N-1):
  a,b=map(int,input().split())
  if tree[a-1]==None:
    tree[a-1]=[b-1]
  else:
    tree[a-1].append(b-1)
point=[0]*N

def search(p,add):
  if p>=N:
    return
  else:
    point[p]=point[p]+add
    if tree[p]!=None:
      for j in range(len(tree[p])):
        search(tree[p][j],add)

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

Submission status: Runtime Error

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

v = [[] for i in range(n)] # []のn個配列
for i in range(n-1):
  a,b = map(int,input().split())
  a -= 1 # listのindex参照をそのまま使いたいので頂点id(1から始まっている)を-1しておく
  b -= 1 # aと同様
  v[a].append(b)
  v[b].append(a)

f = [0]*n
for i in range(q):
  p,x = map(int,input().split())
  p -= 1 # aと同様
  f[p] += x

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の値に戻す

ans = [0]*n
d = [1]*n
d[0] = 0
ans[0] = f[0]
s = f[0]

dfs(0,d,s)
print(*ans) #ansの中身を展開

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
from collections import defaultdict
n,q = map(int,input().split())
direct_dict = defaultdict(list)
for i in range(n-1):
    a,b = map(int,input().split())
    direct_dict[a - 1].append(b - 1)
ans_list = [0 for i in range(n)]
node_point = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    node_point[p-1] += x
    
def get_point(pos, now):
    ans_list[pos] = now + node_point[pos]
    for next_pos in direct_dict[pos]:
        get_point(next_pos, now + node_point[pos])

get_point(0, 0)
for i in ans_list:
    print(str(i)+" ", end="")

Submission status: Runtime Error

Solution code:
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return
 
N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))

Submission status: Runtime Error

Solution code:
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]:
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return
 
N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    for i in inf[num]:
        if visit[i] == 0:
            score[i] += score[num]
            visit[i]  = 1
    for i in inf[num]:
        addp(i)
    return 0
visit[0] = 1
addp(0)
print(*score)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 0:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 1:
            continue
        else:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if visit[inf[num][0]] == 1 and len(inf[num]) == 1:
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 1:
            continue
        else:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Submission status: Runtime Error

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

tree = collections.defaultdict(list)
counter = {}
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    counter[a] = 0
    counter[b] = 0

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

    def add(p_, x_):
        counter[p_] += x_
        for v in tree[p_]:
            add(v, x_)

    add(p, x)

print(' '.join(str(v) for _, v in sorted(counter.items(), key=lambda x: x[0])))

Submission status: Runtime Error

Solution code:
def ct(pp, ls, xx, tct):
  tct[pp-1] += xx
  nodel = [i for i, x in enumerate(ls) if x == pp]:
  if bool(nodel):
    for j in nodel:
      ct(j, ls, xx, tct)
  return

N, Q = map(int, input().split())
nodeR = [0 for nn in range(N)]
for i in range(N):
  a, b = map(int, input().split())
  nodeR[b-1] = a
tree_ct = [0 for nn in range(N)]
for j in range(Q):
  p, x = map(int, input().split())
  ct(p, nodeR, x, tree_ct)
print(' '.join(map(str, tree_ct)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
connect = {a:[] for a in range(1,n+1)}
ans = [0]*n

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

def d(p, x):
    ans[p-1] += x
    for b in connect[p]:
        d(b, x)
        
for _ in range(q):
    p, x = map(int, input().split())
    d(p, x)
    
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = (int(x) for x in input().split())
A = []
B = []
P = []
X = []

for i in range(0,N-1):
    a, b =  (int(x) for x in input().split())
    A.append(a)
    B.append(b)
for i in Q:
    p, x =  (int(x) for x in input().split())
    P.append(p)
    X.append(x)

dic = {}
for i in range(0,N-1):
    dic[B[i]] = A[i]

C = [0] * (N-1)
k = 0
for i in P:
    C[i-1] = C[i-1] + X[k]
    k = k + 1

for i in range(1,N):
    C[i] += C[dic[i]-1]

print(*C)

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
import sys
input=sys.stdin.readline()
n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    
stack = [0]
seen = dict()
seen[0] = 1
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        if seen.get(i,-1) == -1:
            seen[i] = 1
            counter[i]+=counter[yy]
            stack.append(i)
            
print(*counter)

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################


'''from sys import stdin,setrecursionlimit
#from copy import deepcopy
 
setrecursionlimit(10**6)
input=stdin.readline'''

def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

dfs(0)

print(*counter)

'''stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)'''

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################




def dfs(p,father=-1):
    for i in tree[p]:
        if i == father:
            continue
        counter[i]+=counter[p]
        dfs(i,p)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

dfs(0)

print(*counter)

'''stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)'''

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################




def dfs(p,father=-1):
    for i in tree[p]:
        if i != father:
            counter[i]+=counter[p]
            dfs(i,p)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

dfs(0)

print(*counter)

'''stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)'''

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################




def dfs(tree,counter,p):
    for i in tree[p]:
        counter[i]+=counter[p]
        dfs(tree,counter,i)

n ,q = I0()
counter = [0]*n
tree = [[] for i in range(n)]
for i in range(n-1):
    a,b=I0()
    tree[a-1].append(b-1)


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    

dfs(tree,counter,0)


print(*counter)

'''stack = [1]
while stack:
    yy = stack.pop()
    for i in tree[yy]:
        counter[i-1]+=counter[yy-1]
        stack.append(i)'''

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
        else:
            continue
    return 0
add(0)
print(' '.join(map(str, point)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if len(inf[num]) == 1 and visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
    return 0
add(0)
print(' '.join(map(str, point)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if len(inf[num]) == 1 and visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
    return 0
add(0)
print(*point)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
def add(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        point[k] += point[num]
        add(k)
    return 0
     
add(0)
print(*point)

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)


def inc(tree,counter,p):
    if tree[p] == []:
        return
    for i in tree[p]:
        counter[i-1]+=counter[p-1]
        inc(tree,counter,i)

n ,q = I0()
counter = [0]*n
ver = [0]*n
for i in range(n-1):
    a,b=I0()
    tree[a].append(b)


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    ver[p-1]+=x

inc(tree,counter,1)

print(*counter)

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)


def inc(tree,counter,p,x):
    if tree[p] == []:
        return
    for i in tree[p]:
        counter[i-1]+=x
        inc(tree,counter,i,x)

n ,q = I0()
counter = [0]*n
for i in range(n-1):
    a,b=I0()
    tree[a].append(b)


for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    inc(tree,counter,p,x)

print(*counter)

Submission status: Runtime Error

Solution code:
I0 = lambda :map(int,input().split())
I1 = lambda :int(input())
I2 = lambda :list(map(int,input().split()))
#####################################################
from collections import defaultdict
tree = defaultdict(list)
def inc(tree,counter,p,x):
    for i in tree[p]:
        counter[i-1]+=x
        inc(tree,counter,i,x)


n ,q = I0()
counter = [0]*n
for i in range(n-1):
    a,b=I0()
    tree[a].append(b)

for i in range(q):
    p,x = I0()
    counter[p-1]+=x
    inc(tree,counter,p,x)

print(*counter)

Submission status: Runtime Error

Solution code:
import collections

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

t = collections.defaultdict(list)
tc = collections.defaultdict(int)
for _ in range(n - 1):
    a, b = map(int, input().strip().split())
    t[a].append(b)

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

    def add(p_):
        tc[p_] += x
        for v in t[p_]:
            add(v)

    add(p)

print(' '.join(str(v) for _, v in sorted(tc.items(), key=lambda x: x[0])))

Submission status: Runtime Error

Solution code:
N,Q = input().split()
N = int(N)
Q = int(Q)
b_a_dict = {}
p_x_dict = {}
#p_list = []
#x_list = []
point_list = [0] * N
#print(N)
#print(Q)
for i in range(N-1):
	a,b = input().split()
	a = int(a)
	b = int(b)
	#print(a)
	#print(b)
	b_a_dict[b] = a

value_list = b_a_dict.values()
#print(b_a_dict)
#print(b_list)

for v in range(Q):
	p,x = input().split()
	p = int(p)
	x = int(x)
	if p in p_x_dict.keys():
		p_x_dict[p] += x
	else:
		p_x_dict[p] = x
	#dict_keys = [key for key, value in b_a_dict.items() if value == int(p)]
	#print(dict_keys)

#print(b_a_dict)
#print(p_x_dict)

for m in range(N-1):
	if m == 0:
		point_list[m] = p_x_dict[m+1]
	else:
		point_list[m] = p_x_dict[m+1] + point_list[b_a_dict[m+1]]

#print(point_list)
for w in point_list:
	print(w, end=" ")

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
dic = {}
dic_score = {}

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

def add_tree(root, x):
    if root in dic:
        leef = dic[root]
        for l in leef:
            add_tree(l, x)
    dic_score[root] += x

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

print(' '.join(map(str,list(dic_score.values()))))

Submission status: Runtime Error

Solution code:
import sys
n,q=map(int,input().split())
tree=[list(map(int,sys.stdin.readline().split())) for i in range(n-1)]
#f1は使ったのかどうかを表すフラッグ
#f2はそれまでに出てきたものを表すフラッグ
f1=[0]*(n-1)
f2=[1]

c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
    
for i,t in enumerate(tree):
    for i,t in enumerate(tree):
        if f1[i]==0:
            if t[0] in f2:
                c[t[1]-1]+=c[t[0]-1]
                f1[i]=1
                f2.append(t[1])
            elif t[1] in f2:
                c[t[0]-1]+=c[t[1]-1]
                f1[i]=1
                f2.append(t[0])
	if f1!=[1]*(n-1):
        break              

for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

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

        if A not in tree:
            tree[A] = []

        if B not in tree:
            tree[B] = [A]
        else:
            tree[B].append(A)

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] += B
        
    pointList = [0] * (N + 1)
    for idx, val in enumerate(tree):
        for v in tree[val]:
            pointList[val] += pointList[v]
        pointList[val] += point[val]
            
    print(*pointList[1:])

Submission status: Runtime Error

Solution code:
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (int(list(tree.keys())[len(tree)-1]) + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(*pointTree)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(2 * 10**5) #再帰の回数制限を増やす
input = sys.stdin.readline


def add_num(i, total, parent=-1):
    """隣接リストを取り出して深さ優先探索
    自分より上層の total のカウント数と部分木の頂点としてのカウント数を足す"""
    for p in adj[i]:
        if p != parent:
            ans_lst[p] = total + tmp[p]
            add_num(p, total + tmp[p], i)
    return
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] for _ in range(N)] # 隣接リスト
tmp = [0] * N # 部分木の根としてのカウント数 
ans_lst = [0] * N # 答えのリスト

# 隣接リストの作成
for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    adj[b].append(a)

# ｔｍｐリストの作成
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x

ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, total)

print(*ans_lst, sep=' ')

Submission status: Runtime Error

Solution code:
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(*pointTree)

Submission status: Runtime Error

Solution code:
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = point[index] + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    point = [0] * (N + 1)
    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        point[A] = point[A] + B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(' '.join(map(str, pointTree)))

Submission status: Runtime Error

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

to = [[] for _ in range(N)]

counter = [0]*N

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

def dfs(p,x):
  counter[p] += x
  if flag[p]: return
  flag[p] = True
  for cmd in to[p]:
    if p < cmd: dfs(cmd,x)

for i in range(Q):
  flag = [False]*N
  p,x = map(int,input().split())
  dfs(p-1,x)

#print(to)

print(*counter)