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())
dist = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    dist[a - 1].append(b - 1)
value = []
cnt = [0 for i in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    value.append([p - 1, x])

def dfs(root, addvalue):
    global cnt
    pos = deque([root])

    while len(pos) > 0:
        u = pos.popleft()
        cnt[u] += addvalue
        for i in dist[u]:
            pos.append(i)

for i in value:
    dfs(i[0], i[1])

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque

n, q = map(int, input().split())
dist = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    dist[a - 1].append(b - 1)
value = []
cnt = [0 for i in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    value.append([p - 1, x])

def dfs(root, addvalue):
    global cnt
    pos = deque([root])

    while len(pos) > 0:
        u = pos.popleft()
        cnt[u] += addvalue
        for i in dist[u]:
            pos.append(i)

for i in value:
    dfs(i[0], i[1])

for i in cnt:
    print(i, end = " ")

Submission status: Wrong Answer

Solution code:
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)]

graph = [[] for _ in range(N + 1)]
for a, b in X:
    graph[a].append(b)
    
ctr = [0] * (N + 1)
for p, x in Y:
    ctr[p] += x

ans = [0] * (N + 1)
for i in range(1, N + 1):
    ans[i] += ctr[i]
    for j in graph[i]:
        ctr[j] += ctr[i]
        
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

n, q = map(int, input().split())
dist = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    dist[a - 1].append(b - 1)
value = []
cnt = [0 for i in range(n)]
for i in range(q):
    p, x = map(int, input().split())
    value.append([p - 1, x])

def dfs(pos, addvalue):
    cnt[pos] += addvalue
    for i in dist[pos]:
        dfs(i, addvalue)

for i in value:
    dfs(i[0], i[1])
    
for i in cnt:
    print(i, end = " ")

Submission status: Wrong Answer

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

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


def dfs(x):
    # カウンタ加算
    C[x[0]-1] += x[1]

    # 部分木にも加算
    for e in E:
        if e[0] == x[0]:
            dfs([e[1], x[1]])


for x in X:
    dfs(x)

print(*C)

Submission status: Wrong Answer

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

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


def dfs(x):
    # カウンタ加算
    C[x[0]-1] += x[1]

    # 部分木にも加算
    for e in E:
        if e[0] == x[0]:
            dfs([e[1], x[1]])


for x in X:
    dfs(x)

print(C)

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

import sys
sys.setrecursionlimit(500000)

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    n,q = map(int,input().strip().split())
    G=[[] for _ in range(n)]
    for i in range(0,n-1):
        temp1,temp2 = map(lambda x: int(x) - 1,input().strip().split())
        G[temp1].append(temp2)
        # G[temp2].append(temp1)
    
    px=[0 for _ in range(n)]
    for _ in range(q):
       temp1, temp2 = map(int,input().strip().split()) 
       px[temp1-1]+=temp2

    #
    eprint('px ',end=':\n')
    eprint(px)

    # 木の頂点から順番に頂点を見ていき，頂点j
    # 頂点iについて，その子すべてにカウンタを"配る"
    imos=[0 for _ in range(n)]
    Q = Queue()

    # 木の頂点から順番に頂点を見ていき，頂点j
    Q.put(0)
    while not(Q.empty()):
        v = Q.get()
        imos[v] += px[v]
        for u in G[v]: # uは，「今見ているノード」の「子ノード」を表す
            imos[u] += imos[v]
            Q.put(u)
    eprint('imos ',end=':\n')
    eprint(imos)
    
    #
    for value in imos:
        print(value,end=" ")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, Q=map(int, input().split())
import sys
sys.setrecursionlimit(10**6)
tree=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  tree[a-1].append(b-1)

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

ans=[0]*N
def dfs(start, pre, preval):
  addval=preval+add[start]
  ans[start]+=addval
  for next_node in tree[start]:
    if next_node==pre:
      continue
    dfs(next_node, start,addval)
dfs(0,-1,0)  
for a in ans:
  print(a, end=" ")

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())
idx = lambda idx_: idx_ - 1
dict = {}
ans = [0]*n
for _ in range(n-1):
    a_, b_ = map(int, input().split())
    if a_ not in dict: dict[a_] = []
    dict[a_].append(b_)
for _ in range(q):
    p_, x_ = map(int, input().split())
    ans[idx(p_)] += x_
