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

ans=[0]*N
for i in range(Q):
    q = deque([p[i]])   # キュー
    ch=[0]*N
    while len(q) > 0:
        pp = q.popleft()     # キュー取りだし（先頭）
        if ch[pp-1]==0:
            ch[pp-1]=1
        l=[i for i, x in enumerate(a) if x == pp]
    
        for u in l:
            q.append(b[u])
        #print(pp,l,q)
    for y in range(N):
        if ch[y]==1:
            ans[y]+=x[i]
    
for i in range(N):
    print(ans[i],end=' ')

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc138/tasks/abc138_d

def main():
    #入力受付
    N,Q = map(int, input().split())

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

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

    #print(connection_dict)
        
    #頂点 pj を根とする部分木に含まれるすべての頂点をあらかじめ求めておく
    #a < bなので番号Nの頂点は末端
    #番号N-1の頂点は末端or番号Nを持つ
    #……
    #番号1の頂点は番号2～Nを持つ
    #という風に求める
    part_tree_dict = {i:{i} for i in range(1,N+1)}
    for i in range(N,1,-1):
        #print(i,connection_dict[i])
        for connection in connection_dict[i]:
            part_tree_dict[i] |= part_tree_dict[connection]
    part_tree_dict[1] = {i for i in range(1,N+1)}
    #print(part_tree_dict)

    #一個ずつ操作する必要はないので、まとめて実施する
    #入力受付
    px_list = [0 for i in range(0,N+1)]
    for i in range(Q):
        p,x = map(int, input().split())
        px_list[p] += x

    #実施
    counter_list = [0 for _ in range(0,N+1)]
    for i in range(1,N+1):
        if px_list[i] != 0:
            for node in part_tree_dict[i]:
                counter_list[node] += px_list[i]

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc138/tasks/abc138_d

def main():
    #入力受付
    N,Q = map(int, input().split())

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

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

    #print(connection_dict)
        
    #頂点 pj を根とする部分木に含まれるすべての頂点をあらかじめ求めておく
    #a < bなので番号Nの頂点は末端
    #番号N-1の頂点は末端or番号Nを持つ
    #……
    #番号1の頂点は番号2～Nを持つ
    #という風に求める
    part_tree_dict = {i:{i} for i in range(1,N+1)}
    for i in range(N,0,-1):
        #print(i,connection_dict[i])
        for connection in connection_dict[i]:
            part_tree_dict[i] |= part_tree_dict[connection]

    #print(part_tree_dict)

    #一個ずつ操作する必要はないので、まとめて実施する
    #入力受付
    px_list = [0 for i in range(0,N+1)]
    for i in range(Q):
        p,x = map(int, input().split())
        px_list[p] += x

    #実施
    counter_list = [0 for _ in range(0,N+1)]
    for i in range(1,N+1):
        if px_list[i] != 0:
            for node in part_tree_dict[i]:
                counter_list[node] += px_list[i]

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc138/tasks/abc138_d
from collections import deque

#入力受付
N,Q = map(int, input().split())

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

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

#print(connection_dict)
    
#頂点 pj を根とする部分木に含まれるすべての頂点をあらかじめ求めておく
#a < bなので番号Nの頂点は末端
#番号N-1の頂点は末端or番号Nを持つ
#……
#番号1の頂点は番号2～Nを持つ
#という風に求める
part_tree_dict = {i:{i} for i in range(1,N+1)}
for i in range(N,0,-1):
    #print(i,connection_dict[i])
    for connection in connection_dict[i]:
        part_tree_dict[i] |= part_tree_dict[connection]

#print(part_tree_dict)

#一個ずつ操作する必要はないので、まとめて実施する
#入力受付
px_list = [0 for i in range(0,N+1)]
for i in range(Q):
    p,x = map(int, input().split())
    px_list[p] += x

#実施
counter_list = [0 for _ in range(0,N+1)]
for i in range(1,N+1):
    if px_list[i] != 0:
        for node in part_tree_dict[i]:
            counter_list[node] += px_list[i]

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

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc138/tasks/abc138_d
from collections import deque

#入力受付
N,Q = map(int, input().split())

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

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

#print(connection_dict)
    
#頂点 pj を根とする部分木に含まれるすべての頂点をあらかじめ求めておく
#a < bなので番号Nの頂点は末端
#番号N-1の頂点は末端or番号Nを持つ
#……
#番号1の頂点は番号2～Nを持つ
#という風に求める
part_tree_dict = {i:{i} for i in range(1,N+1)}
for i in range(N,0,-1):
    #print(i,connection_dict[i])
    for connection in connection_dict[i]:
        part_tree_dict[i] |= part_tree_dict[connection]

