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:
#!/usr/bin/env python3
from collections import deque

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

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

a = [0] * -~n
b = [True] * -~n
for i in range(q):
    p, x = map(int, input().split())
    a[p] += x

d = deque([1])
b[1] = False

while len(d):
    p = d.popleft()
    for i in graph[p]:
        if b[i]:
            a[i] += a[p]
            b[i] = False
            d.append(i)
print(*a[1:])

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, q  = MAP()
g = [0]*(n+1)
ans = [0]*(n+1)
for i in range(n-1):
    a, b = MAP()
    g[max(a,b)] = min(a,b)
for i in range(q):
    p, x = MAP()
    ans[p] += x
for i in range(1,n+1):
    ans[i] += ans[g[i]]
for i in range(1, n+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, q  = MAP()
g = [0]*(n+1)
ans = [0]*(n+1)
for i in range(n-1):
    a, b = MAP()
    g[b] = a
for i in range(q):
    p, x = MAP()
    ans[p] += x
for i in range(1,n+1):
    ans[i] += ans[g[i]]
for i in range(1, n+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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)
PX = [tuple(map(int, input().split())) for _ in range(Q)]
counter = [0] * (N + 1)
for px in PX:
    p, x = px
    counter[p] += x


q = deque()
visited = [-1] * (N+1)
q.append(1)

while q:
    parent = q.pop()
    nodes = graph[parent]
    for node in nodes:
        if visited[node] == -1:
            visited[node] = 1
            q.append(node)
            counter[node] += counter[parent]

for i in range(1, N+1):
    print(counter[i], end=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

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)
PX = [tuple(map(int, input().split())) for _ in range(Q)]
counter = [0] * (N + 1)
q = deque()

for px in PX:
    visited = [-1] * (N+1)
    p, x = px

    q.append(p)
    while q:
        parent = q.pop()
        if visited[parent] == -1:
            counter[parent] += x

        nodes = graph[parent]
        for node in nodes:
            if visited[node] == -1:
                visited[node] = 1
                q.append(node)
                counter[node] += x


for i in range(1, N+1):
    print(counter[i], end=' ')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,q = map(int,input().split())
d = defaultdict(list)
for i in range(n-1):
    a,b = map(int,input().split())
    d[b].append(a)
# print(d)
nums = [-10**7]+[0]*n
for i in range(q):
    p,x = map(int,input().split())
    nums[p] += x

dp = [-1]+[0]*n
for i in range(1,n+1):
    dp[i] += sum([dp[j] for j in d[i]]) + nums[i]
print(*dp[1:])

Submission status: Wrong Answer

Solution code:
N,Q = list(map(int,input().split()))
ad={}
point={}
P = {}
color={}
child={}

for n in range(N):
    ad[n+1]=set()
    child[n+1]=set()
    color[n+1] = -1
    point[n+1] = 0
    P[n+1] = 0

for n in range(N-1):
    a,b = list(map(int,input().split()))
    ad[a].add(b)
    ad[b].add(a)
    
for q in range(Q):
    p,x = list(map(int,input().split()))
    point[p] += x


#DFS
from collections import deque
start=1
visit = []
stack = deque([start])

i=0
while len(stack) > 0:
    start = stack[-1]
    color[start] = 0
    
    for v_idx in range(len(ad[start])+1):
        if v_idx == len(ad[start]):
            v = stack.pop()
            color[v] = 1
            visit.append(v)
            
            for s in stack:
                child[s].add(v)
            break
            
        else:
            v = list(ad[start])[v_idx]
            if color[v] == -1:
                color[v] = 0
                stack.append(v)  
                start = v
                break
#                 child[start].add(v)

print(stack,start,color)

Submission status: Wrong Answer

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

for i in range(n):
  for j in ki[n-i-1]:
    for k in ki[j]:
      ki[n-i-1].append(k)

p=[0]*n
for i in range(q):
  c,d=[int(x) for x in input().split()]
  for j in ki[c-1]:
    p[j]+=d
  p[c-1]+=d

s = ' '.join([str(n) for n in p])
print(s)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

def main():
    N, Q = MI()
    #木
    tree = [[]]*N
    for i in range(N-1):
        a,b = MI()
        if len(tree[a-1]) == 0:
            tree[a-1] = [b-1]
        else:
            tree[a-1].append(b-1)
    #カウンター保存用、初期化
    counter = [0]*N
    score = [0]*N
    dq = deque([0])
    for i in range(Q):
        p, x = MI()
        counter[p-1] += x
    score[0] = counter[0]
    while dq:
        temp = dq.pop()
        for index, num in enumerate(tree[temp]):
            score[num] = score[temp]+counter[num]
            dq.append(num)
    score = [str(i) for i in score]
    print(' '.join(score))
    return None

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

def main():
    N, Q = MI()
    #木
    tree = [[]]*N
    visit = [[]]*N
    for i in range(N-1):
        a,b = MI()
        if len(tree[a-1]) == 0:
            tree[a-1] = [b-1]
            visit[a-1] = [True]
        else:
            tree[a-1].append(b-1)
            visit[a-1].append(True)
    #カウンター保存用、初期化
    counter = [0]*N
    score = [0]*N
    dq = deque([0])
    for i in range(Q):
        p, x = MI()
        counter[p-1] += x
    score[0] = counter[0]
    while dq:
        temp = dq.pop()
        for index, num in enumerate(tree[temp]):
            if visit[temp][index]:
                score[num] = score[temp]+counter[num]
                visit[temp][index] = False
                dq.append(num)
    score = [str(i) for i in score]
    print(' '.join(score))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


class Tree:
    def __init__(self, n):
        self.n = n
        self.node = [0] * self.n
        self.subtree = [set() for i in range(self.n)]

    def build(self, edges):
        self.child = {i: set() for i in range(self.n)}

        for t in edges:
            self.child[t[0]-1].add(t[1]-1)

    def add(self, p, x):
        self.node[p-1] += x

    def ans(self, i):
        a = self.node[i]
        for k in self.child[i]:
            self.node[k] += a
            self.ans(k)

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

tree = Tree(n)
tree.build(edges)

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


tree.ans(0)
print(" ".join(map(str, tree.node)))

Submission status: Wrong Answer

Solution code:
import re
import sys
from collections import *
from functools import lru_cache
from heapq import *
from itertools import *
from statistics import *
from string import (
    ascii_letters as letters,
    ascii_lowercase as lcletters,
    ascii_uppercase as ucletters,
    digits
)

hexdigits='01234567890ABCDEFabcdef'
inf=float('inf')

sys.setrecursionlimit(2**31-1)

rfs=lambda:rl().split()
ri=lambda:int(rl())
ris=lambda:list(map(int,rfs()))
rls=lambda:list(sys.stdin)
rl=lambda:next(sys.stdin)
rrs=lambda:list(map(str.strip,rls()))
rs=lambda:rl().strip()
rt=lambda:sys.stdin.read()
rg=lambda:list(map(list,rrs()))
rig=lambda:[[*map(int,x.split())] for x in rls()]

def n4(i,j):
    return [
                (i-1,j),
        (i,j-1),        (i,j+1),
                (i+1,j),
    ]

def n8(i,j):
    return [
        (i-1,j-1),(i-1,j),(i-1,j+1),
        (  i,j-1),        (  i,j+1),
        (i+1,j-1),(i+1,j),(i+1,j+1),
    ]

inb=lambda i,j,m,n:0<=i<m and 0<=j<n

###########################################

N,Q=ris()

adj=defaultdict(list)
for _ in range(N-1):
    a,b=ris()
    adj[a].append(b)

ans=[0]*N
def dfs(u,x):
    ans[u-1]+=x
    for v in adj[u]:
        dfs(v,x)

for _ in range(Q):
    a,b=ris()
    dfs(a,b)

print(*ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,q = map(int, input().split())
a = [[0]*2 for _ in range(n-1)]
p = np.zeros((q,2))
counter = [0]*n
for i in range(n-1):
    a[i][0],a[i][1] = map(int,input().split())
for j in range(q):
    x, y = map(int,input().split())
    counter[x-1] += y
a.sort()
for k in range(n-1):
    counter[int(a[k][1])-1] += counter[int(a[k][0])-1]
ans =[str(a) for a in counter]
ans = ' '.join(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,q = map(int, input().split())
a = np.zeros((n-1,2))
p = np.zeros((q,2))
counter = [0]*n
for i in range(n-1):
    a[i][0],a[i][1] = map(int,input().split())
for j in range(q):
    x, y = map(int,input().split())
    counter[x-1] += y
a.sort()
for k in range(n-1):
    counter[int(a[k][1])-1] += counter[int(a[k][0])-1]
ans =[str(a) for a in counter]
ans = ' '.join(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    N,Q =map(int, input().split())
    WHITE = 0 # 未訪問
    GRAY = 1 # 訪問したが、スタックに残っている
    BLACK = 2 # 訪問完了済み

    M = [[0]*N for _ in range(N)] # n*nの隣接行列の作成
    color = [WHITE]*N  # colorの初期化

    def dfs_visit(u): # uの訪問処理
        global trial
        color[u] = GRAY
        trial += point[u]
        point[u] = trial
        for v in range(N):
            trial = point[u]
            if M[u][v] == 0:
                continue
            if color[v] == WHITE: # 深さがあれば再帰的に処理をする
                dfs_visit(v)
        color[u] = BLACK # uに関する全ての深さを探索しきった処理

    def dfs():
        for u in range(N):
            global trial
            trial = 0
            if color[u] == WHITE: # 未訪問のuを始点にする
                dfs_visit(u)

    for _ in range(1,N): # 隣接行列の作成
        a,b = input().split()
        a = int(a)-1  # 0オリジンにする
        b = int(b)-1   # 0オリジン
        M[a][b] = 1

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

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

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
score = [0]*N

#　木を作成
C = [[] for _ in range(N)]
for _ in range(N-1):
    parent, child = map(int, input().split())
    C[parent-1].append(child-1)
    
# スコア表を作る
S = [0]*N
for _ in range(Q):
    node, score = map(int, input().split())
    S[node-1] += score

# DFS
stack = deque([0])
done = [0]*N
done[0] = 1

while len(stack) > 0:
    now = stack.pop()
    
    for new in C[now]:
        if done[new] == 0:
            done[new] = 1
            stack.append(new)
            S[new] += S[now]
    
print(*S)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
score = [0]*N

#　木を作成
C = [[] for _ in range(N)]
for _ in range(N-1):
    parent, child = map(int, input().split())
    C[parent-1].append(child-1)
    
# スコア表を作る
S = [0]*N
for _ in range(Q):
    node, score = map(int, input().split())
    node -= 1
    S[node] += score

# DFS
stack = deque([0])
done = [0]*N
while len(stack) > 0:
    now = stack.pop()
    
    for new in C[now]:
        if done[new] == 0:
            done[new] = 1
            stack.append(new)
            S[new] += S[now]
    
print(*S)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
score = [0]*N

#　木を作成
C = [[None] for _ in range(N)]
for _ in range(N-1):
    parent, child = map(int, input().split())
    C[parent-1].append(child-1)
    
# スコア表を作る
S = [0]*N
for _ in range(Q):
    node, score = map(int, input().split())
    node -= 1
    S[node] += score

# DFS
stack = deque([0])
done = [0]*N
while len(stack) > 0:
    now = stack.pop()
    
    for new in C[now]:
        if new == None:
            continue
        if done[new] == 0:
            done[new] = 1
            stack.append(new)
            S[new] += S[now]
    
print(*S)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
score = [0]*N

# 木を作る # O(N)
ki = [[] for _ in range(N)]
for _ in range(N-1):
    parent, child = map(int, input().split())
    ki[parent-1].append(child-1)
    # ki[child-1].append(parent-1)
    
# O(N)
def DFS(N, ki, node, point, score):
    stack = deque([node])
    ki_done = [0]*N
    score[node] += point
    ki_done[node] = 1
    
    while len(stack) > 0:
        #print(stack)
        now = stack.pop()
        #print(now)
        for new in ki[now]:
            if new > N-1:
                continue
            if ki_done[new] == 0:
                stack.append(new)
                score[new] += point
                ki_done[new] = 1
                
    return score

for _ in range(Q):
    node, point = map(int, input().split())
    node -= 1
    score = DFS(N, ki, node, point, score)
    #print(score)

print(*score)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
score = [0]*N

# 木を作る # O(N)
ki = [[] for _ in range(N)]
for _ in range(N-1):
    parent, child = map(int, input().split())
    ki[parent-1].append(child-1)
    # ki[child-1].append(parent-1)
    
# O(N)
def DFS(N, ki, node, point, score):
    stack = deque([node])
    ki_done = [0]*N
    score[node] += point
    ki_done[node] = 1
    
    while len(stack) > 0:
        #print(stack)
        now = stack.pop()
        #print(now)
        for new in ki[now]:
            if new > N-1:
                continue
            if ki_done[new] == 0:
                stack.append(new)
                score[new] += point
                ki_done[new] = 1
                
    return score

for _ in range(Q):
    node, point = map(int, input().split())
    node -= 1
    score = DFS(N, ki, node, point, score)
    #print(score)

ans = str(score[0])
for s in score:
    ans += " "
    ans += str(s)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = lambda:sys.stdin.readline().strip()
    
    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)]

    from collections import deque

    tree = [[] for i in range(N)]
    for a,b in AB:
        tree[a-1].append(b-1)
    
    node_li = [[i] for i in range(N)]
    for i in range(N):
        queue = deque(tree[i])
        visited = [False]*N

        while queue:
            x = queue.pop()
            if visited[x]==True:
                continue

            for node in tree[x]:
                queue.append(node)

            visited[x]=True
            node_li[i].append(x)

    score_li = [0]*N
    for p,x in PX:
        for i in node_li[p-1]:
            score_li[i]+=x

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

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = lambda:sys.stdin.readline().strip()
    
    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)]

    from collections import deque

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

    def tree_bfs(start,tree):
        
        node_li = [start]
        queue = deque([t for t in tree[start] if t>start])
        visited = [False]*N
        
        while queue:
            x = queue.pop()
            if visited[x] is True:
                continue    
            for node in tree[x]:
                if node>start:
                    queue.append(node)
            
            node_li.append(x)
            visited[x]=True

        return node_li

    ans_li = [0]*N
    
    for p,x in PX:
        node_li = tree_bfs(p-1,tree)

        for node in node_li:
            ans_li[node]+=x
    
    print(' '.join(map(str,ans_li)))

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
lis=[]
for _ in range(n-1):
    a,b=map(int,input().split())
    lis.append((a,b))
