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:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
import sys

sys.setrecursionlimit(500000)

n,q = nm()

tval = [0]*n
t = [[] for _ in range(n)]

for i in range(n-1):
    a = nl()
    aa, bb = sorted(a)
    t[aa-1].append(bb-1)

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

ans = [0]*n
def dfs(num,val):
    now = val+tval[num]
    ans[num] = now
    for nn in t[num]:
        dfs(nn,now)

dfs(0,0)

for i in ans:
    print(i)

Submission status: Wrong Answer

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

def dfs(number_list):
    for number in number_list:
        if number in check_list:
            continue
        check_list.append(number)
        global memo
        ans[number] += memo
        memo = ans[number]
        dfs(graph[number])
    

n, q = map(int, input().split())
li_ab = [list(map(int, input().split())) for _ in range(n-1)]
li_px = [list(map(int, input().split())) for _ in range(q)]

graph = [[] for _ in range(n+10)]
for a,b in li_ab:
    graph[a].append(b)
    graph[b].append(a)

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

check_list = [1]
memo = ans[1]
dfs(graph[1])
    
print(*ans[1:])

Submission status: Wrong Answer

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

def dfs(number):
    ans[number] += x
    for g in graph[number]:
        dfs(g)
    

n, q = map(int, input().split())
li_ab = [list(map(int, input().split())) for _ in range(n-1)]
li_px = [list(map(int, input().split())) for _ in range(q)]

graph = [[] for _ in range(n+10)]
for a,b in li_ab:
    graph[a].append(b)
    
ans = [0 for _ in range(n+1)]

for p,x in li_px:
    dfs(p)
    
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)


def dfs(p, to, ans):
    for v in to[p]:
        ans[v] += ans[p]
        dfs(v, to, ans)


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

    for _ in range(N - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        to[a].append(b)
    
    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        ans[p] += x
    dfs(0, to, ans)
    
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)


def dfs(p, x, to, ans):
    ans[p] += x
    for v in to[p]:
        dfs(v, x, to, ans)


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

    for _ in range(N - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        to[a].append(b)
    
    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        dfs(p, x, to, ans)
    
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)


def dfs(p, x, to, seen, ans):
    seen[p] = True
    ans[p] += x
    for v in to[p]:
        if seen[v]:
            continue
        dfs(v, x, to, seen, ans)


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

    for _ in range(N - 1):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        to[a].append(b)
    
    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        seen = [False] * N
        dfs(p, x, to, seen, ans)
    
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# 頂点数と変数
n, q = map(int, input().split())
Graph = [[] for i in range(n+1)]
seen = [False]*(n+1)
point = [0]*(n+1)


def dfs(g, v, x):

    # vを訪問済みにする
    seen[v] = True
    point[v] += x

    # vから行ける各頂点について
    for next_v in g[v]:

        # next_vが訪問済みだったらスルー
        if seen[next_v]: continue
        if next_v < v: continue

        # 再帰的に探索
        dfs(g, next_v, x)


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

    # 無向グラフを想定
    Graph[a].append(b)
    Graph[b].append(a)

for i in range(q):
    p, x = map(int, input().split())
    dfs(Graph, p, x)
    seen = [False] * (n+1)


for i in point:
    if i == 0: continue
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from collections import deque
def input(): return sys.stdin.readline().rstrip()


def main():
    n, q = map(int, input().split())

    graph = [[] for _ in range(n+1)]

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

    clist = [0]*(n+1)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
a = [list(map(int,input().split())) for i in range(N-1)]
a.sort()
p = [list(map(int,input().split())) for i in range(Q)]

cost = [0]*N
for i in p:
    cost[i[0]-1] += i[1]

total = [0]*N
j=0
for i in range(N):
    total[i] += cost[i]

    while j < N-1:
        if i ==(a[j][0]-1):
            total[a[j][1]-1] += total[i]
            j+=1
        else:
            break
    total[i] = str(total[i])

print(' '.join(total))

Submission status: Wrong Answer

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

N,Q = list(map(int,input().split()))
road = [[] for i in range(N+1)]
for _ in range(N-1):
    a,b = map(int,input().split())
    road[a].append(b)
point = [0]*(N+1)

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

q = collections.deque([1])

while q:
    now = q.pop()
    for i in road[now]:
        q.append(i)
        point[i] += point[now]

print(*point[1:])

Submission status: Wrong Answer

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

N,Q = list(map(int,input().split()))
road = [[] for i in range(N+1)]
for _ in range(N-1):
    a,b = map(int,input().split())
    road[a].append(b)
