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())
a,b = [0 for i in range(N-1)],[0 for i in range(N-1)]
edge = {}
for i in range(N-1):
	a[i],b[i]=map(int,input().split())
	edge[b[i]-1] = a[i]-1

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

ex = ""
for i in range(N):
	ex = ex + str(res[i]) + " "

print(ex)

Submission status: Runtime Error

Solution code:
# D - Ki
from collections import defaultdict

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

# 各頂点の親をメモ
parentDict = defaultdict(list)
for edge in edges:
    a = edge[0]
    b = edge[1]
    parentDict[a].append(b)

counterList = [0] * n
def counter(parent, cnt):
    counterList[parent - 1] += cnt
    for child in parentDict[parent]:
        counter(child, cnt)
    
for operation in operations:
    parent = operation[0]
    cnt = operation[1]
    counter(parent, cnt)

print(' '.join(map(str, counterList)))

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

n , q = map(int, sys.stdin.readline().split())

graph = defaultdict(list)
for i in range(n-1):
	x, y = map(int, sys.stdin.readline().split())
	graph[x].append(y)
	graph[y].append(x)

def dfs(src, p):
	global graph, lazy

	for neighbour in graph[src]:
		if neighbour != p:
			lazy[neighbour] += lazy[src]
			dfs(neighbour, src)

lazy = [0]*(n+1)
for _ in range(q):
	idx, val = map(int, sys.stdin.readline().split())
	lazy[idx] += val
dfs(1, -1)
print(*lazy[1:])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys

# n, q = 4, 3
# arr = [(1, 2), (3, 2), (2, 4)]

n , q = map(int, sys.stdin.readline().split())

graph = defaultdict(list)

for i in range(n-1):
	x, y = map(int, sys.stdin.readline().split())
	graph[x].append(y)
	graph[y].append(x)

def dfs(src, p):
	global graph, lazy

	for neighbour in graph[src]:
		if neighbour != p:
			lazy[neighbour] += lazy[src]
			dfs(neighbour, src)


lazy = [0]*(n+1)

for _ in range(q):
	idx, val = map(int, sys.stdin.readline().split())
	lazy[idx] += val
dfs(1, -1)


print(*lazy[1:])

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
tree = [set() for i in range(n+1)]
for _ in range(n-1):
  a,b = map(int, input().split())
  tree[a].add(b)
  tree[b].add(a)
lazy = [0]*(n+1)
ans = [0]*(n+1)
cnt = 0

for _ in range(q):
  x,v = map(int, input().split())
  lazy[x] += v

def dfs(v,p,tree):
  ans[v] += lazy[v]
  for x in tree[x]:
    if x == p:
      continue
    lazy[x] += lazy[v]
    dfs(x,v,tree)
  lazy[v] = 0
  return

dfs(1,0,tree)

print(" ".join(map(str,ans[1:])))

Submission status: Runtime Error

Solution code:
class Point:
    def __init__(self, id):
        self.id = id
        self.child_list = []
        self.count = 0
    
    def add_child(self, point):
        self.child_list.append(point)
    
    def countup(self, count):
        self.count += count
        for point in self.child_list:
            point.countup(count)

    def get_count(self):
        return self.count

N, Q = map(int, input().split())
point_list = []
for i in range(N):
    point_list.append(Point(i+1))

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

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

for point in point_list:
    print(point.get_count())

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
def add_to_subtree(tree, result_arr, idx, value):
  result_arr[idx] += value
  if len(tree[idx]) == 0:
    return
  for child in tree[idx]:
    add_to_subtree(tree, result_arr, child, value)

N, Q = map(int, input().split())
R = [0] * (N + 1)
T = [[] for _ in range(N + 1)]

for _ in range(N-1):
  a, b = sorted(map(int, input().split()))
  T[a].append(b)
for _ in range(Q):
  P, X = map(int, input().split())
  add_to_subtree(T, R, P, X)

print(*R[1:])

Submission status: Runtime Error

Solution code:
def add_to_subtree(tree, result_arr, idx, value):
  result_arr[idx] += value
  if len(tree[idx]) == 0:
    return
  for child in tree[idx]:
    add_to_subtree(tree, result_arr, child, value)

N, Q = map(int, input().split())
R = [0] * (N + 1)
T = [[] for _ in range(N + 1)]

for _ in range(N-1):
  a, b = sorted(map(int, input().split()))
  T[a].append(b)
