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: Wrong Answer

Solution code:
N, Q = list(map(int, input().split()))
parent = [0 for i in range(N)]
point = [set([i]) for i in range(N)]
result = [0 for i in range(N)]
for i in range(N-1):
	a, b = list(map(int, input().split()))
	parent[b-1] = a-1
for i in range(N):
	point[i] = point[i].union(point[parent[i]])
for i in range(Q):
	p, q = list(map(int, input().split()))
	for j in range(N):
		if p-1 in point[j-1]:
			result[j-1] += q

resultStr = [str(a) for a in result]
resultStr = " ".join(resultStr)
print(resultStr)

Submission status: Wrong Answer

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

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

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

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

wait = [1]
while wait:
    temp = wait.pop()
    for i in v[temp]:
        if i > temp:
            wait.append(i)
            cnt[i] += cnt[temp]


print(' '.join(map(str, cnt[1:])))

Submission status: Wrong Answer

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

N, Q = map(int, input().split())
v = [[] for _ in range(N + 1)]
cnt = [0] * (N + 1)
a = [0] * (N - 1)
b = [0] * (N - 1)

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

for i, j in sorted(zip(a, b)):
    v[i].append(j)
    v[j].append(i)

for i in reversed(range(2, N + 1)):
    v[v[i][0]].extend(v[i][1:])

v[1].append(0)
v[1].sort()

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

ans = cnt[:]

for i in range(1, N + 1):
    for j in v[i][1:]:
        ans[j] += cnt[i]

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

Submission status: Wrong Answer

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

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

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

for i in reversed(range(2, N + 1)):
    v[v[i][0]].extend(v[i][1:])

v[1].append(0)
v[1].sort()

for i in range(Q):
    p, q = map(int, input().split())
    cnt[p] += q

ans = cnt[:]

for i in range(1, N + 1):
    for j in v[i][1:]:
        ans[j] += cnt[i]

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

Submission status: Wrong Answer

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

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

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

for i in reversed(range(2, N + 1)):
    v[i].sort()
    v[v[i][0]].extend(v[i][1:])

v[1].append(0)
v[1].sort()

for i in range(Q):
    p, q = map(int, input().split())
    cnt[p] += q

ans = cnt[:]

for i in range(1, N + 1):
    for j in v[i][1:]:
        ans[j] += cnt[i]

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

Submission status: Wrong Answer

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

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

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

for i in reversed(range(2, N + 1)):
    v[i].sort()
    v[v[i][0]].extend(v[i][1:])

v[1].append(0)

for i in range(Q):
    p, q = map(int, input().split())
    for j in v[p]:
        ans[j] += q
    ans[p] += q

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

Submission status: Wrong Answer

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

A.sort()

cost = [0]*(N+1)

for _ in range(Q):
	a, b = map(int,input().split())
	cost[a] += b

for a in A:
	cost[a[1]] += cost[a[0]]

print(*cost[1:])

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
import itertools
from collections import deque

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