#print(part_tree_dict)

#一個ずつ操作する必要はないので、まとめて実施する
#入力受付
px_list = [0 for i in range(0,N+1)]
for i in range(Q):
    p,x = map(int, input().split())
    px_list[p] += x

#実施
counter_list = [0 for _ in range(0,N+1)]
for i in range(1,N+1):
    for node in part_tree_dict[i]:
        counter_list[node] += px_list[i]

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

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    #入力受付
    N,Q = map(int, input().split())

    tree_dict = {i:set() for i in range(1,N+1)}
    counter_list = [0 for i in range(0,N+1)]

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

    #print(tree_dict)
    #print(counter_list)
    search_target_deque = deque()
    search_target_deque_append =  search_target_deque.append

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

        #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
        search_target_deque.clear() #    Q ← 空のキュー
        search_target_deque_append(p) #    v を Q に追加
        is_visited = [0] + [False for i in range(1,N+1)]
        
        while len(search_target_deque) != 0: #    while Q が空ではない do
            v = search_target_deque.popleft() #        v ← Q から取り出す
            counter_list[v] += x
            #        v を処理する
            neighbor_nodes = tree_dict[v]
            for neighbor_node in neighbor_nodes: #        for each v に接続している頂点 i do
                if is_visited[neighbor_node] == False: # if i が未訪問 then
                    is_visited[neighbor_node] = True #                iに訪問済みの印を付ける
                    search_target_deque_append(neighbor_node) #                i を Q に追加

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

Submission status: Wrong Answer

Solution code:
from collections import deque

#入力受付
N,Q = map(int, input().split())

tree_dict = {i:set() for i in range(1,N+1)}
counter_list = [0 for i in range(0,N+1)]

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

#print(tree_dict)
#print(counter_list)

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

    #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
    search_target_deque = deque() #    Q ← 空のキュー
    search_target_deque.append(p) #    v を Q に追加
    is_visited = [0] + [False for i in range(1,N+1)]
    
    while len(search_target_deque) != 0: #    while Q が空ではない do
        v = search_target_deque.popleft() #        v ← Q から取り出す
        counter_list[v] += x
        #        v を処理する
        neighbor_nodes = tree_dict[v]
        for neighbor_node in neighbor_nodes: #        for each v に接続している頂点 i do
            if is_visited[neighbor_node] == False: # if i が未訪問 then
                is_visited[neighbor_node] = True #                iに訪問済みの印を付ける
                search_target_deque.append(neighbor_node) #                i を Q に追加

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

Submission status: Wrong Answer

Solution code:
from collections import deque

#入力受付
N,Q = map(int, input().split())

tree_dict = {i:set() for i in range(1,N+1)}
counter_list = [0 for i in range(0,N+1)]

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

#print(tree_dict)
#print(counter_list)

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

    #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
    search_target_deque = deque() #    Q ← 空のキュー
    search_target_deque.append(p) #    v を Q に追加
    visited_set = set()
    
    while len(search_target_deque) != 0: #    while Q が空ではない do
        v = search_target_deque.popleft() #        v ← Q から取り出す
        counter_list[v] += x
        #        v を処理する
        neighbor_nodes = tree_dict[v]
        for neighbor_node in neighbor_nodes: #        for each v に接続している頂点 i do
            if neighbor_node not in visited_set: # if i が未訪問 then
                visited_set.add(neighbor_node) #                iに訪問済みの印を付ける
                search_target_deque.append(neighbor_node) #                i を Q に追加

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

Submission status: Wrong Answer

Solution code:
#入力受付
N,Q = map(int, input().split())

tree_dict = {i:set() for i in range(1,N+1)}
counter_list = [0 for i in range(0,N+1)]

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

#print(tree_dict)
#print(counter_list)

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

    #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
    stack = list()   #stack = Stack()
    visited_nodes = set()
    visited_nodes.add(p) #start.setVisited()
    stack.append(p)  #stack.push( start )

    while len(stack) != 0: #while not stack.empty():
        #print(stack)
        node = stack[-1]   #    node = stack.top()    
        
        connected_nodes = tree_dict[node] # child = node.findUnvisitedChild()
        unvisited_nodes = list()
        
        for node in connected_nodes:
            if node not in visited_nodes:
                visited_nodes.add(node)       #child.setVisited()
                stack.append(node)            #stack.push(child)        #深さを優先!! まだ隣接点が残っていても放置!!
                break
        else:
            counter_list[stack.pop(-1)] += x  #    stack.pop()          #隣接点がすべて訪問済みにならない限り、stackに残り続ける!!!!!!!!

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