for _ in range(Q):
  P, X = map(int, input().split())
  add_to_subtree(T, R, P, X)

print(*R[1:])

Submission status: Runtime Error

Solution code:
def add_to_subtree(tree, result_arr, idx, value):
  result_arr[idx] += value
  if len(tree[idx]) == 0:
    return
  for child in tree[idx]:
    add_to_subtree(tree, result_arr, child, value)

N, Q = map(int, input().split())
T = [[] for i in range(N + 1)]
R = [0] * (N + 1)
for _ in range(N-1):
  a, b = map(int, input().split())
  T[a].append(b)
for _ in range(Q):
  P, X = map(int, input().split())
  add_to_subtree(T, R, P, X)

print(" ".join(map(str, R[1:])))

Submission status: Runtime Error

Solution code:
nq=list(map(int,input().split()))
CountPj=[]
for i in range(nq[1]):
    CountPj.append(0)
ABi=[]
PXj=[]
for i in range(nq[0]):
    ab=list(map(int,input().split()))
    ABi.append(ab)
for i in range(nq[1]):
    px=list(map(int,input().split()))
    PXj.append(px)
#n=nq[0],q=nq[1]
#ai=(ABi[i-1])[0],bi=(ABi[i-1])[1]
#pj=(PXj[j-1])[0],xj=(PXj[j-1])[1]
#CounterPj=CountPj[j-1]

#あるpjに対して、カウンターにxjを加え、
#連結するplのlをリストアップする関数を作った
#これをリストが空になるまで繰り返せばよい
def f(j):
    CountPj[j-1]=CountPj[j-1]+(PXj[j-1])[1]
    l_list=[]
#pjと一致するaiを探す
    for i in range(1,nq[0]):
        if (PXj[j-1])[0]==(ABj[i-1])[0]:
#見つけたaiとセットのbiに1対1対応するplを探す
            for l in range(1,nq[1]+1):
#当然l=jのときは省く
                if l==j:
                    l=l
                else:
#例外の処理はしたので実際に探してplを探そう
                    if (ABj[i-1])[1]==(PXj[l-1])[0]:
                        l_list.append(l)
    return l_list

#ではjのループを作っていく
for j in range(1,nq[1]+1):
    L_LIST=[]
    L_LIST.append(j)
    a=len(L_LIST)
    while a>=1:
        lst=[]
        for i in range(a):
            lst+=f(L_LST[i])
        L_LST=lst
        a=len(L_LST)

print(*CountPj)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)
    tmpLst[j].append(i)

ansLst = [0] * (N + 1)
for i,j in pLst:
    ansLst[i] += j

def dfs(v, p, add):
    ansLst[v] += add
    for i in tmpLst[v]:
        if i == p:
            continue
        dfs(i, v, ansLst[v])


dfs(1, 0, 0)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)
    tmpLst[j].append(i)

ansLst = [0] * (N + 1)
for i,j in pLst:
    ansLst[i] += j

def dfs(v, p):
    for i in tmpLst[v]:
        if i == p:
            continue
        ansLst[i] += ansLst[v]
        dfs(i, v)


dfs(1, 0)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)
    tmpLst[j].append(i)

ansLst = [0] * (N + 1)
for i,j in pLst:
    ansLst[i] += j

def dfs(v, p, ansLst):
    for i in tmpLst[v]:
        if i == p:
            continue
        ansLst[i] += ansLst[v]
        dfs(i, v, ansLst)


dfs(1, 0, ansLst)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)
    tmpLst[j].append(i)

ansLst = [0] * N
for i,j in pLst:
    ansLst[i - 1] += j

def dfs(v, p, ansLst):
    for i in tmpLst[v]:
        if i == p:
            continue
        ansLst[i - 1] += ansLst[v - 1]
        dfs(i, v, ansLst)


dfs(1, 0, ansLst)

ans = ' '.join(map(str, ansLst))
print(ans)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)
    tmpLst[j].append(i)

ansLst = [0] * N
for i in range(Q):
    ansLst[pLst[i][0] - 1] += pLst[i][1]

def dfs(v, p, ansLst):
    for i in tmpLst[v]:
        if i == p:
            continue
        ansLst[i - 1] += ansLst[v - 1]
        dfs(i, v, ansLst)


dfs(1, 0, ansLst)