que = deque()
keys = list(dict.keys())
keys.sort()
for k in keys:
    if k not in dict: continue
    for i in dict[k]:
        ans[idx(i)] += ans[idx(k)]
print(*ans)

Submission status: Wrong Answer

Solution code:
# D - Ki

from collections import deque

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

graph = [[] for _ in range(n)]
cnt = [0]*n

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

d = deque()
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    d.append(p)
    while len(d)>0:
        v = d.popleft()
        cnt[v] += x
        for i in graph[v]:
            if i > v:
                d.append(i)

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
score = [0] * N
link = [[] for _ in range(N)]

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

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

for i in range(N):
    x = score[i]
    for j in link[i]:
        score[j] += x

print(*score)

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
score=[0]*(N)
link=[[] for _ in range(N)]

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

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

for i in range(N):
  x=score[i]
  for j in link[i]:
    score[j]+=x

print(*score)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)
n, q = map(int, input().split())
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

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


def dfs(parent):
    visited[parent] = 1
    for child in edges[parent]:
        if visited[child] == 1:
            continue
        else:
            counter[child] += counter[parent]
            dfs(child)


dfs(0)
print(*counter)

Submission status: Wrong Answer

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

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

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

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

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

print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

parent_child = defaultdict(list)

has_parent = set()
for _ in range(N - 1):
    a, b = map(int, input().split())
    parent_child[a].append(b)
    has_parent.add(b)

no_parent = set([i + 1 for i in range(N)]) - has_parent

query_weight = defaultdict(int)
for _ in range(Q):
    q, x = map(int, input().split())
    query_weight[q] += x


dq = deque([])
ans = [0] * N
used = set()

for c in no_parent:
    dq.append(c)
    used.add(c)

while dq:
    node = dq.popleft()
    children = parent_child[node]
    ans[node - 1] += query_weight[node]

    for c in children:
        ans[c - 1] += ans[node - 1]

        if c in used:
            continue

        dq.append(c)
        used.add(c)

ans = [str(w) for w in ans]
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

parent_child = defaultdict(list)

has_parent = set()
for _ in range(N - 1):
    a, b = map(int, input().split())
    parent_child[a].append(b)
    has_parent.add(b)

no_parent = set([i + 1 for i in range(N)]) - has_parent

query_weight = defaultdict(int)
for _ in range(Q):
    q, x = map(int, input().split())
    query_weight[q] += x


dq = deque([])
ans = [0] * N
used = set()

for c in no_parent:
    dq.append(c)
    used.add(c)

while dq:
    node = dq.popleft()
    children = parent_child[node]
    ans[node - 1] += query_weight[node]

    print(node, ans[node - 1])

    for c in children:
        ans[c - 1] += ans[node - 1]

        if c in used:
            continue

        dq.append(c)
        used.add(c)

ans = [str(w) for w in ans]
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
def main():
    n,q = map(int,input().split())
    ki = {}
    for i in range(1,n+1):
        ki[i]=[]
    for i in range(n-1):
        a,b = map(int,input().split())
        ki[a]+=[b]

    ans = [0 for i in range(n)]
    for i in range(q):
        p,x = map(int,input().split())
        ans[p-1]+=x
    s = [1]
    while len(s)>0:
        i = s.pop(0)
        for j in ki[i]:
            ans[j-1]+=ans[i-1]
            s.append(j)

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,q = map(int,input().split())
    ki = {}
    for i in range(1,n+1):
        ki[i]=[]
    for i in range(n-1):
        a,b = map(int,input().split())
        ki[a]+=[b]

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,q = map(int,input().split())
    ki = {}
    for i in range(1,n+1):
        ki[i]=[]
    for i in range(n-1):
        a,b = map(int,input().split())
        ki[a]+=[b]
    print(ki)

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

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import numpy as np
    n,q = map(int,input().split())
    ki = {}
    for i in range(1,n+1):
        ki[i]=[]
    for i in range(n-1):
        a,b = map(int,input().split())
        ki[a]+=[b]
    for i in range(n):
        k = n-i
        for j in ki[k]:
            ki[k]+=ki[j]

    Q = {}
    for i in range(q):
        p,x = map(int,input().split())
        if p not in Q.keys():
            Q[p] = x
        else:
            Q[p] += x
    ans = [0 for i in range(n)]
    for p,x in Q.items():
        ans[p-1]+=x
        for k in ki[p]:
            ans[k-1]+=x
    print(' '.join(map(str,ans)))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,q = map(int,input().split())
    ki = {1:[]}
    for i in range(n-1):
        a,b = map(int,input().split())
        if a not in ki.keys():
            ki[a] = [b]
        else:
            ki[a]+=[b]

    ans = [0 for i in range(n)]
    for i in range(q):
        p,x = map(int,input().split())
        ans[p-1]+=x
        s = [p]
        while len(s)>0:
            nv = s.pop(0)
            if nv in ki.keys():
                for v in ki[nv]:
                    ans[v-1]+=x
                    s.append(v)
    print(' '.join(map(str,ans)))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