Submission status: Wrong Answer

Solution code:
#入力受付
N,Q = map(int, input().split())

tree_dict = {i:set() for i in range(1,N+1)}
counter_list = [0 for i in range(0,N+1)]

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

#print(tree_dict)
#print(counter_list)

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

    #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
    stack = list()   #stack = Stack()
    visited_nodes = set()
    visited_nodes.add(p) #start.setVisited()
    stack.append(p)  #stack.push( start )

    counter_list[p] += x

    while len(stack) != 0: #while not stack.empty():
        #print(stack)
        node = stack[-1]   #    node = stack.top()    
        
        connected_nodes = tree_dict[node] # child = node.findUnvisitedChild()
        unvisited_nodes = list()
        
        for node in connected_nodes:
            if node not in visited_nodes:
                visited_nodes.add(node)       #child.setVisited()
                stack.append(node)            #stack.push(child)        #深さを優先!! まだ隣接点が残っていても放置!!
                break
        else:
            counter_list[stack.pop(-1)] += x  #    stack.pop()          #隣接点がすべて訪問済みにならない限り、stackに残り続ける!!!!!!!!

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

Submission status: Wrong Answer

Solution code:
#入力受付
N,Q = map(int, input().split())

tree_dict = {i:set() for i in range(1,N+1)}
counter_list = [0 for i in range(0,N+1)]

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

#print(tree_dict)
#print(counter_list)

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

    #深さ優先探索 https://ja.wikipedia.org/wiki/%E6%B7%B1%E3%81%95%E5%84%AA%E5%85%88%E6%8E%A2%E7%B4%A2
    stack = list()   #stack = Stack()
    visited_nodes = set()
    visited_nodes.add(p) #start.setVisited()
    stack.append(p)  #stack.push( start )

    counter_list[p] += x

    while len(stack) != 0: #while not stack.empty():
        #print(stack)
        node = stack[-1]   #    node = stack.top()    
        
        connected_nodes = tree_dict[node] # child = node.findUnvisitedChild()
        unvisited_nodes = list()
        
        for node in connected_nodes:
            if node not in visited_nodes:
                counter_list[node] += x
                visited_nodes.add(node) #child.setVisited()
                stack.append(node)      #stack.push(child)        #深さを優先!! まだ隣接点が残っていても放置!!
                break
        else:
            stack.pop(-1)               #    stack.pop()          #隣接点がすべて訪問済みにならない限り、stackに残り続ける!!!!!!!!

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

Submission status: Wrong Answer

Solution code:
def main():
    n,q=map(int,input().split())
    ab=[list(map(int,input().split())) for _ in range(n-1)]
    tree=[list() for _ in range(n)]
    score=[0]*n
    for a,b in ab:
        a-=1
        b-=1
        tree[a].append(b)
    px=[list(map(int,input().split())) for _ in range(q)]
    for p,x in px:
        p-=1
        score[p]+=x
    add(tree,score)
    print(*score)

def add(tree,score):
    s=tree[0][:]
    for i in s:
        score[i] += score[0]
    while len(s)>0:
        t = s.pop()
        for i in tree[t]:
            s.append(i)
            score[i] += score[t]

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,q=map(int,input().split())
    ab=[list(map(int,input().split())) for _ in range(n-1)]
    tree=[list() for _ in range(n)]
    score=[0]*n
    for a,b in ab:
        a-=1
        b-=1
        tree[a].append(b)
    px=[list(map(int,input().split())) for _ in range(q)]
    for p,x in px:
        p-=1
        add(p,x,tree,score)
    print(*score)

def add(p,x,tree,score):
    score[p] += x
    s=tree[p][:]
    while len(s)>0:
        t = s.pop()
        score[t] += x
        for i in tree[t]:
            s.append(i)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split()) # Nは頂点の数、Qは操作の回数
graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a].append(b)

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

answer = [0] * (N+1)
for start in starts:
    visited = [-1] * (N+1)
    q = deque()
    q.append(start)
    count = counts[start]
    while q:
        node = q.pop()
        answer[node] += count

        next_nodes = graph[node]
        for next in next_nodes:
            if visited[next] != -1:
                continue
            q.append(next)
            visited[next] = 1

print(*answer[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split()) # Nは頂点の数、Qは操作の回数
graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a].append(b)

counts = {}
for _ in range(Q):
    p, x  = map(int, input().split())
    if p not in counts:
        counts[p] = x
    else:
        counts[p] += x