point = [0]*(N+1)

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

q = collections.deque([1])

while q:
    now = q.popleft()
    for i in road[now]:
        q.append(i)
        point[i] += point[now]

print(*point[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int,input().split()) # 複数の数字を変数に格納
List_ab = [list(map(int,input().split())) for i in range(N-1)] # [[1,2],[3,4],[5,6]]
List_px = [list(map(int,input().split())) for i in range(Q)] # [[1,2],[3,4],[5,6]]
 
count_list = [0]*N
 
List_ab.sort()
 
for i in range(Q):
    count_list[List_px[i][0]-1] += List_px[i][1]
        
for i in range(N-1):
    count_list[List_ab[i][1]-1] += count_list[List_ab[i][0]-1]
    
print(*count_list)

Submission status: Wrong Answer

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)]
count = [0] * n

def dfs(node, point):
    global n, count
    visited = [-1] * n
    stack = []
    stack.append(node)
    visited[node] = 0
    while stack:
        target = stack.pop()
        for i in graph[target]:
            if visited[i] != -1:
                continue
            if i < node:
                continue
            else:
                visited[i] = 0
                stack.append(i)
    for i, v in enumerate(visited):
        if v == 0:
            count[i] += point
    return 0


# グラフ作成
graph = [[] for _ in range(n)]
for node1, node2 in ab:
    node1, node2 = node1 - 1, node2 - 1
    graph[node1].append(node2)
    graph[node2].append(node1)

# 一回の操作に対して、そのノードをスタートとするDFSを行い、countに足していく
for p, x in px:
    dfs(p-1, x)
print(*count, sep=" ")

Submission status: Wrong Answer

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)]
count = [0] * n

def dfs(node, point):
    global n, count
    visited = [-1] * n
    stack = []
    stack.append(node)
    visited[node] = 0
    while stack:
        target = stack.pop()
        for i in graph[target]:
            if i < node:
                continue
            else:
                if visited[i] == 0:
                    continue
                visited[i] = 0
                stack.append(i)
    for i, v in enumerate(visited):
        if v == 0:
            count[i] += point
    return 0


# グラフ作成
graph = [[] for _ in range(n)]
for node1, node2 in ab:
    node1, node2 = node1 - 1, node2 - 1
    graph[node1].append(node2)
    graph[node2].append(node1)

# 一回の操作に対して、そのノードをスタートとするDFSを行い、countに足していく
for p, x in px:
    dfs(p-1, x)
print(*count, sep=" ")

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = [int(x) for x in input().split()]

tree = {}
result = [0] * N

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

add = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]

    add[p-1] += x

queue = deque()
queue.appendleft((0, add[0]))

while queue:
    target = queue.pop()
    node = target[0]
    count = target[1]

    result[node] = count

    if node in tree:
        for next_node in tree[node]:
            queue.appendleft((next_node, count + add[next_node]))

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

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = [int(x) for x in input().split()]

tree = {}
result = [0] * N

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

for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    queue = deque()
    queue.appendleft((p, x))

    while queue:
        target = queue.pop()
        node = target[0]
        count = target[1]

        result[node-1] += count

        if node in tree:
            for next_node in tree[node]:
                queue.appendleft((next_node, count))

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

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

n, q = map(int, input().split())
nodes = [[] for _ in range(n+1)]
nodes[1].append(0)
counts = [0 for _ in range(n+1)]

for i in range(n-1):
    pea, chi = map(int, input().split())
    nodes[chi].append(pea)
    nodes[pea].append(chi)
    while pea != 1:
        pea = nodes[pea][0]
        nodes[pea].append(chi)
        
for i in range(q):
    p, q = map(int, input().split())
    if len(nodes[p]) != 1:
        for j in range(1,len(nodes[p])):
            counts[nodes[p][j]] += q
    counts[p] += q

str_counts = map(str, counts[1:])
print(' '.join(str_counts))

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
nodes = [[] for _ in range(n+1)]
nodes[1].append(0)
counts = [0 for _ in range(n+1)]

for i in range(n-1):
    pea, chi = map(int, input().split())
    nodes[chi].append(pea)
    nodes[pea].append(chi)
    while pea != 1:
        pea = nodes[pea][0]
        nodes[pea].append(chi)
        
for i in range(q):
    p, q = map(int, input().split())
    if len(nodes[p]) != 1:
        for j in range(1,len(nodes[p])):
            counts[nodes[p][j]] += q
    counts[p] += q

str_counts = map(str, counts[1:])
print(' '.join(str_counts))

Submission status: Wrong Answer

Solution code:
import sys

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