parent = [[] for _ in range(N)]
child = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    parent[b-1].append(a-1)
    child[a-1].append(b-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # ノード0の親は便宜上ノード0にする

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

# BFSで親ノードの値を子ノードに足していく
point = deque(child[0])
while point:
    current_point = point.popleft()
    counter[current_point] += counter[parent[current_point]]
    point.extend(child[current_point])

print(*counter)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque

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

parent = [[] for _ in range(N)]
child = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a, b = min(a,b), max(a,b)
    parent[b-1].append(a-1)
    child[a-1].append(b-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # ノード0の親は便宜上ノード0にする

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

# DFSで親ノードの値を子ノードに足していく
point = deque(child[0])
while point:
    current_point = point.pop()
    counter[current_point] += counter[parent[current_point]]
    point.extend(child[current_point])

print(*counter)

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque

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

parent = [[] for _ in range(N)]
child = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    parent[b-1].append(a-1)
    child[a-1].append(b-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # ノード0の親は便宜上ノード0にする

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

# DFSで親ノードの値を子ノードに足していく
point = deque(child[0])
while point:
    current_point = point.pop()
    counter[current_point] += counter[parent[current_point]]
    point.extend(child[current_point])

print(*counter)

Submission status: Wrong Answer

Solution code:
import itertools

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

parent = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    parent[b-1].append(a-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # 便宜上ノード0の親はノード0にしておく

# ノードiを対象とする操作のリスト
operation = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    operation[p-1] += x

# ノードiのカウンター値は、親のカウンター値 + p_j=iを満たす全てのjについてΣx_j
counter = [operation[0]] + [0]*(N-1)

for node_idx in range(1, N):
    counter[node_idx] += operation[node_idx]
    counter[node_idx] += counter[parent[node_idx]]


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

Submission status: Wrong Answer

Solution code:
import itertools

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

parent = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    parent[b-1].append(a-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # 便宜上ノード0の親はノード0にしておく

# ノードiを対象とする操作のリスト
operation = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    operation[p-1] += x

# ノードiのカウンター値は、親のカウンター値 + p_j=iを満たす全てのjについてΣx_j
counter = [0]*(N)

for node_idx in range(N):
    counter[node_idx] += counter[parent[node_idx]]
    counter[node_idx] += operation[node_idx]

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

Submission status: Wrong Answer

Solution code:
import itertools

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

parent = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    parent[b-1].append(a-1)
parent = [0] + list(itertools.chain.from_iterable(parent)) # 便宜上ノード0の親はノード0にしておく

# ノードiを対象とする操作のリスト
operation = [[] for _ in range(N)]
for _ in range(Q):
    p, x = map(int, input().split())
    operation[p-1].append(x)

# ノードiのカウンター値は、親のカウンター値 + p_j=iを満たす全てのjについてΣx_j
counter = [0]*(N)

for node_idx in range(N):
    counter[node_idx] += counter[parent[node_idx]]
    counter[node_idx] += sum(operation[node_idx])

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

Submission status: Wrong Answer

Solution code:
import numpy as np
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())

a = np.array(a)
b = np.array(b)
p = np.array(p)
x = np.array(x)
  
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: Wrong Answer

Solution code:
#!/usr/bin/env python3
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 a, b in ABs:
  above_nodes[b-1].extend(above_nodes[a-1])

for p, x in PXs:
  scores[p-1] = scores[p-1] + x

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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from operator import itemgetter

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

  ABs.sort(key=itemgetter(0))

  PXs = [None]*Q
  for i in range(0, Q):
    PXs[i] = list(map(int, sys.stdin.readline().split()))

  #### 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 __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from operator import itemgetter

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

ABs.sort(key=itemgetter(0))

PXs = [None]*Q
for i in range(0, Q):
  PXs[i] = list(map(int, sys.stdin.readline().split()))

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from operator import itemgetter

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
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: Wrong Answer

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

def main():
  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 __name__ == '__main__':
    main()

# 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: Wrong Answer

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

# def main():
# 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, sys.stdin.readline().split()))
ABs = [None]*(N-1)
for i in range(0, N-1):
  ABs[i] = list(map(int, sys.stdin.readline().split()))

# ABs.sort(key=itemgetter(0))

PXs = [None]*Q
for i in range(0, Q):
  PXs[i] = list(map(int, sys.stdin.readline().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 __name__ == '__main__':
#     main()

# 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: Wrong Answer

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

def main():
  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, sys.stdin.readline().split()))
  ABs = [None]*(N-1)
  for i in range(0, N-1):
    ABs[i] = list(map(int, sys.stdin.readline().split()))

  # ABs.sort(key=itemgetter(0))

  PXs = [None]*Q
  for i in range(0, Q):
    PXs[i] = list(map(int, sys.stdin.readline().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 __name__ == '__main__':
    main()

# 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: Wrong Answer

Solution code:
#!/usr/bin/env python3
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(1))

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: Wrong Answer

Solution code:
#!/usr/bin/env python3
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 = []
for i in range(0, N-1):
  ABs.append(list(map(int, input().split())))

ABs.sort(key=itemgetter(1))

PXs = []
for i in range(0, Q):
  PXs.append(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 = []
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.append(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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# 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 = []
for i in range(0, N-1):
  ABs.append(list(map(int, input().split())))

ABs.sort(key=lambda x:x[1])

PXs = []
for i in range(0, Q):
  PXs.append(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 = []
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.append(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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# 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 = []
for i in range(0, N-1):
  ABs.append(list(map(int, input().split())))

ABs = sorted(ABs, key=lambda x:x[1])

PXs = []
for i in range(0, Q):
  PXs.append(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 = []
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.append(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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# 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 = []
for i in range(0, N-1):
  ABs.append(list(map(int, input().split())))

ABs = sorted(ABs, key=lambda x:x[1])

PXs = []
for i in range(0, Q):
  PXs.append(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]

for i in range(0, N):
  node_score = 0
  for above_node in above_nodes[i]:
    node_score = node_score + scores[above_node-1]
  print(node_score, end = ' ')
print("")

# 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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

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

ABs = sorted(ABs, key=lambda x:x[1])

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

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

for i in range(0, len(ABs)):
  above_nodes[ABs[i][1]-1].extend(above_nodes[ABs[i][0]-1])

for i in range(0, len(PXs)):
  scores[PXs[i][0]-1] = scores[PXs[i][0]-1] + PXs[i][1]

for i in range(0, N):
  node_score = 0
  for above_node in above_nodes[i]:
    node_score = node_score + scores[above_node-1]
  print(node_score, end = ' ')
print("")

Submission status: Wrong Answer

Solution code:

#WA aiはbiの親であるとは限らない
#有向グラフかと思ったけど違かった無向グラフ
#訪れたことのあるとこにはフラグをたてる
#グラフの隣接リストによる実装

class edge:
#    def __init__(self,to,cost):
    def __init__(self,to):
        self.to = to
        #self.cost = cost

#N個の頂点、N-1個の辺、Q操作回数
N,Q = map(int,(input().split()))
 
#添字と入力が1ずれるので注意
#グラフ (iがグラフの頂点、G[i]に隣接するグラフが入っている)
G = [ [] for row in range(N)]
for i in range(N-1):
    #sからtへ辺を張る
    s,t = map(int,input().split())
    #添字調整
    s -= 1
    t -= 1
    G[s].append(edge(t))
    #無向グラフなので逆にも張る
    G[t].append(edge(s))
 
#各頂点以下に加算する重み
ans = [0] * N
for i in range(Q):
    #頂点pにxの重みをつける
    p,x = map(int,input().split())
    #添字調整
    p -= 1
    ans[p] += x

#訪れたことのあるとこにはいかないようにチェックする
check = [0] * N
#開始点には戻ってこない
check[0] = 1

#各頂点以下の重みの和を上から順に計算する
for i in range(N):
    for j in range(len(G[i])):
        if check[G[i][j].to] == 0:
            ans[G[i][j].to] += ans[i]
            check[G[i][j].to] = 1
 
print(" ".join(map(str,ans)))