answer = [0] * (N+1)
for start, count in counts.items():

    visited = [-1] * (N+1)
    q = deque()
    q.append(start)
    
    while q:
        node = q.pop()
        answer[node] += count

        next_nodes = graph[node]
        for next in next_nodes:
            if visited[next] != -1:
                continue
            q.append(next)
            visited[next] = 1

print(*answer[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split()) # Nは頂点の数、Qは操作の回数
graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a].append(b)
counts = [0] * (N+1)
for _ in range(Q):
    p, x  = map(int, input().split())
    counts[p] += x

answer = [0] * (N+1)
for start in range(1, N+1):
    count = counts[start]
    if count == 0:
        continue

    visited = [-1] * (N+1)
    q = deque()
    q.append(start)
    
    while q:
        node = q.pop()
        answer[node] += count

        next_nodes = graph[node]
        for next in next_nodes:
            if visited[next] != -1:
                continue
            q.append(next)
            visited[next] = 1

print(*answer[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split()) # Nは頂点の数、Qは操作の回数
graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a].append(b)
counts = [0] * (N+1)
for _ in range(Q):
    p, x  = map(int, input().split())
    counts[p] += x

answer = [0] * (N+1)
for start in range(1, N+1):
    visited = [-1] * (N+1)

    q = deque()
    q.append(start)
    count = counts[start]

    while q:
        node = q.pop()
        answer[node] += count

        next_nodes = graph[node]
        for next in next_nodes:
            if visited[next] != -1:
                continue
            q.append(next)
            visited[next] = 1

print(*answer[1:])

Submission status: Wrong Answer

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


n, q = LI()
point = [0]*n
graph = [[] for i in range(n)]

for i in range(n - 1):
    a, b = LI()
    graph[a-1].append(b)

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

for i, children in enumerate(graph):
    for child in children:
        point[child-1] += point[i]


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

Submission status: Wrong Answer

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


n, q = LI()
nodes = LIR(n-1)
count = [0]*n


def add(node, x):
    count[node - 1] += x
    for i in range(n - 1):
        if nodes[i][0] == node:
            add(nodes[i][1], x)


def main():

    for i in range(q):
        p, x = LI()
        add(p, x)
    print(" ".join(map(str, count)))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

value = [0]*(n+1)
for p,x in px:
    value[p] += x

def dfs(v,add):
    value[v] += add
    for x in graph[v]:
        dfs(x,value[v])

dfs(1,0)
print(*value[1:],end="\t")

Submission status: Wrong Answer

Solution code:
N,Q=list(map(int,input().split()))
l=[[] for i in range(N)]
for i in range(N-1):
   a,b=list(map(int,input().split()))
   l[a-1].append(b-1)
l_c=[0]*N
for i in range(Q):
   a,b=list(map(int,input().split()))
   l_c[a-1]+=b
for i in range(N):
   for j in l[i]:
      l_c[j]+=l_c[i]
print(*l_c)

Submission status: Wrong Answer

Solution code:
n, q = list(map(int, input().split()))
parents = [0] * n

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

    parents[b-1] = a-1

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

    cnt[p-1] += x

counter = [0] * n
for i in range(n):
    counter[i] = cnt[i] + counter[parents[i]]

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

Submission status: Wrong Answer

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


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

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


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


def main():
    global ans
    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        ans[p] += x
    dfs(0)
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


N, Q = map(int, input().split())
to = [[] for _ in range(N)]

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


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