p_list=[0]*n
for _ in range(q):
    p,x=map(int,input().split())
    p_list[p-1]+=x
lis.sort()
for t in lis: p_list[t[1]-1]+=p_list[t[0]-1]
for i in p_list: print(i,end=" ")

Submission status: Wrong Answer

Solution code:
import copy

n, q = map(int, input().split())
x_sum = [0] * (n+1)
adj_list = [[] for _ in range(n+1)]

counter = [0] * (n+1)

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

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

stack = [1]
counter[1] = x_sum[1]
while stack:
    parent = stack.pop()
    children = adj_list[parent]
    if children:
        for child in children:
            counter[child] = x_sum[child] + counter[parent]
            stack.append(child)

counter = [str(x) for x in counter]
print(" ".join(counter[1:]))

Submission status: Wrong Answer

Solution code:
import copy

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

adj_list = [[] for _ in range(n+1)]
counter = [0] * (n+1)

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

def op(p, x):
    counter[p] += x
    stack = copy.deepcopy(adj_list[p])
    while stack:
        n = stack.pop()
        counter[n] += x
        l = copy.deepcopy(adj_list[n])
        if len(l):
            for i in l:
                stack.append(i)

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

counter = [str(x) for x in counter]
print(" ".join(counter[1:]))

Submission status: Wrong Answer

