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:
class RootedTree():
    def __init__(self, N, root = 1):
        # N is the number of nodes
        # self.nodes is a list of TreeNode
        self.nodes = [None] * (N + 1) # self.nodes[0] is kept None
        self.root = root
        self.N = N # number of nodes

    def add_node(self, tree_node, num):
        self.nodes[num] = tree_node

    def add_val(self, p, x, Q):
        for j in range(1, Q + 1):
            self._add_val(p[j], x[j])

    def _add_val(self, p_val, x_val):
        self.nodes[p_val].increment_counter(x_val)
        for ch in self.nodes[p_val].children:
            self._add_val(ch, x_val)

    def print_counter(self):
        for i in range(1, N):
            print("{} ".format(self.nodes[i].counter), end = "")
        print("{} ".format(self.nodes[N].counter))

class TreeNode():
    def __init__(self, num):
        self.number = num  # integer (vertex number)
        self.counter = 0
        self.children = set()

    def add_child(self, v):
        self.children.add(v)

    def increment_counter(self, x):
        self.counter += x

class Graph():
    def __init__(self, N):
        self.AL = [None] + [set() for i in range(N)] # adjacency list
        self.N = N # number ov vertices

    def add_edge(self, u, v):
        self.AL[u].add(v)
        self.AL[v].add(u)

    def to_rooted_tree(self):
        rooted_tree = RootedTree(self.N, 1)
        self._to_rooted_tree(1, {1}, rooted_tree)
        return rooted_tree

    def _to_rooted_tree(self, v, visited, rooted_tree):
        node = TreeNode(v)
        rooted_tree.add_node(node, v)

        for u in self.AL[v]: # leaf has only one neighbour (parent)
            if not u in visited:
                visited.add(u)
                node.add_child(u)
                self._to_rooted_tree(u, visited, rooted_tree)


# input
N, Q = [int(k) for k in input().split()]
a = [None] * N # a[0] = None
b = [None] * N # b[0] = None
for i in range(1, N):
    a[i], b[i] = [int(k) for k in input().split()]
p = [None] * (Q + 1)
x = [None] * (Q + 1)
for i in range(1, Q + 1):
    p[i], x[i] = [int(k) for k in input().split()]

# create a graph
G = Graph(N)
for i in range(1, N):
    G.add_edge(a[i], b[i])

# run depth first search to form a rooted tree
T = G.to_rooted_tree()


T.add_val(p, x, Q)
T.print_counter()

Submission status: Runtime Error

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

tree = [[0] * 1 for i in range(N)]


tree_list = [0] * N
end = [0] * N

for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)
    tree[b].append(a)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    end[node] = -1
    child = tree[node]
    for c in child:
        if end[c] == 0:
            tree_list[c] += tree_list[node]
            iter(c)


iter(0)

print(*tree_list)

Submission status: Runtime Error

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

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


tree_list = [0] * N
end = [0] * N

for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)
    tree[b].append(a)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    end[node] = -1
    child = tree[node]
    for c in child:
        if end[c] == 0:
            tree_list[c] += tree_list[node]
            iter(c)


iter(0)

print(*tree_list)

Submission status: Runtime Error

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

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


tree_list = [0] * N
end = [0] * N

for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)
    tree[b].append(a)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    end[node] = -1
    child = tree[node]
    for c in child:
        if end[c] == 0:
            tree_list[c] += tree_list[node]
            iter(c)


iter(0)

print(*tree_list)

Submission status: Runtime Error

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

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


tree_list = [0] * N
for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    child = tree[node]
    for c in child:
        tree_list[c] += tree_list[node]
        iter(c)


iter(0)

print(*tree_list)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
graph=[[] for i in range(n)]
visited=[False]*n
visited[0]=True
from collection import deque
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1;b-=1
    graph[a].append(b)
    graph[b].append(a)

s=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    s[p-1]+=x
quest=deque([0])#はじめどうせ見るし入れとく
while len(quest)!=0:
    see=quest.popleft()
    for i in graph[see]:
        if visited[i]==True:
            continue
        quest.append(i)
        visited[i]=True
        s[i]+=s[see]
#for i in range(n):
    #print(s[i],end=" ")
print(*s)

Submission status: Runtime Error

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

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

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

# print(nodes)
result = [0 for _ in range(n + 1)]
def dfs(i=1, cost=nodes[1]):
    result[i] = cost
    for sub in graph[i]:
        dfs(sub, cost + nodes[sub])
dfs()
print(*result[1:])

Submission status: Runtime Error

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

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

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

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

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

def dfs(n, r):
    for i in T[n]:
        if i != r:
            ans[i] += ans[n]
            dfs(i, n)

dfs(0, -1)

print(*ans)