def main():
    counter = [0] * N
    for _ in range(Q):
        seen = [False] * N
        p, x = map(int, input().split())
        p -= 1
        dfs(p, to, seen, counter, x)
    
    print(*counter)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def solve():
    n, q = map(int, input().split())
    ans = [0] * n
    d = {i:1 for i in range(n+1)}
    d[1] = 0
    score = {i:0 for i in range(n+1)}

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

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

    for i in range(1,n+1):
        ans[i-1] = ans[d[i]-1] + score[i]
    
    print(*ans, sep=' ')

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    n, q = list(map(int, input().split(" ")))
    v_list = [[] for i in range(n)]
    for _ in range(n-1):
        a, b = list(map(int, input().split(" ")))
        v_list[a-1].append(b-1)

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

    point_list = [0]*n
    checked_list = [0]*n
    stack = deque([0]) 
    while stack:
        cur = stack.pop()
        checked_list[cur] = 1
        point_list[cur]+=p_x_list[cur]
        for next_i in v_list[cur]:
            if checked_list[next_i]:
                continue
            stack.append(next_i)
            point_list[next_i]+=point_list[cur]
   
    print(*point_list)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    n, q = list(map(int, input().split(" ")))
    v_dict = {i: [] for i in range(n)}
    for _ in range(n-1):
        a, b = list(map(int, input().split(" ")))
        v_dict[a-1].append(b-1)

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

    point_list = [0]*n
    stack = deque([0]) 
    while stack:
        cur = stack.pop()
        point_list[cur]+=p_x_list[cur]
        for next_i in v_dict[cur]:
            if next_i not in stack:
                stack.append(next_i)
                point_list[next_i]+=point_list[cur]
   
    print(" ".join(list(map(str, point_list))))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    n, q = list(map(int, input().split(" ")))
    v_dict = {i: [] for i in range(n)}
    for _ in range(n-1):
        a, b = list(map(int, input().split(" ")))
        v_dict[a-1].append(b-1)

    point_list = [0]*n
    for _ in range(q):
        p, x = list(map(int, input().split(" ")))
        stack = deque([p-1]) 
        while stack:
            cur = stack.pop()
            point_list[cur]+=x
            for next_i in v_dict[cur]:
                stack.append(next_i)
   
    print(" ".join(list(map(str, point_list))))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    n, q = list(map(int, input().split(" ")))
    v_dict = {i: [] for i in range(n)}
    for _ in range(n-1):
        a, b = list(map(int, input().split(" ")))
        v_dict[a-1].append(b-1)

    point_list = [0]*n
    for _ in range(q):
        p, x = list(map(int, input().split(" ")))
        stack = deque([p-1]) 
        while stack:
            cur = stack.pop()
            point_list[cur]+=x
            for next_i in v_dict[cur]:
                stack.append(next_i)
   
    for i in range(n):
        print(point_list[i], end=" ")

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10**9)

n,q=map(int,input().split())
G=[None]*(n+1)
for _ in range(n-1):
  a,b=map(int,input().split())
  if G[a]==None:G[a]=[b]
  else:G[a].append(b)

S=[0]*(n+1)
def dfs(p,x):
  S[p]+=x
  if G[p]!=None:
    for w in G[p]:
      dfs(w,x)

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

print(*S[1:])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
L=[[] for i in range(n+1)]
S=[0 for i in range(n+1)]
V=[0 for i in range(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())
    S[p]+=x


que=[1]
head=0
V[1]=1
while len(que)>head:
    now=que[head]
    head+=1
    for nex in L[now]:
        if V[nex]==0:
            V[nex]=1
            que.append(nex)
            S[nex]+=S[now]

print(" ".join([str(i) for i in S[1:]]))

Submission status: Wrong Answer

Solution code:
def clarify_partial_tree(N, tree, partial_tree):
  for n in reversed(range(N)):
    for child in tree[n]:
      partial_tree[n] = partial_tree[n] | partial_tree[child]
      
def update_counter(p, x):
  for node in partial_tree[p]:
    counter[node] += x

    
N, Q = map(int, input().split(' '))
tree = [{n} for n in range(N)]
partial_tree = [{n} for n in range(N)]
counter = [0 for n in range(N)]

for n in range(N-1):
  a, b = map(int, input().split(' '))
  tree[a-1].add(b-1)
clarify_partial_tree(N, tree, partial_tree)
      
for q in range(Q):
  p, x = map(int, input().split(' '))
  update_counter(p-1, x)
  
print(*counter)

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
N, Q = map(int, input().split())
G = defaultdict(lambda: deque())
for _ in range(N-1):
    a,b = map(int, input().split())
    G[a].append(b)
   
cnt = [0]*(N+1)
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

def dfs(v, cnt):
    seen = [0] * (N+1)
    stack = [v]
    while stack:
        v = stack[-1]
        if G[v]:
            w = G[v].popleft()
            if not seen[w]: # if 訪れていなければ
                cnt[w] += cnt[v]
                seen[w] = 1
                stack.append(w)
        else:
            stack.pop()
    return cnt

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

Submission status: Wrong Answer

Solution code:
import sys, math
from functools import lru_cache
from collections import deque

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

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    N, Q = mi()
    a, b = i2(N-1)
    p, x = i2(Q)

    child = [[] for i in range(N)]
    for i in range(N-1):
        child[a[i]-1].append(b[i]-1)

    parent = [None]*N
    for i in range(N-1):
        parent[b[i]-1] = a[i]-1


    c = [0]*N
    for i in range(Q):
        c[p[i]-1] += x[i]

    @lru_cache(maxsize=None)
    def dfs(i):
        if parent[i] == None:
            return c[i]
        
        return c[i]+dfs(parent[i])

    print(*[dfs(i) for i in range(N)])

if __name__ == '__main__':
    main()