Solution code:
n, q = (int(xi) for xi in input().split())



r = [[] for xi in range(n+1)]

for xi in range(n-1):
    a0, b0 = (int(xi) for xi in input().split())
    r[b0].append(a0)
    r[a0].append(b0)


p = [0 for xi in range(n+1)]
for xi in range(q):
    p0, x0 = (int(xi) for xi in input().split())
    p[p0] += x0


h = r[1][:]
for xi in r[1]:
    r[xi].remove(1)

for xi in range(n*100000):
    tgt = h[0]
    h.extend(r[tgt])

    for yi in r[tgt]:
        r[yi].remove(tgt)

    h.remove(h[0])

    if len(h)==0: break
    if xi == 10: break


h = r[1][:]
for yi in r[1]:
    p[yi] += p[1]

for xi in range(n*100000):
    tgt = h[0]
    h.extend(r[tgt])

    for yi in r[tgt]:
        p[yi] += p[tgt]

    h.remove(h[0])

    if len(h)==0: break
    if xi == 10: break

print(*p[1:])

Submission status: Wrong Answer

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

ans=['?']+[0]*n

def dfs(x,y):
  ans[x]+=y
  for i in range(n-1):
    if tree[i][0]==x:
      dfs(tree[i][1],y)

for i,j in point:
  dfs(i,j)