edges = [[] for _ in range(n)]
for _ in range(n - 1):
    frm, to = map(int, input().split())
    # 0-index
    edges[frm - 1].append(to - 1)


scores = [0] * n
for _ in range(q):
    v, s = map(int, input().split())
    scores[v - 1] += s


stack = [0]
while len(stack):
    frm = stack.pop()
    for to in edges[frm]:
        scores[to] += scores[frm]
        stack.append(to)


print(" ".join([str(w) for w in scores]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]  
    n, q = [int(x) for x in input().split()]  
    g = defaultdict(list)
    cnt = [0] * (n+1)

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

    for _ in range(q):
        v, x = [int(x) for x in input().split()]
        cnt[v] += x
    
    q = deque([(1, 0)])
    while q:
        v, val = q.popleft()
        cnt[v] += val
        for to in g[v]:
            q.append((to, cnt[v]))

    print(*cnt[1:])


    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""
a b a
d c d
a b a
"""

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
parent = [-1]*N
for i in range(N-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    parent[b] = a
point = [0]*N
for _ in range(Q):
    p, x = map(int, input().split())
    point[p-1] += x
done = [-1]*N
def dfs(cur, p):
    if done[cur] != -1: return p+done[cur]
    if parent[cur] == -1: return p+point[cur]
    p += dfs(parent[cur], point[cur])
    done[cur] = p
    return p
ans = []
for i in range(N):
    ans.append(dfs(i, 0))
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ki = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int, input().split())
    ki[a-1].append(b-1)
ans = [0 for i in range(N)]
for i in range(Q):
    p,x = map(int, input().split())
    prev = [False for i in range(N)]
    pre = [p-1]
    while pre != []:
        tmp = pre.pop()
        prev[tmp] = True
        ans[tmp] += x
        for t in ki[tmp]:
            if prev[t] == False and t not in pre:
                pre.append(t)
print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
point = [0] * n
for _ in range(n-1):
    a, b = map(int, input().split())
    min_v, max_v = min(a, b), max(a, b)
    tree[min_v-1].append(max_v-1)

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

q = [0]
while True:
    if len(q) == 0:
        break
    x = q.pop(0)
    for i in tree[x]:
        point[i] += point[x]
        q.append(i)

print(*point)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
point = [0] * n
for _ in range(n-1):
    a, b = map(int, input().split())
    min_v, max_v = min(a, b), max(a, b)
    tree[a-1].append(b-1)

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

q = [0]
while True:
    if len(q) == 0:
        break
    x = q.pop(0)
    for i in tree[x]:
        point[i] += point[x]
        q.append(i)

print(*point)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
point = [0] * n
list_AB = [ list(map(int,input().split(" "))) for _ in range(n-1)]
list_AB = sorted(list_AB, key=lambda x:(x[0], x[1]))

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

for a, b in list_AB:
    min_v, max_v = min(a-1, b-1), max(a-1, b-1)
    point[max_v] += point[min_v]

print(*point)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
point = [0] * n
list_AB = [ list(map(int,input().split(" "))) for _ in range(n-1)]
list_AB = sorted(list_AB, key=lambda x:x[0])

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

for a, b in list_AB:
    min_v, max_v = min(a-1, b-1), max(a-1, b-1)
    point[max_v] += point[min_v]

print(*point)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
point = [0] * n
list_AB = [ list(map(int,input().split(" "))) for _ in range(n-1)]

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

for a, b in list_AB:
    min_v, max_v = min(a-1, b-1), max(a-1, b-1)
    point[max_v] += point[min_v]

print(*point)

Submission status: Wrong Answer

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

for _ in range(q):
    p, x = map(int, input().split())
    k = [p-1]
    frag = [0] * n
    while True:
        if len(k) == 0:
            break
        y = k.pop(0)
        frag[y] = 1
        point[y] += x
        for i in tree[y]:
            if frag[i] ==  0 and i >= p-1:
                k.append(i)

print(*point)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

def bfs(s):
    seen = [0 for i in range(N+1)]
    prev = [0 for i in range(N+1)]
    todo = deque([])
    now = s
    seen[now]=1
    todo.append(now)
    while 1:
      if len(todo)==0:break
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          prev[b] = a
          P[b] += P[a]

bfs(1)
print(*P[1:])

Submission status: Wrong Answer

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

d = deque()
d.append(0)
while len(d) != 0:
    x = d.popleft()
    for i in edge[x]:
        nord[i] += nord[x]
        d.append(i)
for i in nord:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q = map(int,input().split())
l = [[] for i in range(n+1)]
pts = [0]*(n+1)
for _ in range(n-1):
  a,b = map(int,input().split())
  l[a].append(b)
for _ in range(q):
  p,x = map(int,input().split())
  pts[p] += x
Q = deque()
Q.append(1)
ans = [0]*(n+1)
vst = [0]*(n+1)
while Q:
  temp = Q.popleft()
  for n in l[temp]:
    if vst[n]:
      continue
    ans[n] = ans[temp] + pts[temp]
    Q.append(n)
  ans[temp] += pts[temp]
  vst[temp] = 1
print(*ans[1:])

Submission status: Wrong Answer

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

N,Q = (int(x) for x in input().rstrip('\n').split())
r = {1:0}
d = {x:{x} for x in range(1,N+1)}
pl = [[x] for x in range(N+1)]
for _ in range(N-1):
  a,b = (int(x) for x in input().rstrip('\n').split())
  if a in r:
    r[b] = a
    d[b] = d[b] | d[a]
    for x in d[a]:
      pl[x].append(b)
  else:
    r[a] = b
    d[a] = d[a] | d[b]
    for x in d[b]:
      pl[x].append(a)
res = [0]*(N)
for _ in range(Q):
  p,s = (int(x) for x in input().rstrip('\n').split())
  for x in pl[p]:
    res[x-1] += s
print(*res)

Submission status: Wrong Answer

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

N,Q = (int(x) for x in input().rstrip('\n').split())
r = {1:0}
d = {x:{x} for x in range(1,N+1)}
for _ in range(N-1):
  a,b = (int(x) for x in input().rstrip('\n').split())
  if a in r:
    r[b] = a
    d[b] = d[b] | d[a]
  else:
    r[a] = b
    d[a] = d[a] | d[b]
pl = {x:[] for x in range(1,N+1)}
for i in range(1,N+1):
  for x in d[i]:
    pl[x].append(i)
res = [0]*(N)
for _ in range(Q):
  p,s = (int(x) for x in input().rstrip('\n').split())
  for x in pl[p]:
    res[x-1] += s
print(*res)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
d=[[0] for _ in range(n+1)]
d1=[0 for _ in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  d[a].append(b)
for i in range(q):
  p,x=map(int,input().split())
  d1[p]+=x

for i in range(1,n+1):
  for m in d[i]:
    d1[m]+=d1[i]
print(*d1[1:])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
d=[[0] for _ in range(n+1)]
d1=[0 for _ in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  d[a].append(b)
for i in range(q):
  p,x=map(int,input().split())
  d1[p]+=x

for i in range(1,n+1):
  for m in d[i]:
    d1[m]+=d1[i]
print(d1[1:])

Submission status: Wrong Answer

Solution code:
from queue import Queue
def bfs(target, graph, scores):
    visited = [False]*len(graph)
    que = Queue()
    que.put(target)
    visited[target] = True
    while not que.empty():
        root = que.get()
        for dst in graph[root]:
            if visited[dst] is False:
                visited[dst] = True
                que.put(dst)
                scores[dst] += scores[root] 
    return scores 
n, q = map(int, input().split())
graph = {i:[] for i in range(0, n)}
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)

scores = [0]*n
for _ in range(q):
    t, s = map(int, input().split())
    t -= 1
    scores[t] += s

ans = bfs(0, graph, scores)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
def dfs(target, graph, scores):
    visited = [False]*len(graph)
    stack = []
    stack.append(target)
    visited[target] = True
    while len(stack) > 0: 
        root = stack.pop()
        for dst in graph[root]:
            if visited[dst] is False:
                visited[dst] = True
                stack.append(dst)
                scores[dst] += scores[root] 
    return scores 

n, q = map(int, input().split())
graph = {i:[] for i in range(0, n)}
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)

scores = [0]*n
for _ in range(q):
    t, s = map(int, input().split())
    t -= 1
    scores[t] += s

ans = dfs(0, graph, scores)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, x, cnt):
    cnt[p] += x
    for c in tree[p]:
        dfs(tree, c, x, cnt)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
    # query = [(0, 0)] * q
    cnt = [0] * (n + 1)
    for i in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
        # query[i] = (p, x)
    # query.sort(key=lambda q: q[0])

    # for p, x in query:
    #     dfs(tree, p, x, cnt)
    # for p, x in query:
    #     cnt[p] += x
    for p in range(1, n + 1):
        for c in tree[p]:
            cnt[c] += cnt[p]
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, x, cnt):
    cnt[p] += x
    for c in tree[p]:
        dfs(tree, c, x, cnt)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        query = [(0, 0)] * q
    for i in range(q):
        p, x = map(int, input().split())
        query[i] = (p, x)
    # query.sort(key=lambda q: q[0])
    cnt = [0] * (n + 1)
    # for p, x in query:
    #     dfs(tree, p, x, cnt)
    for p, x in query:
        cnt[p] += x
    for p in range(1, n + 1):
        for c in tree[p]:
            cnt[c] += cnt[p]
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, x, cnt):
    cnt[p] += x
    for c in tree[p]:
        dfs(tree, c, x, cnt)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        query = [(0, 0)] * q
    for i in range(q):
        p, x = map(int, input().split())
        query[i] = (p, x)
    query.sort(key=lambda q: q[0])
    cnt = [0] * (n + 1)
    # for p, x in query:
    #     dfs(tree, p, x, cnt)
    i = 0
    while i < q:
        p = query[i][0]
        x = 0
        while query[i][0] == p:
            x += query[i][1]
            i += 1
            if i == q:
                break
        cnt[p] = x
    for p in range(1, n + 1):
        for c in tree[p]:
            cnt[c] += cnt[p]
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, x, cnt):
    cnt[p] += x
    for c in tree[p]:
        dfs(tree, c, x, cnt)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        query = [(0, 0)] * q
    for i in range(q):
        p, x = map(int, input().split())
        query[i] = (p, x)
    cnt = [0] * (n + 1)
    for p, x in query:
        dfs(tree, p, x, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  Tree[b].append(a)
  
for _ in range(q):
  p,x = map(int, input().split())
  Counter[p] += x

P = [-1 for _ in range(n+1)]  

nodes = [1]
while nodes:
  parent = nodes.pop(0)
  for node_i in Tree[parent]:
    if P[parent]==node_i:
      continue
    P[node_i] = parent
    nodes.append(node_i)
    Counter[node_i] += Counter[parent]

print(*Counter)

Submission status: Wrong Answer

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

n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = sorted(map(int, input().split()))
  Tree[a].append(b)
  
def dfs(i, c, t):
  if t[i]:
    for j in t[i]:
      c[j] += c[i]
      dfs(j, c, t)

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

dfs(1, Counter, Tree)
print(*Counter.values())

Submission status: Wrong Answer

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

n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  
def dfs(i, c, t):
  if t[i]:
    for j in t[i]:
      c[j] += c[i]
      dfs(j, c, t)

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

dfs(1, Counter, Tree)
print(*Counter.values())

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
pathes = []
for _ in range(N-1):
    a, b = map(int, input().split())
    pathes.append((a, b))
childs = [[] for i in range(N+1)]
for a, b in pathes:
    childs[a].append(b)
cnt = [0]*(N+1)
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
nodes = deque()
nodes.append(1)
while len(nodes) != 0:
    node = nodes.popleft()
    for c in childs[node]:
        nodes.append(c)
        cnt[c] += cnt[node]
for i in range(1, N+1):
    print(cnt[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
pathes = []
for _ in range(N-1):
    a, b = map(int, input().split())
    pathes.append((a, b))
pathes.sort(reverse=True)
childs = [[] for i in range(N+1)]
for a, b in pathes:
    childs[a].append(b)
cnt = [0]*(N+1)
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x
nodes = deque()
nodes.append(1)
while len(nodes) != 0:
    node = nodes.pop()
    for c in childs[node]:
        nodes.append(c)
        cnt[c] += cnt[node]
for i in range(1, N+1):
    print(cnt[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q=map(int,input().split())
B=[[] for i in range(N)]
ans=[0]*N
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    B[a].append(b)

for i in range(Q):
    p,x=map(int,input().split())
    p-=1
    d=deque([p])
    while len(d)!=0:
        m=d.pop()
        ans[m]+=x
        for j in B[m]:
            d.append(j)
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

Tree = [0]*(N+1)

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

points = [0]*(N+1)

for j in range(Q):
    p, x = map(int, input().split())#
    points[p] += x
ans = [0]*N
for i in range(1, N+1):
    k = i
    while k >= 1:
        ans[i-1] += points[k]
        if k == 1:
            break
        else:
            k = Tree[k]

print(*ans)

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10 ** 6)

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

graph   = [ [] for _ in range(N) ]
queries = { p: 0 for p in range(N) }

for _ in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)

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

    queries[p - 1] += q

answer = [ 0 ] * N

def dfs(p, acc):
    acc += queries[p]

    answer[p] = acc

    for child in graph[p]:
        dfs(child, acc)

dfs(0, 0)

print(*answer)