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:
from collections import deque
N, Q = map(int, input().split())
tree = [[] for i in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  if a > b:
    0/0
  tree[a-1] += [b-1]

ans = [0 for i in range(N)]
for i in range(Q):
  x, p = map(int, input().split())
  ans[x-1] += p
que = deque()
for a in tree[0]:
  que.append([0,a])
while que:
  u, v = que.pop()
  ans[v] += ans[u]
  for a in tree[v]:
    que.append([v,a])

    
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
tree = [[] for i in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  tree[a-1] += [b-1]

ans = [0 for i in range(N)]
for i in range(Q):
  x, p = map(int, input().split())
  ans[x-1] += p
que = deque()
for a in tree[0]:
  que.append([0,a])
while que:
  u, v = que.pop()
  ans[v] += ans[u]
  for a in tree[v]:
    que.append([v,a])

    
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
tree = [[] for i in range(N)]
for i in range(N-1):
  a, b = map(int, input().split())
  tree[a-1] += [b-1]

ans = [0 for i in range(N)]
for i in range(Q):
  x, p = map(int, input().split())
  ans[x-1] += p
que = deque()
for a in tree[0]:
  que.append([0,a])
while que:
  u, v = que.popleft()
  ans[v] += ans[u]
  for a in tree[v]:
    que.append([v,a])

    
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
# print(n,q)
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]

# print(tree)
# print(px)

c = [0]*n

for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]

# print(c)

for i in range(1,n):
    c[i] += c[min(tree[i])-1]

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
# print(n,q)
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]

# print(tree)
# print(px)

c = [0]*n

for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]

# print(c)

for i in range(1,n):
    c[i] += c[tree[i][0]-1]

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

Submission status: Wrong Answer

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

s = [0]*n
t = [[]for _ in range(n)]
m = [0]*n
sm = [0]*n

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

for h, i in zip(list(range(n, 0, -1)), s[:0:-1]): #hがインデックス、iが上根っこの元
    t[i-1].append(h-1)
    if t[h-1]:
        t[i-1] += t[h-1]

l2 = [x for x in t if x]

for _ in range(q):
    p, x = map(int, input().split())
    m[p-1] += x
    
for i, p in enumerate(l2):
    for q in p:
        sm[q] += m[i]
        
for i in range(n):
    sm[i] += m[i]
        
print(*sm)

Submission status: Wrong Answer

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

s = [0]*n
t = [[]for _ in range(n)]
m = [0]*n
sm = [0]*n

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

for h, i in zip(list(range(n, 0, -1)), s[:0:-1]): #hがインデックス、iが上根っこの元
    t[i-1].append(h-1)
    if t[h-1]:
        t[i-1] += t[h-1]

l2 = [x for x in t if x]

for _ in range(q):
    p, x = map(int, input().split())
    m[p-1] += x
    
for i, p in enumerate(l2):
    for q in p:
        sm[q] += m[i]
        
for i in range(n):
    sm[i] += m[i]
        
print(sm)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(50000)

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

ki = [[] for _ in range(250005)]
result = [0] * N

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

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

def dfs(start, parent):
    for child in ki[start]:
        # if child == parent:
        #     continue

        result[child] += result[start]
        dfs(child, start)

dfs(0, -1)
print(" ".join(map(str, result)))

Submission status: Wrong Answer

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

np = n+1
nm = n-1
at = [[] for _ in range(np)]
ans = [0]*np

for _ in range(nm):
    a, b = map(int, input().split())
    at[a].append(b)

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

for i in range(np):
    for j in at[i]:
        ans[j] += ans[i]

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(41000000)

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

at = [[] for _ in range(n)]
ans = [0]*n

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

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

def dfs(i):
    for y in at[i]:
        ans[y] += ans[i]
        dfs(y)