ans2=''
for i in range(1,n+1):
  ans2+=str(ans[i])
  ans2+=' '

print(ans2)

Submission status: Wrong Answer

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template
N,Q = inputlist()
tree = [[] for _ in range(N+1)]
for i in range(N-1):
    a,b = inputlist()
    tree[a].append(b)
    tree[b].append(a)
ans = [0 for _ in range(N+1)]
for i in range(Q):
    p,x = inputlist()
    ans[p] += x

for i in range(1,N+1):
    for x in tree[i]:
        if i > x:
            continue
        ans[x] += ans[i]
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict


def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    Children = defaultdict(set)
    for a, b in AB:
        Children[a].add(b)

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

    Q = deque([1])
    Visited = [False] * (n + 1)
    while len(Q) > 0:
        v = Q.popleft()
        for vc in Children[v]:
            if not Visited[vc]:
                Ans[vc] += Ans[v]
                Q.append(vc)
                Visited[vc] = True

    print(*Ans[1:])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict


def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    C = defaultdict(set)
    for a, b in AB:
        C[a].add(b)

    Ans = [0] * n
    for p, x in PX:
        Ans[p - 1] += x

    Q = deque([1])
    while len(Q) > 0:
        v = Q.popleft()
        for ve in C[v]:
            Ans[ve - 1] += Ans[v - 1]
            Q.append(ve)

    print(*Ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict


def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    C = defaultdict(set)
    P = dict()
    for a, b in AB:
        C[a].add(b)
        P[b] = a

    Ans = [0] * n
    for p, x in PX:
        Ans[p - 1] += x

    Q = deque([1])
    while len(Q) > 0:
        v = Q.popleft()
        for ve in C[v]:
            Ans[ve - 1] += Ans[P[ve] - 1]

        Q.extend(C[v])

    print(*Ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict


def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    C = defaultdict(set)
    P = dict()
    for a, b in AB:
        C[a].add(b)
        P[b] = a

    S = defaultdict(int)
    for p, x in PX:
        S[p] += x

    Ans = [0] * n
    Ans[0] = S[1]
    Q = deque([1])
    while len(Q) > 0:
        v = Q.popleft()
        for ve in C[v]:
            Ans[ve - 1] += S[ve] + Ans[P[ve] - 1]

        Q.extend(C[v])

    print(*Ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

from collections import deque
que = deque([[0, counter[0]]])
while que:
    node, point = que.pop()
    for nn in G[node]:
        counter[nn] += point
        que.append([nn, counter[nn]]) 

print(*counter)

Submission status: Wrong Answer

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

nodes = [[] for _ in range(n)]
from collections import deque
for i in range(n):
    que = deque([i])
    while que:
        node = que.pop()
        for nn in G[node]:
            if len(nodes[nn]) > 0:
                nodes[i] += nodes[nn]
            else:
                nodes[i].append(nn)
                que.append(nn)

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

print(*counter)

Submission status: Wrong Answer

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

nodes = [[] for _ in range(n)]
from collections import deque
for i in range(n):
    que = deque([i])
    while que:
        node = que.pop()
        for nn in G[node]:
            nodes[i].append(nn)
            que.append(nn)

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

print(*counter)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x:int(x)-1, input().split())
    G[a].append(b)
from collections import deque
counter = [0]*n
for _ in range(q):
    p, x = map(int, input().split())
    q = deque([p-1])
    while q:
        node = q.pop()
        counter[node] += x
        for nn in G[node]:
            q.append(nn)

print(*counter)

Submission status: Wrong Answer

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

n,q = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n-1)]
PX = [tuple(map(int, input().split())) for i in range(q)]
#print(AB)