ans = ' '.join(map(str, ansLst))
print(ans)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)

ansLst= [0] * N
for i in range(Q):
    ansLst[pLst[i][0] - 1] += pLst[i][1]
def dfs(v, ansLst):
    for i in tmpLst[v]:
        ansLst[i - 1] += ansLst[v - 1]
        dfs(i, ansLst)


dfs(1, ansLst)

ans = ' '.join(map(str, ansLst))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, Q = map(int, input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a, b in AB:
  graph[a].append(b)
  graph[b].append(a)
  
value = [0]  * (N+1)
for p, x in PX:
  value[p] += x
  
def dfs(v, parent, add):
  value[v] += add
  for x in graph[v]:
    if x == parent:
      continue
    dfs(x, v, value[v])
dfs(1, 0, 0)    

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import numpy


class Node(object):
    def __init__(self):
        self.counter = 0
        self.parent = None

    def value(self):
        if self.parent is None:
            return self.counter
        else:
            return self.parent.value() + self.counter


N, Q = map(int, sys.stdin.readline().rstrip().split(" "))
nodes = [Node() for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, sys.stdin.readline().rstrip().split(" "))
    nodes[b - 1].parent = nodes[a - 1]
for _ in range(Q):
    p, x = map(int, sys.stdin.readline().rstrip().split(" "))
    nodes[p - 1].counter += x

print(" ".join(str(node.value()) for node in nodes))
exit(0)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
AB = [[0]*2 for i in range(N-1)]
for i in range(N-1):
  AB[i] = list(map(int, input().split()))
C = [0]*N
g = list([] for i in range(N+1))
for i in range(Q):
  p, x = map(int, input().split())
  C[p-1] += x

for i,j in AB:
  g[i].append(j)
  g[j].append(i)
 
def BTS(a, b):
  for i in g[a]:
    if i == b:
      continue
    C[i-1] += C[a-1]
    BTS(i,i)
    
BTS(1,-1)
 
print(*C)

Submission status: Runtime Error

Solution code:
import networkx as nx
n,q=map(int,input().split())

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

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

ans=[0 for i in range(n)]
for i in range(n):
    tmp=i
    flg=1
    cntans=0
    while(flg):
        cntans=cntans+cnt[tmp]
        if tmp==0:
            flg=0
        tmp=l[tmp]
    ans[i]=cntans
print(ans)

Submission status: Runtime Error

Solution code:
n, q = tuple([int(i) for i in input().split(' ')])

d = [{'c': 0, 'b': []} for _ in range(n + 1)]

for i in range(1, n):
    a, b = tuple([int(i) for i in input().split(' ')])
    d[a]['b'].append(b)

def solve(p, x):
    d[p]['c'] += x
    if len(d[p]['b']) == 0:
        return
    for e in d[p]['b']:
        solve(e, x)

for i in range(q):
    p, x = tuple([int(i) for i in input().split(' ')])
    solve(p, x)

print(' '.join([str(i['c']) for i in d[1:]]))

Submission status: Runtime Error

Solution code:
import numpy as np

def f(lis,l):
  if len(l)!=0:
    lis.extend(l)
    for i in range(len(l)):
      f(lis,a[l[i]-1])
                   
n,q=list(map(int,input().split()))

a=[]
for i in range(n):
  a.append([])
b=np.zeros(n)

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

for j in range(q):
  tmp=list(map(int,input().split()))
  b[tmp[0]-1] += tmp[1]
  lis=[]
  f(lis,a[tmp[0]-1])
  lis=list(set(lis))
  for k in range(len(lis)):
    b[lis[k]-1] += tmp[1]
  
for i in range(n):
  print(int(b[i]),end=" ")

Submission status: Runtime Error

Solution code:
import numpy as np

def f(l,x):
  if len(l)!=0:
    for i in range(len(l)):
      b[l[i]-1] += x
      f(a[l[i]-1],x)
                   
n,q=list(map(int,input().split()))

a=[]
for i in range(n):
  a.append([])
b=np.zeros(n)

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

for j in range(q):
  tmp=list(map(int,input().split()))
  b[tmp[0]-1] += tmp[1]
  f(a[tmp[0]-1],tmp[1])
  
for i in range(n):
  print(int(b[i]),end=" ")

Submission status: Runtime Error

Solution code:
import numpy as np

def f(l,x):
  if len(l)!=0:
    for i in range(len(l)):
      b[l[i]-1] += x
      f(a[l[i]-1],x)
                   
n,q=list(map(int,input().split()))

a=[]
for i in range(n):
  a.append([])
b=np.zeros(n)

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

for j in range(q):
  tmp=list(map(int,input().split()))
  b[tmp[0]-1] += tmp[1]
  f(a[tmp[0]-1],tmp[1])
  
print(b)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline


def main():
    N, Q = map(int, input().split())
    ans = [[0, deque()] for _ in range(N)]

    for _ in range(N - 1):
        p, c = map(int, input().split())
        ans[p-1][1].append(c-1)
        ans[c-1][1].append(p-1)

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

    def add_value(node, parent):
        val = ans[node][0]
        children = ans[node][1]
        for child in children:
            if child == parent:
                continue
            ans[child][0] += val
            add_value(child, node)

    add_value(0, -1)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

ans = [0] * n


def f(par, node, count):
    count += counter[node]
    ans[node] = count
    for n in edges[node]:
        if n == par:
            continue
        f(node, n, count)


f(-1, 0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline


def main():
    N, Q = map(int, input().split())
    ans = [0] * N
    con = [[0, deque()] for _ in range(N)]

    for _ in range(N - 1):
        p, c = map(int, input().split())
        con[p-1][1].append(c-1)

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

    def add_value(node):
        val = con[node][0]
        for child in con[node][1]:
            con[child][0] += val
            add_value(child)

    add_value(0)

    print(' '.join([str(x[0]) for x in con]))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

ans = ['0'] * n


def f(node, count):
    count += counter[node]
    ans[node] = str(count)
    while len(edges[node]) > 0:
        n = edges[node].pop()
        edges[n].remove(node)
        f(n, count)


f(0, 0)
print(' '.join(ans))

Submission status: Runtime Error

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

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

s=[0,]*n

def calc(dd,i,num):
  global s
  if dd[i]==[]:
    s[i-1]+=num
    return(True)
  else:
    s[i-1]+=num
    for item in dd[i]:
      calc(dd,item,num)
    return(True)


dd=defaultdict(list)
for key in range(1,n):
  dd[ab[key-1][0]].append(ab[key-1][1])

for item in px:
  calc(dd,item[0],item[1])

print(*s)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

s=[0,]*n

def calc(dd,i,num):
  global s
  if dd[i]==[]:
    s[i-1]+=num
    return(True)
  else:
    s[i-1]+=num
    for item in dd[i]:
      calc(dd,item,num)
    return(True)


dd=defaultdict(list)
for key in range(1,n):
  dd[ab[key-1][0]].append(ab[key-1][1])

for item in px:
  calc(dd,item[0],item[1])

print(*s)

Submission status: Runtime Error

Solution code:
from collections import deque
from collections import defaultdict
N, Q = map(int, input().split())
C = [0]* N
D = defaultdict(set)
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].add(b-1)
  D[b-1].add(a-1)
for i in range(Q):
  p, x = map(int, input().split())
  C[p-1] += x

def dfs(n, vstd, x):
  C[n] += x
  vstd.append(n)
  for i in D[n]:
    if i in vstd:
      continue
    dfs(i, vstd, C[n])

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

Submission status: Runtime Error

Solution code:
from collections import deque
from collections import defaultdict
N, Q = map(int, input().split())
C = defaultdict(int)
D = defaultdict(set)
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].add(b-1)
  D[b-1].add(a-1)