checked = [False]*(N+1)
tree = {}
count = {}
depth = {}
for i in range(1,N+1):
  tree[i] =[]
  count[i] = 0
  depth[i] = 1 
for i in range(N-1):
  a,b = map(int,input().split())
  if a != 1 and len(tree[a]) == 0 and checked[b] == True:
    tmp = a
    a = b
    b = tmp
    
  depth[b] = depth[a]+1 
  checked[b] = True  
  tree[a].append(b)

for i in range(Q):
  p,x = map(int,input().split())
  count[p] += x
  
depth = sorted(depth.items(), key=lambda x: x[1])
depth = list(zip(*depth))[0]
for i in depth:
  for j in range(len(tree[i])): 
    count[tree[i][j]] += count[i]
    
print(" ".join(map(str,count.values())))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
checked = [False]*(N+1)
tree = {}
count = {}
for i in range(1,N+1):
  tree[i] =[]
  count[i] = 0
for i in range(N-1):
  a,b = map(int,input().split())
  if a != 1 and len(tree[a]) == 0 and checked[b] == True:
    print(a,b)
    tmp = a
    a = b
    b = tmp
  checked[b] = True  
  tree[a].append(b)

for i in range(Q):
  p,x = map(int,input().split())
  count[p] += x
  
for i in range(1,N+1):
  for j in range(len(tree[i])): 
    count[tree[i][j]] += count[i]
    
print(" ".join(map(str,count.values())))

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())

tree = {}
count = {}
for i in range(1,N+1):
  tree[i] =[]
  count[i] = 0
for i in range(N-1):
  a,b = map(int,input().split())
  tree[a].append(b)

for i in range(Q):
  p,x = map(int,input().split())
  count[p] += x
  
for i in range(1,N+1):
  if len(tree[i]) == 0:
    continue
  for j in range(len(tree[i])): 
    count[tree[i][j]] += count[i]
    
print(" ".join(map(str,count.values())))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,q=mii()
mat = [[] for _ in range(n)]

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

cnt=[0]*n

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

ans=[0]*n

def dfs(point,now,parent):
    ans[now]+=point+cnt[now]

    for visit in mat[now]:
        if visit==parent:continue
        dfs(ans[now],visit,now)
dfs(0,0,0)
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,q=mii()
mat = [[] for _ in range(n)]

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

cnt=[0]*n

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

ans=[0]*n

def dfs(point,now):
    ans[now]+=point+cnt[now]

    for visit in mat[now]:
        dfs(ans[now],visit)
dfs(0,0)
for i in ans:
    print(i)

Submission status: Wrong Answer

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

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

from collections import deque
q = deque()
q.append(0)
visited = [False]*N
visited[0] = True
while len(q) > 0:
    index = q.pop()
    
    for e in edge[index]:
        if visited[e]:
            continue
        node[e] += node[index]
        visited[e] = True
        q.append(e)
print(*node)

Submission status: Wrong Answer

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

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

from collections import deque
q = deque()
q.append(0)
visited = [False]*N
while len(q) > 0:
    index = q.pop()
    visited[index] = True
    for e in edge[index]:
        if visited[e]:
            continue
        node[e] += node[index]
        q.append(e)
print(*node)

Submission status: Wrong Answer

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

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

from collections import deque
q = deque()
q.append(0)
visited = [False]*N
while len(q) > 0:
    index = q.popleft()
    visited[index] = True
    for e in edge[index]:
        if visited[e]:
            continue
        node[e] += node[index]
        q.append(e)
print(*node)

Submission status: Wrong Answer

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

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

from collections import deque
q = deque()
q.append(0)
while len(q) > 0:
    index = q.popleft()
    for e in edge[index]:
        node[e] += node[index]
        q.append(e)
print(*node)

Submission status: Wrong Answer