C = [[] for i in range(n)] #Cはグラフ表示
for a,b in AB:
  C[b-1].append(a-1)
  C[a-1].append(b-1)
Q = [0] * n
for p,x in PX:
  Q[p-1] += x
#print(Q)
  
STACK = [0]
VISITED = [0] * n
ANS = [0] * n
ANS[0] = Q[0]
VISITED[0] = 1

while STACK:
  d = STACK.pop()
  for i in C[d]:
    if VISITED[d]:
      continue
    else:
      VISITED[i] = 1
      ANS[i] = ANS[d] + Q[i]
      STACK.append(i)

print(*ANS)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
graph = [[] for _ in range(N)]

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

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

que = deque([0])
parent = [-1]*N
while que:
    node = que.pop()
    for v in graph[node]:
        que.append(v)
        ans[v] += ans[node]

print(*ans)

Submission status: Wrong Answer

Solution code:

from collections import deque


from collections import deque

N, Q = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N - 1)]
Y = [list(map(int, input().split())) for _ in range(Q)]

tree =[[] for _ in range(N + 1)]
for a, b in X:
    tree[a].append(b)
    tree[b].append(a)

ctr = [0] * (N + 1)
for p, x in Y:
    ctr[p] += x

ans = [0] * (N + 1)
ans[1] = ctr[1]
q = deque([1])
while q:
    u = q.popleft()
    for v in tree[u]:
        if ans[v] == 0:
            ans[v] = ans[u] + ctr[v]
            q.append(v)

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

N,Q=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(N-1)]
P=[list(map(int,input().split())) for _ in range(Q)]

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

for i in G:
    v=i[0]
    #gr[v-1]=[*gr[v-1],i[1]]
    gr[v-1].append(i[1])
    
cnt=[0]*N

def dfs(p,x):
    cnt[p-1]+=x
    for n in gr[p-1]:
        dfs(n,x)
    return

for p,x in P:
    dfs(p,x)
print(' '.join(map(str,cnt)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

N,Q=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(N-1)]
P=[list(map(int,input().split())) for _ in range(Q)]

gr=[[]]*N
for i in G:
    v=i[0]
    gr[v-1]=[*gr[v-1],i[1]]

cnt=[0]*N

def dfs(p,x):
    cnt[p-1]+=x
    for n in gr[p-1]:
        dfs(n,x)
    return

for p,x in P:
    dfs(p,x)
print(' '.join(map(str,cnt)))