for i in range(Q):
  p, x = map(int, input().split())
  C[p-1] = x

def dfs(n, vstd, x):
  C[n] += x
  vstd.append(n)
  for i in D[n]:
    if i in vstd:
      continue
    dfs(i, vstd, C[n])

dfs(0, [0], 0)    
print (" ".join(map(str, C.values())))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N, Q = map(int, input().split())
C = defaultdict(int)
D = defaultdict(set)
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].add(b-1)
  
def AddPX(n, x):
  C[n] += x
  for i in D[n]:
    AddPX(i, x)
  
for i in range(Q):
  p, x = map(int, input().split())
  AddPX(p-1, x)
print (" ".join(map(str, C.values())))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N, Q = map(int, input().split())
C = [0] * (N+1)
D = defaultdict(set)
for i in range(N-1):
  a, b = map(int, input().split())
  D[a].add(b)
  
def AddPX(n, x):
  C[n] += x
  for i in D[n]:
    AddPX(i, x)
  
for i in range(Q):
  p, x = map(int, input().split())
  AddPX(p, x)

print (" ".join(map(str, C[1:])))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N,Q = map(int,input().split())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
PX = []
for _ in range(Q):
    PX.append([int(i) for i in input().split()])
class Tree:
    def __init__(self,n):
        self.tree = defaultdict(list)
        self.score = [0]*n
    def add_edge(self,n1,n2):
        self.tree[n1].append(n2)
    def calc_score(self,n,s):
        self.score[n-1]+=s
        if self.tree[n] == []:
            pass
        else:
            for n in self.tree[n]:
                self.calc_score(n,s)