Solution code:
# 初期入力 input = sys.stdin.readline ＋　PyPy3
import sys
input = sys.stdin.readline
N,Q = (int(x) for x in input().split())
aa=0  
bb=0

a_set =set()
ki_kouzou_dic ={i:set() for i in range(1,N+1)}

#木の構造を入力
for i in range(1,N): #直接つながっているところ。
    aa,bb = (int(x) for x in input().split())
    if bb in a_set: #親子判定 頂点3⇒頂点2もありうる
        ki_kouzou_dic[bb].add(aa)
        a_set.add(aa)
    else:
        ki_kouzou_dic[aa].add(bb)
        a_set.add(bb)

# ポイント入力
point={i:0 for i in range(1,N+1)}
pp=0 ; xx=0
for _ in range(Q):
    pp,xx = (int(x) for x in input().split())
    point[pp] += xx

#ポイント計算（根⇒葉に向かって累積和）
for ki_i,ki_v in ki_kouzou_dic.items():
    for v_v in ki_v:
        point[v_v] += point[ki_i]

print(*point.values())

Submission status: Wrong Answer

Solution code:
# 初期入力　input=sys.stdin.readline入れてみた
import sys
input = sys.stdin.readline
N,Q = (int(x) for x in input().split())
aa=0  
bb=0

a_set =set()
ki_kouzou_dic ={i:set() for i in range(1,N+1)}

#木の構造を入力
for i in range(1,N): #直接つながっているところ。
    aa,bb = (int(x) for x in input().split())
    if bb in a_set: #親子判定 頂点3⇒頂点2もありうる
        ki_kouzou_dic[bb].add(aa)
        a_set.add(aa)
    else:
        ki_kouzou_dic[aa].add(bb)
        a_set.add(bb)

# ポイント入力
point={i:0 for i in range(1,N+1)}
pp=0 ; xx=0
for _ in range(Q):
    pp,xx = (int(x) for x in input().split())
    point[pp] += xx

#ポイント計算（根⇒葉に向かって累積和）
for ki_i,ki_v in ki_kouzou_dic.items():
    for v_v in ki_v:
        point[v_v] += point[ki_i]

print(*point.values())

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q = (int(x) for x in input().split())
connected = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = (int(x) for x in input().split())
    connected[a-1].append(b)
    connected[b-1].append(a)
counter = [0] * N
for _ in range(Q):
    p,x = (int(y) for y in input().split())
    counter[p-1] += x
ans = counter
q = deque([(1,0)])
searched = [0] * N
while q:
    a,p = q.popleft()
    if searched[a-1] == 0:
        ans[a-1] += ans[p-1]
        searched[a-1] = 1
        for child in connected[a-1]:
            q.append((child,a))
print(*ans)

Submission status: Wrong Answer

Solution code:
# 初期入力
N,Q = (int(x) for x in input().split())
aa=0  
bb=0

a_set =set()
ki_kouzou_dic ={i:set() for i in range(1,N+1)}

#木の構造を入力
for i in range(1,N): #直接つながっているところ。
    aa,bb = (int(x) for x in input().split())
    if bb in a_set: #親子判定 頂点3⇒頂点2もありうる
        ki_kouzou_dic[bb].add(aa)
        a_set.add(aa)
    else:
        ki_kouzou_dic[aa].add(bb)
        a_set.add(bb)

# ポイント入力
point={i:0 for i in range(1,N+1)}
pp=0 ; xx=0
for _ in range(Q):
    pp,xx = (int(x) for x in input().split())
    point[pp] += xx

#ポイント計算（根⇒葉に向かって累積和）
for ki_i,ki_v in ki_kouzou_dic.items():
    for v_v in ki_v:
        point[v_v] += point[ki_i]

print(*point.values())

Submission status: Wrong Answer

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

n,q=map(int,input().split())
pairs=list(tuple(map(lambda x:int(x)-1,input().split())) for _ in range(n-1))
point=list(tuple(map(lambda x:int(x)-1, input().split())) for _ in range(q))

node=[[] for _ in range(n)]
parent=[None]*n
parent[0]=0
res=[0]*n

for p in pairs:
  node[p[0]].append(p[1])
  node[p[1]].append(p[0])
  
for s in point:
  res[s[0]]+=(s[1]+1)
    
for i in range(n):
  for c in node[i]:
    if parent[c]==None:
      parent[c]=i
      res[c]+=res[i]
      