Submission status: Runtime Error

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

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

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

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

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

def dfs(n, r):
    for i in T[n]:
        if i != r:
            O[i] += O[n]
            dfs(i, n)

dfs(0, -1)

print(*ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

dic_score={}
tree_list=[]
for i in range(N+1):
  tree_list.append([])
ans_list=[0]*(N+1)

def dfs(v,s):
  global dic_score
  global tree_list
  global ans_list
  
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)
    
  return s

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

dfs(1,0)

#print(tree_list)
#print(ans_list)
ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

dic_score={}
tree_list=[]
for i in range(N+1):
  tree_list.append([])
ans_list=[0]*(N+1)

def dfs(v,s):
  global dic_score
  global tree_list
  global ans_list
  
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

dfs(1,0)

#print(tree_list)
#print(ans_list)
ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def dfs(v,s):
  global dic_score
  global tree_list
  global ans_list
  
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)
ans_list=[0]*(N+1)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)
ans_list=[0]*(N+1)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)  
ans_list=[0]*(N+1)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    #dfs(vv,score)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)  
ans_list=[0]*(N+1)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  #ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)  
ans_list=[0]*(N+1)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

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

tree_list=[]
for i in range(N+1):
  tree_list.append([])

for i in range(N-1):
  a,b=map(int,input().split())
  tree_list[a].append(b)
  
dic_score={}
for i in range(Q):
  p,x=map(int,input().split())
  if p in dic_score:
    dic_score[p]+=x
  else:
    dic_score[p]=x

#print(tree_list)  
ans_list=[0]*(N+1)

def dfs(v,s):
  score=s
  if v in dic_score:
    score+=dic_score[v]
    
  ans_list[v]+=score
  for vv in tree_list[v]:
    dfs(vv,score)

dfs(1,0)
#print(ans_list)

ans_list_str=[]
for i in range(1,N+1):
  ans_list_str.append(str(ans_list[i]))

print(" ".join(ans_list_str))

Submission status: Runtime Error

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

cnt = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    cnt[p-1] += x
 
 
def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)
 
 
dfs(1, -1)
print(*cnt)

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[now-1]
      dfs(nex, now)
dfs(1, -1)
print(*point)

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[now-1]
      dfs(nex, now)
dfs(1, -1)
print(*point)

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[now-1]
      dfs(nex, now)
dfs(0, -1)
print(' '.join(str(s) for s in point))

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[bef-1]
      dfs(nex, now)
dfs(1, -1)
print(' '.join(str(s) for s in point))

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[bef-1]
      dfs(nex, now)
dfs(1, -1)
print(' '.join(str(s) for s in point))

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for nex in edge[now-1]:
    if nex != bef:
      point[nex-1] += point[bef-1]
      dfs(nex, now)