tree = Tree(N)
for ab in AB:
    tree.add_edge(ab[0],ab[1])
for px in PX:
    tree.calc_score(px[0],px[1])
print(*tree.score)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
ab = []
a=[]
for n in range(N-1):
    ab.append([int(i) for i in input().split()])
px = []
for q in range(Q):
    px.append([int(i) for i in input().split()])
class Graph():
    def __init__(self):
        """ ノードのつながりを辞書型で表現する """
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        """ ノードを追加する """
        self.adjacency_dict[v] = []
    def add_edge(self, v1, v2):
        """ ノード同士をつなぐ。"""
        # 無向グラフの場合は双方向。もし有向グラフなら片側のみ。
        self.adjacency_dict[v1].append(v2)    
    def print_graph(self):
        print(self.adjacency_dict)
graph = Graph()
for n in range(N-1):
    if graph.adjacency_dict.get(ab[n][0]):
        pass
    else:
        graph.add_vertex(ab[n][0])
    graph.add_edge(ab[n][0],ab[n][1])
def search(graph,j,ret):
    if graph.get(j):
        for k in graph[j]:
            ret.append(k)
            search(graph,k,ret)
    return ret
values = [0]* N
for q in range(Q):
    add_list = search(graph.adjacency_dict,px[q][0],[px[q][0]])
    for add in add_list:
        values[add-1] += px[q][1]