print(*res)

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
L = []
for i in range(N-1):
    a,b = map(int,input().split())
    L.append([a,b])
C = [0 for _ in range(N+1)]
for j in range(Q):
    p,x = map(int,input().split())
    C[p] += x
for i in range(len(L)):
    C[L[i][1]] += C[L[i][0]]
print(*C[1:N+1])

Submission status: Wrong Answer

Solution code:
from collections import deque


def dfs(G, start):
    S = deque()
    S.append(start)
    while len(S) > 0:
        x, parent = S.pop()
        Counter[x] += Counter[parent]
        for g in G[x]:
            if g == parent:
                continue
            S.append((g, x))
            # print((g, x))


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

Counter = [0 for _ in range(N + 1)]

for p, x in PX:
    Counter[p] += x

dfs(G, (1, 0))
# print(G)
Counter = [str(a) for a in Counter]
print(" ".join(Counter))

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
    node = [0 for _ in range(N)]
    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x

    que = queue.Queue()
    que.put(0)
    res = [0 for _ in range(N)]
    while not que.empty():
        s = que.get()
        res[s] += node[s]
        for ns in edge[s]:
            res[ns] += res[s]
            que.put(ns)

    print(*res)

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
L = [[i] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    L[a].append(b)
    for j in range(b):
        if a in L[j] and not b in L[j]:
            L[j].append(b)
ans = [0 for _ in range(N+1)]
for i in range(Q):
    p,x = map(int,input().split())
    for j in range(len(L[p])):
        ans[L[p][j]] += x
print(*ans[1:N+1])

Submission status: Wrong Answer

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

root=[[1]]
for i in range(N-1):
    ex=0
    for j in range(len(root)):
        if L[i][0] in root[j]:
            root[j].append(L[i][1])
        if L[i][0]==root[j][0]:
            ex += 1
    if ex==0:
        root.append(L[i])

point=[0]*N

for l in range(Q):
    n,p=map(int,input().split())
    ex=0
    for k in range(len(root)):
        if root[k][0]==n:
            for s in range(len(root[k])):
                point[root[k][s]-1] += p
            ex += 1
    if ex==0:
        point[n-1] += p

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

Submission status: Wrong Answer

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


def DFS(num,point):
    D[num]=point+Q[num]
    color[num]="gray"
    for i in M[num]:
        if color[i]=="white":
            DFS(i,D[num])
    color[num]="black"


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

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

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

color=["white" for _ in range(n+1)]
D=[0]*(n+1)

DFS(1,0)
print(*D[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
N, Q = map(int, input().split())
ab = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]
graph = [[] for _ in range(N)]
for a, b in ab:
  graph[a] += [b]
dist = [0 for _ in range(N)]
for p, x in px:
  dist[p-1] += x

def dfs(u, pre):
  for v in graph[u]:
    if v != pre:
      dist[v] += dist[u]
      dfs(v, u)


dfs(0, -1)
dist = list(map(str, dist))
print(" ".join(dist))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
N, Q = map(int, input().split())
ab = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N-1)]
px = [list(map(lambda x:int(x)-1, input().split())) for _ in range(Q)]
graph = [[] for _ in range(N)]
for a, b in ab:
  graph[a] += [b]
dist = [0 for _ in range(N)]
for p, x in px:
  dist[p] += x+1

def dfs(u, pre):
  for v in graph[u]:
    if v != pre:
      dist[v] += dist[u]
      dfs(v, u)

  return 

dfs(0, -1)
dist = list(map(str, dist))
print(" ".join(dist))

Submission status: Wrong Answer

Solution code:
from collections import deque


def dfs(G, start, x):
    S = deque()
    child = deque()
    VL[start - 1] = 1
    Counter[start - 1] += x
    S.append(start)
    while len(S) > 0:
        node = S.pop()
        child.clear()
        for g in G:
            if g[0] == node and VL[g[1] - 1] == 0:
                child.append(g[1])
        if len(child) > 0:
            for c in child:
                VL[c - 1] = 1
                S.append(c)
                Counter[c - 1] += x


N, Q = map(int, input().split())
G = [tuple(map(int, input().split())) for _ in range(N - 1)]
PX = [tuple(map(int, input().split())) for _ in range(Q)]
VL = [0 for _ in range(N)]
Counter = [0 for _ in range(N)]

for p, x in PX:
    dfs(G, p, x)
    VL = [0 for _ in range(N)]

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