dfs(1, -1)
print(' '.join(str(s) for s in point)

Submission status: Runtime Error

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

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

def dfs(now, bef):
  for next in edge[now-1]:
    if next != bef:
      point[next-1] += point[bef-1]
      dfs(next, now)
dfs(1, -1)
print(' '.join(str(s) for s in point)

Submission status: Runtime Error

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

n,q = map(int,input().split())
t = [[] for _ in range(n+1)]
for _ in range(n-1):
  a,b = map(int,input().split())
  t[a].append(b)
  t[b].append(a)
cnt = [0]*(n+1)
for _ in range(q):
  p,x = map(int,input().split())
  cnt[p] += x
def csum(now,bef):
  for i in t[now]:
    if i != bef:
      cnt[i] += cnt[now]
      csum(i,now)
csum(1,0)
for i in range(1,n+1):
  print(cnt[i],end=" ")

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
t = [[] for i in range(n)]# 空の2次元配列を作る
for i in range(n-1):
    a, b = map(int, input().split())
    t[a - 1].append(b -1)
    t[b - 1].append(a -1)
# 配列は０から始まるから１、２、３、４の木を０、１、２、３として扱ってる
# 配列の０番目（つまり頂点０）が持つ値がその頂点が持つリンクとなる

v = [0] * (n) #空の配列を作る
for j in  range(q):# 0からqまで
    a, b = map(int, input().split())
    v[a-1] += b


def csum(now, bef):  # 累積和
    for nxt in t[now]:
        if nxt != bef:
            v[nxt] += v[now]
            csum(nxt, now)

csum(0, -1)
print(*v)

Submission status: Runtime Error

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

v = [0] * (n)
for j in  range(q):
    a, b = map(int, input().split())
    v[a-1] += b


def csum(now, bef):
    for nxt in t[now]:
        if nxt != bef:
            v[nxt] += v[now]
            dfs(nxt, now)

csum(0, -1)
print(*v)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
a = [0]*(N-1)
b = [0]*(N-1)
for i in range(N-1):
  a[i], b[i] = map(int, input().split())
p = [0]*Q
x = [0]*Q
for i in range(Q):
  p[i], x[i] = map(int, input().split())
  
point = [0]* N
point = np.array(point)
for i in range(Q):
    count = 0
    for j in range(N):
        if count == 0:
            b_posi = np.where(a == p[i])
            point[p[i]-1] += x[i]
        else:
            b_posi = []
            for k in range(len(b_tmp)):
                b_posi.extend(np.where(a == b_tmp[k])[0])
        if b_posi:
            point[b[b_posi]-1] += x[i]
            b_tmp = list(set(b[b_posi]))
            count += 1
        else:
            break
print(' '.join([str(s) for s in point]))

Submission status: Runtime Error

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

for _ in range(N - 1):
	a, b = map(int, input().split())
	V[a - 1].append(b - 1)
	V[b - 1].append(a - 1)
for _ in range(Q):
	p, x = map(int, input().split())
	P[p - 1] += x
	
visit = [False] * N
visit[0] = True
	
def dfs(cur_v, point):
	point += P[cur_v]
	ret[cur_v] += point
	for v in V[cur_v]:
		if not visit[v]:
			visit[v] = True
			dfs(v, point)
			
dfs(0, 0)

print(*ret)

Submission status: Runtime Error

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

for _ in range(N - 1):
	a, b = map(int, input().split())
	V[a - 1].append(b - 1)
	V[b - 1].append(a - 1)
for _ in range(Q):
	p, x = map(int, input().split())
	P[p - 1] += x
	
visit = [False] * N
visit[0] = True
	
def dfs(cur_v, point):
	point += P[cur_v]
	ret[cur_v] += point
	for v in V[cur_v]:
		if not visit[v]:
			visit[v] = True
			dfs(v, point)
			
dfs(0, 0)

print(*ret)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import cupy
import sys
from operator import itemgetter
# import math
# import numpy as np
# from functools import lru_cache

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)


input = sys.stdin.readline
#### input
N, Q = list(map(int, input().split()))
ABs = [None]*(N-1)
for i in range(0, N-1):
  ABs[i] = list(map(int, input().split()))

ABs.sort(key=itemgetter(0))

PXs = [None]*Q
for i in range(0, Q):
  PXs[i] = list(map(int, input().split()))
# a = list(map(int, input().split()))
# required_list = list(map(lambda x: int(x)*(-1), target_list))  # 各要素を-1倍


# n = int(input())
# t = [int(input()) for i in range(n)]
if em: sys.stdin = open('/dev/tty')
#### class
# class Node:
#   def __init__(self, data):
#     self.data = data
#     self.child= []
#     self.below = []

#### function
# @lru_cache(maxsize=10000)

#### process
scores = [0] * N
above_nodes  = [[x+1] for x in range(0, N)]

for AB in ABs:
  above_nodes[AB[1]-1].extend(above_nodes[AB[0]-1])

for PX in PXs:
  scores[PX[0]-1] = scores[PX[0]-1] + PX[1]

node_scores = [None] * N
for i in range(0, N):
  node_score = 0
  for above_node in above_nodes[i]:
    node_score = node_score + scores[above_node-1]
  node_scores[i] = node_score
print(' '.join(map(str, node_scores)))

# if em: embed()
## heapsort, O(NlogN)
# import heapq
# As = [0,1,2,3]
# heapq.heapify(As)
# min = heapq.heappop(As)
# heapq.heappush(As, inserted_element)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll N, Q;

int main(){
  cin >> N >> Q;
  vector<vector<ll> > g(N+1);
  vector<ll> ans(N+1, 0);
  vector<bool> vsd(N+1, 0);
  
  for(ll i = 1; i <= N-1; i++){
    ll a, b; cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  for(ll i = 1; i <= Q; i++){
    ll p, x; cin >> p >> x;
    ans[p] += x;
  }
  
  queue<ll> que;
  que.push(1);
  vsd[1] = true;
  
  while(!que.empty()){
    ll now = que.front(); que.pop();
    for(auto e: g[now]){
      if(vsd[e]) continue;
      ans[e] += ans[now];
      que.push(e);
      vsd[e] = true;
    }
  }
  
  for(ll i = 1; i <= N; i++){
    cout << ans[i] << ' ';
  }
}

Submission status: Runtime Error

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


def plus_score(tree, index, point):
    tree[index][0] += point
    if len(tree[index][1]) == 0:
        return
    for t in tree[index][1]:
        plus_score(tree, t, point)


def main():
    N, Q = [int(x) for x in input().strip().split()]
    data = []
    for _ in range(N-1):
        data.append([int(x) for x in input().strip().split()])

    operate = [0] * (N+1)
    for _ in range(Q):
        node, point = [int(x) for x in input().strip().split()]
        operate[node] += point

    tree = [[0, []] for _ in range(N+1)]
    for d in data:
        tree[d[0]][1].append(d[1])

    for node, op in enumerate(operate):
        if op > 0:
            plus_score(tree, node, op)

    ans = [x[0] for x in tree]
    print(*ans[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

ans = {}
def dfs(cur_pos, cur_point):
	next_point = cur_point + point.get(cur_pos, 0)
	ans[cur_pos] = next_point
	if(cur_pos >= len(tree)):
		exit(0)
	for next_pos in tree[cur_pos]:
		dfs(next_pos, next_point)

dfs(0, 0)
output=[]
for i in range(N):
	output.append(ans.get(i, 0))
print(' '.join(map(str, output)))

Submission status: Runtime Error

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

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

ans = {}
def dfs(cur_pos, cur_point):
	next_point = cur_point + point.get(cur_pos, 0)
	ans[cur_pos] = next_point
	if(next_pos >= len(tree)):
		exit(0)
	for next_pos in tree[cur_pos]:
		dfs(next_pos, next_point)

dfs(0, 0)
output=[]
for i in range(N):
	output.append(ans.get(i, 0))
print(' '.join(map(str, output)))

Submission status: Runtime Error

Solution code:
from collections import deque
N, Q = map(int, input().split())

tree = [[] for _ in range(N+1)]
tree[0].append(1)

answer = [0] * N

def bfs(anker, point):
    q = deque([])  # キュー
    q.append(tree[anker])
    answer[anker - 1] += point

    for _ in range(100000000):
        if len(q) > 0:
            p = q.popleft()  # キュー取りだし（先頭）
            for child in p:
                answer[child-1] += point
                if child:
                    q.append(tree[child])  # キュー追加

for i in range(N-1):
    parent, child = map(int, input().split())
    tree[parent].append(child)
for q in range(Q):
    anker, point = map(int, input().split())
    bfs(anker, point, answer)

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

Submission status: Runtime Error

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

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

ans = {}
def dfs(cur_pos, cur_point):
	next_point = cur_point + point.get(cur_pos, 0)
	ans[cur_pos] = next_point
	for next_pos in tree[cur_pos]:
		dfs(next_pos, next_point)

dfs(0, 0)
output=[]
for i in range(N):
	output.append(ans.get(i, 0))
print(' '.join(map(str, output)))

Submission status: Runtime Error

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


def plus_score(tree, index, point):
    tree[index][0] += point
    if len(tree[index][1]) == 0:
        return
    for t in tree[index][1]:
        plus_score(tree, t, point)


def main():
    N, Q = [int(x) for x in input().strip().split()]
    data = []
    for _ in range(N-1):
        data.append([int(x) for x in input().strip().split()])
    operate = []
    for _ in range(Q):
        operate.append([int(x) for x in input().strip().split()])

    tree = [ [0,[]] for _ in range(N+1)]
    for d in data:
        tree[d[0]][1].append(d[1])

    for op in operate:
        plus_score(tree, op[0], op[1])

    ans = [x[0] for x in tree]
    print(*ans[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

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


def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)


dfs(1, -1)
print(*cnt)

Submission status: Runtime Error

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

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

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


def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)


dfs(1, -1)
print(*cnt)

Submission status: Runtime Error

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

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

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


def dfs(now, bef):
  for i in edge[now-1]:
    if i != bef:
      cnt[i-1] += cnt[now-1]
      dfs(i, now)


dfs(1, -12345)
print(" ".join(map(str, cnt)))

Submission status: Runtime Error

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

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

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


def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt)

Submission status: Runtime Error

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

cnt = [0 for _ in range(n+1)]
for p, x in px:
    cnt[p] += x

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


def dfs(now, bef):
    for nxt in edge[now]:
        if nxt != bef:
            cnt[nxt] += cnt[now]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt[1:])

Submission status: Runtime Error

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

cnt = [0 for _ in range(n)]
for p, x in px:
    cnt[p-1] += x

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


def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt)

Submission status: Runtime Error

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

cnt = [0 for _ in range(n)]
for p, x in px:
    cnt[p-1] += x

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


def dfs(now, bef):
    for nxt in edge[now-1]:
        if nxt != bef:
            cnt[nxt-1] += cnt[now-1]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt[1:])

Submission status: Runtime Error

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

cnt = [0 for _ in range(n)]
for p, x in px:
    cnt[p-1] += x

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


def dfs(now, bef):
    for nxt in edge[now]:
        if nxt != bef:
            cnt[nxt] += cnt[now]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt[1:])