print(' '.join(map(str, values)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N,Q = map(int,input().split())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
PX = []
for _ in range(Q):
    PX.append([int(i) for i in input().split()])
class Tree:
    def __init__(self,n):
        self.tree = defaultdict(list)
        self.score = [0]*n
    def add_edge(self,n1,n2):
        self.tree[n1].append(n2)
    def calc_score(self,n,s):
        self.score[n-1]+=s
        if self.tree[n] == []:
            pass
        else:
            for n in self.tree[n]:
                self.calc_score(n,s)
tree = Tree(N)
for ab in AB:
    tree.add_edge(ab[0],ab[1])
for px in PX:
    tree.calc_score(px[0],px[1])
print(*tree.score)

Submission status: Runtime Error

Solution code:
class Leef(object):
	tree = {}

	def __init__(self):
		self.children = []
		self.counter = 0

	def addCount(self, n):
		self.counter += n
		for c in self.children:
			Leef.tree[c].addCount(n)

N, Q = map(int, input().split())
for n in range(1,N):
	a, b = map(int, input().split())
	if a not in Leef.tree:
		leef = Leef()
		leef.children.append(b)
		Leef.tree[a] = leef
	else:
		Leef.tree[a].children.append(b)

	if b not in Leef.tree:
		leef = Leef()
		Leef.tree[b] = leef

for q in range(Q):
	p, x = map(int, input().split())
	Leef.tree[p].addCount(x)

sorted_tree = sorted(Leef.tree.items())
res_counter = list(map(lambda l:str(l[1].counter), sorted_tree))
print(' '.join(res_counter))

Submission status: Runtime Error

Solution code:
class Node():
  def __init__(self, num):
    self.num = num
    self.count = 0
    self.childs = []
  def add_child(self, node):
    self.childs.append(node)
  def counter(self, n):
    self.count += n
    for node in self.childs:
      node.counter(n)
  def __repr__(self):
    return str(self.num)
  def __str__(self):
    return str(self.num)

N, Q = list(map(int, input().split()))
node_list = [Node(i) for i in range(1, N+1)]
for i in range(0, N-1):
  a, b = list(map(int, input().split()))
  node_list[a-1].add_child(node_list[b-1])
for i in range(0, Q):
  p, x = list(map(int, input().split()))
  node_list[p-1].counter(x)
res=""
for node in node_list:
  res += "{} ".format(node.count)
print(res)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, Q = map(int,input().split())
edge_from = defaultdict(list)
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    edge_from[a].append(b)
tmp = [0]*N
for i in range(Q):
    p, x = map(int,input().split())
    tmp[p-1] += x
q = [None]*N
head = 0
tail = 1
q[0] = (0, 0)
while head < N:
    p, x = q[head]
    x += tmp[p]
    tmp[p] = x
    head += 1
    for nx in edge_from[p]:
        q[tail] = (nx, x)
        tail += 1
print(*tmp)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline
#sys.setrecursionlimit(10 ** 6)

N, Q = map(int, input().split())
V = {i: [] for i in range(1, N + 1)}
for _ in range(N - 1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[b].append(a)

C = {i: 0 for i in range(1, N + 1)}

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


def propagate(u, p):
    for v in V[u]:
        if v == p:
            continue
        C[v] += C[u]
        propagate(v, u)


propagate(1, 0)

print(' '.join(str(C[i]) for i in range(1, N + 1)))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)

def plus(node, _from):
    #cost_ls[node] += num
    for child in tree[node]:
        if child == _from:
            continue
        cost_ls[child] += cost_ls[node]
        plus(child, node)

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

plus(1, 0)

print(' '.join(str(cost_ls[i]) for i in range(1,N+1)))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for _ in range(N-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)

yet = set()
def plus(node, _from):
    yet.add(node)
    #cost_ls[node] += num
    for child in tree[node]:
        if child == _from:
            continue
        cost_ls[child] += cost_ls[node]
        plus(child, node)

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

plus(1, 0)

print(' '.join(str(cost_ls[i]) for i in range(1,N+1)))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for i in range(N-1):
    a, b = [int(j) for j in sys.stdin.readline().split()]
    tree[a].append(b)
    tree[b].append(a)

yet = set()
def plus(node, _from):
    yet.add(node)
    #cost_ls[node] += num
    for child in tree[node]:
        if child == _from:
            continue
        cost_ls[child] += cost_ls[node]
        plus(child, node)

for i in range(Q):
    p, x = [int(j) for j in sys.stdin.readline().split()]
    cost_ls[p] += x

plus(1, 0)

print(' '.join(str(cost_ls[i]) for i in range(1,N+1)))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for i in range(N-1):
    a, b = [int(j) for j in sys.stdin.readline().split()]
    tree[a].append(b)
    tree[b].append(a)

yet = set()
def plus(node, _from):
    yet.add(node)
    #cost_ls[node] += num
    for child in tree[node]:
        if child == _from:
            continue
        cost_ls[child] += cost_ls[node]
        plus(child, node)

for i in range(Q):
    p, x = [int(j) for j in sys.stdin.readline().split()]
    cost_ls[p] += x

plus(1, 0)

print(" ".join([str(cost) for cost in cost_ls.values()]))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for i in range(N-1):
    a, b = [int(j) for j in sys.stdin.readline().split()]
    tree[a].append(b)
    tree[b].append(a)

yet = set()
def plus(node, _from):
    yet.add(node)
    #cost_ls[node] += num
    if len(tree[node]) > 0:
        for child in tree[node]:
            if child == _from:
                continue
            cost_ls[child] += cost_ls[node]
            plus(child, node)

for i in range(Q):
    p, x = [int(j) for j in sys.stdin.readline().split()]
    cost_ls[p] += x

plus(1, 0)

print(" ".join([str(cost) for cost in cost_ls.values()]))

Submission status: Runtime Error

Solution code:
import sys
N, Q = [int(i) for i in input().split()]
tree = {i:[] for i in range(1,N+1)}
cost_ls = {i:0 for i in range(1,N+1)}
for i in range(N-1):
    a, b = [int(j) for j in sys.stdin.readline().split()]
    tree[a].append(b)
    tree[b].append(a)

yet = set()
def plus(node, _from):
    yet.add(node)
    #cost_ls[node] += num
    if len(tree[node]) > 0:
        for child in tree[node]:
            if child in yet:
                continue
            cost_ls[child] += cost_ls[node]
            plus(child, node)

for i in range(Q):
    p, x = [int(j) for j in sys.stdin.readline().split()]
    cost_ls[p] += x

plus(1, 0)

print(" ".join([str(cost) for cost in cost_ls.values()]))