dfs(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(41000000)

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

at = [[] for _ in range(n)]
ans = [0]*n

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

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

def dfs(i):
    if at[i]:
        for y in at[i]:
            ans[y] += ans[i]
            dfs(y)

dfs(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(41000000)

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

at = [[] for _ in range(n)]
ans = [0]*n

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

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

def dfs(i):
    if at[i]:
        for y in at[i]:
            ans[y] += ans[i]
            dfs(y)

dfs(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

at = [[] for _ in range(n)]
bt = [[] for _ in range(n)]
ans = [0]*n
i = 0
ii = 0

while i < n-1:
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    #print(at)
    #print(a)
    #print(b)
    at[a].append(b)
    bt[b].append(a)
    i += 1
    #print(i)

while ii < q:
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
    ii += 1
    #print(ans)
    #print(ii)

def dfs(i):
    #print(at)
    if at[i]:
        for y in at[i]:
            #print(ans)
            ans[y] += ans[i]
            dfs(y)

dfs(0)
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
AB, CD = [], []
for _ in range(N - 1):
    line = map(int, input().split())
    AB.append(list(line))
AB.sort(key = lambda x: x[0])

for _ in range(Q):
    line = map(int, input().split())
    CD.append(list(line))

values = [0 for _ in range(N+1)]
for q in range(Q):
    values[CD[q][0]] += CD[q][1]
for n in range(N-1):
    values[AB[n][1]] += values[AB[n][0]]

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
AB, CD = [], []
for _ in range(N - 1):
    line = map(int, input().split())
    AB.append(list(line))
AB.sort(key = lambda x: x[0])

for _ in range(Q):
    line = map(int, input().split())
    CD.append(list(line))

values = [0 for _ in range(N+1)]
for q in range(Q):
    values[CD[q][0]] += CD[q][1]
for n in range(N-1):
    values[AB[n][1]] += values[AB[n][0]]

print(values[1::])

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
d = {}
v = {}
for _ in range(n - 1):
    a, b = input().split()
    if a not in d:
        d[a] = [b]
    else:
        d[a].append(b)
    if b not in d:
        d[b] = []

for key in d.keys():
    v[key] = 0

for _ in range(q):
    node, increment = input().split()
    queue = [node]
    all_childs = [node]
    while len(queue) > 0:
        el = queue.pop()
        if el not in all_childs:
            all_childs.append(el)
        childs = d[el]
        if len(childs) > 0:
            queue.extend(d[el])
    for child in all_childs:
        v[child] += int(increment)

for value in v.values():
    print(value, end=' ')

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(' '))
 
listTree = [[] for i in range(N)]
listNode = [0] * N
 
for i in range(N-1):
  a, b  = map(int, input().split(' '))
  listTree[a-1] += [b]

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

listResult = [0] * N
 
def calcNode(target):
  listResult[target - 1] = listNode[target - 1]
  targets = listTree[target - 1]
  for j in targets:
    listNode[j - 1] += listNode[target - 1]
  return targets

next_targets = [1]
while True:
  next_targets = list(map(calcNode, next_targets))
  _next_targets = []
  for i in next_targets:
    _next_targets += i
  next_targets = _next_targets
  if len(next_targets) == 0:
    break

str_result = ' '.join(map(str, listResult))
print(str_result)

Submission status: Wrong Answer

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

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

score = [0]*(N+1)
for j in range(Q):
    p, x = map(int, input().split())
    score[p]+=x
    
for root, leaf in edge.items():
    for i in leaf:
        score[i]+=score[root]

print(*score[1:])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline


def main():
    N, Q = map(int, input().split())
    leaves = [[0, []] for _ in range(N+1)]  # [parent, [children]]
    for _ in range(N-1):
        a, b = map(int, input().split())
        leaves[b][0] = a
        leaves[a][1].append(b)

    costs = [0] * (N+1)

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

    q = deque([i for i in leaves[1][1]])
    while len(q) > 0:
        i = q.pop()
        parent = leaves[i][0]
        costs[i] += costs[parent]

        for j in leaves[i][1]:
            q.appendleft(j)

    print(*costs[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline


def main():
    N, Q = map(int, input().split())
    leaves = [[0, []] for _ in range(N+1)]  # [parent, [children]]
    for _ in range(N-1):
        a, b = map(int, input().split())
        leaves[b][0] = a
        leaves[a][1].append(b)

    costs = [0] * (N+1)

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

    q = deque([i for i in leaves[1][1]])
    while len(q) > 0:
        i = q.pop()
        parent = leaves[i][0]
        costs[i] += costs[parent]

        for j in leaves[i][1]:
            q.appendleft(j)

    print(costs[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = ki[0][0]
    q = ki[0][1]
    #n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    #q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    n_data = ki[1:-q]
    #q_data = ki[-q:]
    q_index = [0] * n
    for qq in ki[-q:]:
        q_index[qq[0] - 1] = qq[1]

    score = [0] * n

    """
        assert get_answer(["4 3",
                       "1 3", "1 4", "2 3",
                       "2 10", "1 100", "3 1"]) == "100 111 101 100"
    """
    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        t_index[nn[0]].append(nn[1])
        t_index[nn[1]].append(nn[0])


    """
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]
    """

    t_stack = [1]
    s_stack = [0]
    r_flag = [0] * n
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if r_flag[i - 1]:
            continue
        r_flag[i - 1] = 1
        s += q_index[i - 1]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            s = input1.split(" ")
            input_ki.append([int(s[0]), int(s[1])])
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
A =[]
c = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
    for j in range(i):
        if A[i][1] == A[j][0]:
            A.insert(j,A.pop(i))
for j in range(Q):
   x = list(map(int,input().split()))
   c[x[0]-1] += x[1]
for a in A:
    c[a[1]-1] += c[a[0]-1]
C=""
for y in c:
    C += str(y)+" "
print(C)

Submission status: Wrong Answer

Solution code:

input_int_list = list(map(int, input().split()))

N = input_int_list[0]
Q = input_int_list[1]
edge_list = [[0,0]] * N
q_list = [[0,0]] * Q
node_value = [0] * (N+1)
node_ancestor = [0] * (N+1)
ans = [0] * (N+1)

node_ancestor[0] = -1
node_ancestor[1] = 0


for ni in range(N - 1):
	edge_list[ni] = list(map(int, input().split()))
	node_ancestor[edge_list[ni][1]] = edge_list[ni][0]

for qi in range(Q):
	q_list[qi] = list(map(int, input().split()))
	node_value[q_list[qi][0]] += q_list[qi][1]

for ni in range(1,N+1):
	now_node = ni
	while node_ancestor[now_node] != -1:
		ans[ni] += node_value[now_node]
		now_node = node_ancestor[now_node]

output_string = ""

for ni in range(1,N+1):
	output_string += str(ans[ni])
	output_string += " "

print(output_string)

Submission status: Wrong Answer

Solution code:
import numpy as np

def tree(branch, a, flag):
  while (flag==1).sum()>0:
    i = branch[(flag==1)][0]
    if len(np.where(a[:,0]==i)[0])>0:
      flag[a[np.where(a[:,0]==i)[0],1]-1] = 1
    flag[i-1] = 2
  return flag

N, Q = np.array(list(map(int,input().split())))
a = np.array([list(map(int,input().split())) for i in range(N-1)])
q = np.array([list(map(int,input().split())) for i in range(Q)])

branch = np.sort(np.unique(a))
count = np.zeros_like(branch, dtype="i") 
flag = np.zeros_like(branch, dtype="i")

for i in range(Q):
  flag[:] = 0
  flag[q[i,0]-1] = 1
  flag = tree(branch, a, flag)
  count[flag>0] += q[i,1]

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

Submission status: Wrong Answer

Solution code:

input_int_list = list(map(int, input().split()))
print(input_int_list)
print(input_int_list[1])

N = input_int_list[0]
Q = input_int_list[1]
edge_list = [[0,0]] * N
q_list = [[0,0]] * Q
node_value = [0] * (N+1)
node_ancestor = [0] * (N+1)
ans = [0] * (N+1)

node_ancestor[0] = -1
node_ancestor[1] = 0


for ni in range(N - 1):
	edge_list[ni] = list(map(int, input().split()))
	node_ancestor[edge_list[ni][1]] = edge_list[ni][0]

for qi in range(Q):
	q_list[qi] = list(map(int, input().split()))
	node_value[q_list[qi][0]] += q_list[qi][1]

for ni in range(1,N+1):
	now_node = ni
	while node_ancestor[now_node] != -1:
		ans[ni] += node_value[now_node]
		now_node = node_ancestor[now_node]

output_string = ""

for ni in range(1,N+1):
	output_string += str(ans[ni])
	output_string += " "

print(output_string)

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        if nn[0] > nn[1]:
            nn[0], nn[1] = nn[1], nn[0]
        i = nn[0]
        t_index[i].append(nn[1])

    q_index = {}
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]

    t_stack = [1]
    s_stack = [0]
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if i in q_index.keys():
            s += q_index[i]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        if nn[0] == nn[1]:
            continue
        if nn[0] > nn[1]:
            nn[0], nn[1] = nn[1], nn[0]
        i = nn[0]
        t_index[i].append(nn[1])

    q_index = {}
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]

    t_stack = [1]
    s_stack = [0]
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if i in q_index.keys():
            s += q_index[i]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
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
    
for i in range(2, n+1):
    for j in graph[i]: 
        if i > j:
            value[i] += value[j]

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

Submission status: Wrong Answer

Solution code:
def get_subtree(edges,node):
    subtree = [node]
    for edge in edges:
        if edge[0] == node:
            subtree.extend(get_subtree(edges,edge[1]))
    return subtree

N,Q = map(int,input().split())
edges = list(list(map(int, input().split())) for _ in range(N-1))
operations = list(list(map(int, input().split())) for _ in range(Q))

scores = [0 for _ in range(N)]
subtree = [get_subtree(edges, i) for i in range(1,N+1,1)]

for operation in operations:
    for node in subtree[operation[0]-1]:
        scores[node-1] += operation[1]

print(*scores, sep=" ")

Submission status: Wrong Answer

Solution code:
def apply_operation(edges, operation, scores):
    root = operation[0]
    scores[root-1] += operation[1]
    for edge in edges:
        if edge[0] == root:
            apply_operation(edges,[edge[1], operation[1]], scores)
    return scores


N,Q = map(int,input().split())
edges = list(list(map(int, input().split())) for _ in range(N-1))
operations = list(list(map(int, input().split())) for _ in range(Q))

scores = [0 for _ in range(N)]

for operation in operations:
    scores = apply_operation(edges, operation, scores)

print(*scores, sep=" ")

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(1000000)

N,Q = map(int,input().split())
ab_list = [tuple(map(int,input().split())) for _ in range(N-1)]
px_list = [tuple(map(int,input().split())) for _ in range(Q)]

px_dict = {}
for i in range(N+1):
    px_dict[i] = 0

for p, x in px_list:
    px_dict[p] += x

ab_dict = {}
for i in range(N+1):
    ab_dict[i] = []

for a,b in ab_list:
    ab_dict[a].append(b)

scores = [0 for _ in range(N+1)] # 1子おおい

def add_cnt(v, score):
    # pxdictから持ってきてスコアを足す
    score += px_dict[v]

    #今いる頂点に現在のスコアをめも
    scores[v] = score
    
    # 繋がっている頂点に今のスコアを投げる
    for b in ab_dict[v]:
        add_cnt(b,score)

add_cnt(1,0)

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [[] for n in range(N+1)]

edges = {1:[]}
for i in range(N-1):
    a,b = map(int, input().split())
    if not edges.get(a):
        edges[a] = []
    edges[a].append(b)
ans = [0] * N
for i in range(Q):
    p,q = map(int, input().split())
    ans[p-1] += q

for par,chi in edges.items():
    for c in chi:
        ans[c-1] += ans[par-1]
for _ in range(len(ans)):
    print(ans[_], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [[] for n in range(N+1)]

edges = {}
for i in range(N-1):
    a,b = map(int, input().split())
    if not edges.get(a):
        edges[a] = []
    edges[a].append(b)
ans = [0] * N
for i in range(Q):
    p,q = map(int, input().split())
    ans[p-1] += q

for par,chi in edges.items():
    for c in chi:
        ans[c-1] += ans[par-1]
for _ in range(len(ans)):
    print(ans[_], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [[] for n in range(N+1)]

edges = {}
for i in range(N-1):
    a,b = map(int, input().split())
    if not edges.get(a):
        edges[a] = []
    edges[a].append(b)
ans = [0] * N
for i in range(Q):
    p,q = map(int, input().split())
    ans[p-1] += q

for par,chi in edges.items():
    for c in chi:
        ans[c-1] += ans[par-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [[] for n in range(N+1)]
ANS = [0] * (N+1)
for _ in range(N-1):
    a,b = map(int, input().split())
    G[a].append(b)
for _ in range(Q):
    p, x = map(int, input().split())
    ANS[p] += x

from collections import deque
que = deque([])
que.append(1)
while que:
    node = que.pop()
    ans = ANS[node]
    for child in G[node]:
        ANS[child] += ans
        que.append(child)
for i in range(1, N+1):
    print(ANS[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
adj = [[] for _ in range(N)]
for a, b in edges:
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)

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

Q = [[0, -1, 0]]
ans = [0]*N
while Q:
    cur, prev, add = Q.pop(0)
    add += query[cur]
    ans[cur] = add
    if len(adj[cur]) != 1:
        for nex in adj[cur]:
            if nex != prev:
                Q.append([nex, cur, add])
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
v_out = [[] for _ in range(N)]
for a, b in edges:
    v_out[a-1].append(b-1)

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

Q = [[0, 0]]  # (cur,add)
ans = [0]*N
while Q:
    cur, add = Q.pop(0)
    add += query[cur]
    ans[cur] = add
    if len(v_out[cur]) != 0:
        for nex in v_out[cur]:
            Q.append([nex, add])
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, Q = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N-1)]
v_out = [[] for _ in range(N)]
for a, b in edges:
    v_out[a-1].append(b-1)

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

Q = [[0, 0]]  # (cur,add)
ans = [0]*N
while Q:
    cur, add = Q.pop(0)
    add += query[cur]
    ans[cur] = add
    if len(v_out[cur]) != 0:
        for nex in v_out[cur]:
            Q.append((nex, add))
print(*ans)

Submission status: Wrong Answer

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

n_val = [0] * n
depth = [-1] * n
depth[0] = 0

i = -1
while i == -1:
    i = 0
    for a,b in ab:
        if depth[a - 1] == -1 and depth[b - 1] == -1:
            i = -1
        elif depth[a - 1] > depth[b - 1]:
             depth[b - 1] = depth[a - 1] + 1

for p,x in px:
    n_val[p-1] += x

for i in ab:
    i.append(depth[i[0]-1] if depth[i[0]-1] < depth[i[1]-1] else depth[i[1]-1])

ab.sort(key=lambda x: x[2])

for a,b,c in ab:
    if depth[a - 1] < depth[b - 1]:
        n_val[b - 1] += n_val[a - 1]
    else:
        n_val[a - 1] += n_val[b - 1]

answer = ""
for ans in n_val:
    answer += str(ans) + " "

print(answer.strip())

Submission status: Wrong Answer

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

n_val = [0] * n
depth = [-1] * n
depth[0] = 0

i = -1
while i == -1:
    i = 0
    for a,b in ab:
        if depth[a - 1] == -1 and depth[b - 1] == -1:
            i = -1
        elif depth[a - 1] > depth[b - 1]:
             depth[b - 1] = depth[a - 1] + 1

for p,x in px:
    n_val[p-1] += x

for i in ab:
    i.append(depth[i[0]-1] if depth[i[0]-1] < depth[i[1]-1] else depth[i[1]-1])

ab.sort(key=lambda x: x[2])

print(ab)

for a,b,c in ab:
    if depth[a - 1] < depth[b - 1]:
        n_val[b - 1] += n_val[a - 1]
    else:
        n_val[a - 1] += n_val[b - 1]

answer = ""
for ans in n_val:
    answer += str(ans) + " "

print(answer.strip())

Submission status: Wrong Answer

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

input = sys.stdin.readline
N,Q = [int(i) for i in input().split()]
#a_all, b_all = [[int(j) for j in input().split()] for i in range(N-1)]
#x_all, x_all = [[int(j) for j in input().split()] for i in range(Q)]

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

counter = [0 for i in range(N)]
def count(p, x):
    counter[p-1] += x
    for child in graph[p-1]:
        count(child, x)

for i in range(Q):
    p, x = [int(j) for j in input().split()]
    count(p, x)
    
print(*counter)

Submission status: Wrong Answer

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

#input = sys.stdin.readline
N,Q = [int(i) for i in input().split()]
#a_all, b_all = [[int(j) for j in input().split()] for i in range(N-1)]
#x_all, x_all = [[int(j) for j in input().split()] for i in range(Q)]

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

counter = [0 for i in range(N)]
def count(p, x):
    counter[p-1] += x
    for child in graph[p-1]:
        count(child, x)

for i in range(Q):
    p, x = [int(j) for j in input().split()]
    count(p, x)
    
print(*counter)

Submission status: Wrong Answer

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

n_val = [0] * n
depth = [-1] * n
depth[0] = 0

for p,x in px:
    n_val[p-1] += x

for a,b in ab:
    if depth[a - 1] > depth[b - 1]:
        depth[b - 1] = depth[a - 1] + 1
        n_val[b - 1] += n_val[a - 1]
    else:
        depth[a - 1] = depth[b - 1] + 1
        n_val[a - 1] += n_val[b - 1]

answer = ""
for ans in n_val:
    answer += str(ans) + " "

print(answer.strip())

Submission status: Wrong Answer

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

n_val = [0] * n

for i in range(n-1):
    n_val[i] = 0

for p,x in px:
    n_val[p-1] += x

for a,b in ab:
    n_val[b-1] += n_val[a-1]

answer = ""
for ans in n_val:
    answer += str(ans) + " "

print(answer.strip())

Submission status: Wrong Answer

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

for i in range(N-1):
	a,b = map(int,input().split())
	tree[a].append(b)
	
for i in range(Q):
	p,x = map(int,input().split())
	for node in tree[p]:
		ans[node] += x

answer=""
for i in range(1,N+1):
	answer += str(ans[i]) + " "

print(answer)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
tree ={}
ans=[]
for i in range(1,N+1):
	tree[str(i)] = [i]
for i in range(N+1):
	ans.append(0)

for i in range(N-1):
	a,b = map(int,input().split())
	tree[str(a)].append(b)
	
for i in range(Q):
	p,x = map(int,input().split())
	for node in tree[str(p)]:
		ans[node] += x

answer=""
for i in range(1,N+1):
	answer += str(ans[i]) + " "

print(answer)