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:
# D
import sys
sys.setrecursionlimit(10**9)                    #再帰の深さブースト

def now2next(root, edge, score, ans):
    childlist = edge[root]
    if childlist:
        ans[root-1] += score[root]
        for child in childlist:
            ans[child-1] += ans[root-1]
            now2next(child, edge, score, ans)
    else:
        ans[root-1] += score[root]
    
n,q = map(int, input().split())
edge = [[] for i in range(n+1)]
score = [0 for i in range(n+1)]
ans = [0 for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    edge[a].append(b)
for req in range(q):
    reqnode, x = map(int, input().split())
    score[reqnode] += x
now2next(1, edge, score, ans)
print(*ans, sep=' ')

Submission status: Wrong Answer

Solution code:
# D
import sys
sys.setrecursionlimit(10**9)                    #再帰の深さブースト

def now2next(root, edge, union):
    childlist = edge[root]
    if childlist:
        for child in childlist:
            union[root].extend(now2next(child, edge, union))
        return union[root]
    else:
        return [root]
    
n,q = map(int, input().split())
edge = [[] for i in range(n+1)]
union = [[i] for i in range(n+1)]
ans = [0 for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    edge[a].append(b)
now2next(1, edge, union)
for req in range(q):
    reqnode, x = map(int, input().split())
    for num in union[reqnode]:
        ans[num-1] += x
print(*ans, sep=' ')

Submission status: Wrong Answer

Solution code:
def o():return map(int,input().split())
n,q=o();a=[[]for i in range(n)];x=[0]*n
for i in range(n-1):u,v=o();a[u-1]+=[v-1]
for i in range(q):
  u,v=o();p=[u-1]
  while p:r=p.pop();x[r]+=v;p+=a[r]
print(*x)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ab = []
for i in range(N-1):
    x,y = map(int,input().split())
    ab.append([x,y])
ab.sort()
ans = [0]*N
for i in range(Q):
    x,y = map(int,input().split())
    ans[x-1] += y
for i in range(N-1):
    ans[ab[i][1]-1] += ans[ab[i][0] - 1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ab = []
for i in range(N-1):
    x,y = map(int,input().split())
    ab.append([x,y])
ab.sort()
ans = [0]*N
for i in range(Q):
    x,y = map(int,input().split())
    ans[x-1] += y
for i in range(N-1):
    ans[ab[i][1]-1] += ans[ab[i][1] - 1]
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
N,Q = map(int,input().split())
adjacent_list = [[] for _ in range(N+1)]
for _ in range(N-1):
    a,b = map(int,input().split())
    adjacent_list[a].append(b)
cnt = [0]*(N+1)
def dfs(v,x):
    cnt[v] += x
    for nv in adjacent_list[v]:
        dfs(nv,x)

for q in range(Q):
    p,x = map(int,input().split())
    dfs(p,x)
print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
T = [[0 for j in range(N)] for i in range(N)]
c = [0] * N

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


def f(u, x):
    if is_visited[u]:
        return
    is_visited[u] = 1
    c[u] += x
    for i in range(N):
        if T[u][i]:
            f(i, x)


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

print(*c)

Submission status: Wrong Answer

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

tree = dict()
parent = [0] * (N+1)
for _ in range(N-1):
  a, b = map(int, input().split())
  parent[b] = a
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

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

counter = [0] * (N+1)

import sys
sys.setrecursionlimit(2500100)

def score(i):
  global counter
  counter[i] = counter[parent[i]] + action[i]
  if i in tree:
    for ch in tree[i]:
      score(ch)

score(1)

ans = ' '.join([str(c) for c in counter[1:]])
print(ans)

Submission status: Wrong Answer

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

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

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

counter = [0] * (N+1)
for i in range(1, N+1):
  counter[i] = counter[tree[i]] + action[i]

ans = ' '.join([str(c) for c in counter[1:]])
print(ans)

Submission status: Wrong Answer

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

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

lc=[0 for i in range(n)]
for i in range(q):
    lc[p[i][0]-1]+=p[i][1]

for i in range(n):
    if i!=0:
        lc[i]+=lc[lp[i]-1]
print(*lc)

Submission status: Wrong Answer

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

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

lc=[0 for i in range(n)]
for i in range(q):
    lc[p[i][0]-1]+=p[i][1]

for i in range(n):
    if i!=0:
        lc[i]+=lc[lp[i]-1]
print(*lc)

Submission status: Wrong Answer

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

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

next_point = []
next_point_cand = []

for i in query:
    next_point.append(i[0])
    while True:
        next_point_cand.clear()
        for j in next_point:
            tree_point[j] += i[1]
            next_point_cand.extend(tree[j])
        next_point.clear()
        next_point.extend(next_point_cand)
        if len(next_point) == 0:
            break

print(' '.join(list(map(str, tree_point))))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n, q = map(int, sys.stdin.readline().split())
counter = [0 for _ in range(n)]
relation = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    relation[a-1].append(b-1)
operate = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

for ope in operate:
    que = deque()
    start, point = ope
    start -= 1
    visited = [False for _ in range(n)]
    visited[start] = True
    counter[start] += point
    que.append(start)
    while que:
        node = que.popleft()
        for child in relation[node]:
            if visited[child] == True: continue
            counter[child] += point
            que.append(child)


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

Submission status: Wrong Answer

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

A=[0 for _ in range(n+1)]
T=[[] for _ in range(n+1)]

for a,b in AB:T[a] +=[b]
for p,x in PX:A[p] +=x

for i in range(n+1):
    for j in T[i]:
        A[j] +=A[i]

print(*A[1:])

Submission status: Wrong Answer

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

def Graph(ab,n):
    up_G=[[0,0] for i in range(n)]#up_G[i]=頂点iの親
    for a,b in ab:
        up_G[b-1]=[a,b]
    return up_G

per=sorted(Graph(ab,n)[1:])
#print(per)
ans=[0]*n
for p,x in px:
    ans[p-1]+=x

for up,ind in per:
    ans[ind-1]+=ans[up-1]
print(*ans)

Submission status: Wrong Answer

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

def Graph(ab,n):
    up_G=[[0,0] for i in range(n)]#up_G[i]=頂点iの親
    for a,b in ab:
        up_G[b-1][0]=a
        up_G[b-1][1]=b
    return up_G

per=sorted(Graph(ab,n))
#print(per)
ans=[0]*n
for p,x in px:
    ans[p-1]+=x

for up,ind in per:
    ans[ind-1]+=ans[up-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]
counter = [0] * N

edge = []
for i in range(N-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    edge.append((a, b))

for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    counter[p] += x

edge.sort()
for i in range(N-1):
    counter[edge[i][1]] += counter[edge[i][0]]
print(' '.join(map(str, counter)))

Submission status: Wrong Answer

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

#グラフの連結成分を調べる
def Graph(ab):
    up_G=[[0,0] for i in range(n)]#up_G[i]=頂点iの親
    for a,b in ab:
        up_G[b-1]=[a,b-1]
    return up_G
up_G=Graph(ab)
ans=[0]*n
for p,x in px:
    ans[p-1]+=x
#print(ans)
up_G=sorted(up_G)
for i in range(1,n):
    ans[up_G[i][1]]+=ans[up_G[i][0]-1]
print(*ans)
#print(up_G)

Submission status: Wrong Answer

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

#グラフの連結成分を調べる
def Graph(ab):
    up_G=[[0,0] for i in range(n)]#up_G[i]=頂点iの親
    for a,b in ab:
        up_G[b-1]=[a,b-1]
    return up_G
up_G=Graph(ab)
ans=[0]*n
for p,x in px:
    ans[p-1]+=x
#print(ans)
up_G=sorted(up_G)
for i in range(1,n):
    ans[i]+=ans[up_G[i][0]-1]
print(*ans)
#print(up_G)

Submission status: Wrong Answer

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

#グラフの連結成分を調べる
def Graph(ab):
    up_G=[0]*n#up_G[i]=頂点iの親
    for a,b in ab:
        up_G[b-1]=a
    return up_G
up_G=Graph(ab)
ans=[0]*n
for p,x in px:
    ans[p-1]+=x
#print(ans)

for i in range(1,n):
    ans[i]+=ans[up_G[i]-1]
print(*ans)

Submission status: Wrong Answer

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

#グラフの状態を調べる
def Graph(ab):
    G=[[] for i in range(n)]
    up_G=[0]*n#up_G[i]=頂点iの親
    for a,b in ab:
        G[a-1].append(b)
        G[b-1].append(a)
        up_G[b-1]=a
    return G,up_G

#木上のDFSテンプレ
from collections import deque
def dfs(G,v,p):
    #(G,v,p)=(グラフ(木の情報),探索中の頂点,vの親)
    #vの子の頂点を決めて、vをpに移す
    global ans
    log=[False]*n
    q=deque([])
    q.append([v,p])
    log[v-1]=True
    ans[v-1]+=x
    while q:
        V,P=q[-1]
        for new_v in G[V-1]:
            #new_vが頂点の時はアウト
            if new_v==P:continue
            if log[new_v-1]:continue
            ans[new_v-1]+=x
            log[new_v-1]=True
            q.append([new_v,V])
            break
        else:
            q.pop()

G,up_G=Graph(ab)
ans=[0]*n
#print(G,up_G,sep='\n')

for p,x in px:
    dfs(G,p,up_G[p-1])
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, Q = MAP()
ab = [LIST() for _ in range(N-1)]
px = [LIST() for _ in range(Q)]

tree = defaultdict(list)
for a, b in ab:
	tree[a-1].append(b-1)

dic = defaultdict(int)

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

ans = [0]*N

checked = [0]*N
checked[0] = 1

q = deque([])
q.append(0)

while q:
	n = q.popleft()
	ans[n] += dic[n]
	for node in tree[n]:
		if checked[node]:
			continue
		ans[node] += ans[n]
		checked[node] = 1
		q.append(node)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
def solve():
	N, Q = MAP()
	ab = [LIST() for _ in range(N-1)]
	tree = defaultdict(list)
	par = [-1]*N
	par[0] = 0
	for a, b in ab:
		tree[a-1].append(b-1)
		tree[b-1].append(a-1)
		par[b-1] = a-1
 
	px = [LIST() for _ in range(Q)]
 
	ans = [0] * N  # 答え入れる
 
	for p, x in px:
		ans[p-1] += x
 
	
	for i in range(1, N):
		ans[i] += ans[par[i]]
 
	print(*ans, sep=" ")
 
if  __name__ == "__main__":
	solve()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, Q = MAP()
ab = [LIST() for _ in range(N-1)]
px = [LIST() for _ in range(Q)]

tree = defaultdict(list)
for a, b in ab:
	tree[a].append(b)

dic = defaultdict(int)

for p, x in px:
	dic[p] += x

ans = [0]*(N+1)

checked = [0]*(N+1)
checked[1] = 1

def DFS(n):
	ans[n] += dic[n]
	for node in tree[n]:
		if checked[node]:
			continue
		ans[node] += ans[n]
		checked[node] = 1
		DFS(node)

DFS(1)
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, Q = MAP()
ab = [LIST() for _ in range(N-1)]
px = [LIST() for _ in range(Q)]

tree = defaultdict(list)
for a, b in ab:
	tree[a].append(b)

dic = defaultdict(int)

for p, x in px:
	dic[p] += x

ans = [0]*(N+1)

checked = [0]*(N+1)

def DFS(n):
	ans[n] += dic[n]
	for node in tree[n]:
		if checked[node]:
			continue
		ans[node] += ans[n]
		checked[node] = 1
		DFS(node)

DFS(1)
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, pre):
  for i in G[s]:
    if(i == pre):
      continue
    val[pre] += val[s]
    dfs(i, s)

#X = str(input()).split()
#a = [int(x) for x in input().split()]


sys.setrecursionlimit(10**7)

N, Q = map(int, input().split())
G = [[] for i in range(N)]
Ope = [0]*N

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

val = [0]*N

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

dfs(0, 0)

val = map(str, val)
vall = ' '.join(val)

print(vall)

Submission status: Wrong Answer

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

visited[0] = 1
stack = [0]
while stack:
  v = stack.pop()
  for c in tree[v]:
    if visited[c]:
      continue
    visited[c] = 1
    ans[c] += ans[v]
    stack.append(c)
    
print(" ".join(str(i) for i in ans))

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

from collections import defaultdict
from collections import deque

def main():
	n,q=map(int,input().split())
	d=defaultdict(list)
	
	for i in range(n-1):
		a,b=map(int,input().split())
		d[a].append(b)
	
	cnt=[0]*(n+1)
	
	for i in range(q):
		p,x=map(int,input().split())
		cnt[p]+=x
	
	data=[]
	
	
	q=deque([1])
	sumi=set()
	
	while q:
		now=q.popleft()
		sumi.add(now)
		kouho=d[now]
		for i in kouho:
			if i not in sumi:
				q.append(i)
				cnt[i]+=cnt[now]
	
	print(*cnt[1:])
	
	
	
	
	
	
	
	
	
	
	
	
	

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

from collections import defaultdict

def main():
	n,q=map(int,input().split())
	d=defaultdict(list)
	
	for i in range(n-1):
		a,b=map(int,input().split())
		d[a].append(b)
	
	cnt=[0]*(n+1)
	
	for i in range(q):
		p,x=map(int,input().split())
		cnt[p]+=x
	
	data=[]
	
	for k,l in d.items():
		data.append([k,l])
	
	data.sort()
	
	for k,l in data:
		for i in l:
			cnt[i]+=cnt[k]
	
	print(*cnt[1:])
	
	
	
	
	
	
	
	
	
	
	
	
	

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
import sys
N,Q=map(int,input().split())
G=defaultdict(list)
for i in range(N-1):
  u,v=map(int,input().split())
  G[u].append(v)
  
def dfs(G,S):    # S:始点
  stack=deque()
  stack.append(S)
  seen=[]
  seen.append(S)
  leaf=[]
  prev=[1]*(N)
  while len(stack)!=0:
    u=stack.pop()
    if len(G[u])==0:
      leaf.append(u)
    for v in G[u]:
      if v not in seen:
        seen.append(v)
        stack.append(v)
        prev[v-1]=u
  return [leaf,prev]

l=dfs(G,1)
leaf,prev=l[0],l[1]

def v_child(leaf,prev):
  child=[[i] for i in range(N)]  # 自分自身を子に持つ
  stack=deque()
  prev_checked=[0]*N
  for v in leaf:
    stack.append(v)
    prev_checked[v-1]=v-1
  while len(stack)!=0:
    v=stack.pop()
    if v==1:
      pass
    else:
      child[v-1]+=child[prev_checked[v-1]]  # 確認
      u=prev[v-1]
      prev_checked[u-1]=v-1     
      stack.append(u)
  for i in range(N):
    if i==0:
      child[i]=[i for i in range(N)]
    else:
      child[i]=list(set(child[i]))
  return child
    
child=v_child(leaf,prev)
counter=[0]*N

for i in range(Q):
  v,x=map(int,input().split())
  for u in child[v-1]:
    counter[u]+=x
    
for i in range(N):
  counter[i]=str(counter[i])
print(' '.join(counter))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

N,Q=map(int,input().split())
G=defaultdict(list)
for i in range(N-1):
  u,v=map(int,input().split())
  G[u].append(v)
  
def dfs(G,S):    # S:始点
  stack=deque()
  stack.append(S)
  seen=[]
  seen.append(S)
  while len(stack)!=0:
    u=stack.pop()
    for v in G[u]:
      if v not in seen:
        seen.append(v)
        stack.append(v)
  return seen
        
  
child=[[] for _ in range(N)]
counter=[0]*N

for S in range(1,N+1):
  if S==1:
    child[S-1]=[i for i in range(N)]   # 1が与えられる木の根であることはわかっているので
  else:
    child[S-1]=dfs(G,S)
  
for i in range(Q):
  v,x=map(int,input().split())
  for u in child[v-1]:
    counter[u-1]+=x
    
for i in range(N):
  counter[i]=str(counter[i])
print(' '.join(counter))

Submission status: Wrong Answer

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

n,m=map(int,input().split())
a=[[] for i in range(n)]
for i in range(n-1):
    q,w=map(int,input().split())
    a[q-1].append(w-1)
#print(a)
res=[0 for i in range(n)]
#res2=[0 for i in range(n)]
def f(q,w):
    global res,a
    #res[q]+=w
    if len(a[q])==0:
        pass
    else:
        #print(a[q],q)
        for i in a[q]:
            res[i]+=w

for i in range(m):
    q,w=map(int,input().split())
    res[q-1]+=w
for i in range(n):
    f(i,res[i])

print(*res)

Submission status: Wrong Answer

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

n,m=map(int,input().split())
a=[[] for i in range(n)]
for i in range(n-1):
    q,w=map(int,input().split())
    a[q-1].append(w-1)
#print(a)
res=[0 for i in range(n)]
res2=[0 for i in range(n)]
def f(q,w):
    global res,a
    res[q]+=w
    if len(a[q])==0:
        pass
    else:
        #print(a[q],q)
        for i in a[q]:
            f(i,w)

for i in range(m):
    q,w=map(int,input().split())
    res2[q-1]+=w
for i in range(n):
    f(i,res2[i])

print(*res)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
# 親の番号 < 子の番号
edge = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
vertices = [0] * N
for _ in range(Q):
    p,x = map(int,input().split())
    vertices[p-1] += x

from collections import deque

que = deque([0])
while que:
    v = que.popleft()
    cost = vertices[v]
    for w in edge[v]:
        vertices[w] += cost
        que.append(w)

print(*vertices, sep=" ")

Submission status: Wrong Answer

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

n,m=map(int,input().split())
a=[[] for i in range(n)]
for i in range(n-1):
    q,w=map(int,input().split())
    a[q-1].append(w-1)
#print(a)
res=[0 for i in range(n)]

def f(q,w):
    global res,a
    res[q]+=w
    if len(a[q])==0:
        pass
    else:
        #print(a[q],q)
        for i in a[q]:
            f(i,w)

for i in range(m):
    q,w=map(int,input().split())
    f(q-1,w)

print(*res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

class TreeNode():
    def __init__(self, num, x):
        self.num = num
        self.val = x
        self.children = []
        self.progeny_num = []

def get_progeny(tree_node):
    tree_node.progeny_num = [tree_node.num]
    if len(tree_node.children) == 0:
        return 
    for child in tree_node.children:
        get_progeny(child)
        tree_node.progeny_num.extend(child.progeny_num)
    return

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

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

PX = []
for q in range(Q):
    [p, x] = [int(i) for i in input().split()]
    PX.append((p, x))

# ツリーとツリー辞書を作成
tree_vertex = {}
for (a, b) in AB:
    a_node = tree_vertex[a] if a in tree_vertex else TreeNode(a, 0)
    b_node = tree_vertex[b] if b in tree_vertex else TreeNode(b, 0)
    a_node.children.append(b_node)

    if a not in tree_vertex:
        tree_vertex[a] = a_node
    if b not in tree_vertex:
        tree_vertex[b] = b_node

# 子孫リストを作成
get_progeny(tree_vertex[1])

for (p, x) in PX:
    for progeny_num in tree_vertex[p].progeny_num:
        tree_vertex[progeny_num].val += x

#for k, v in tree_vertex.items():
#    print('k: {}'.format(k))
#    print('val: {}'.format(v.val))
#    for child in v.children:
#        print('child: {}'.format(child))
#    print('progeny_num: {}'.format(v.progeny_num))
    
ans = ''
for v in tree_vertex.values():
    ans += str(v.val) + ' '

print(ans)

Submission status: Wrong Answer

Solution code:
# グラフの作成(無向グラフでは#を消す)
from collections import deque

def graph(N):# Nのゆうこうグラフの作成
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = [int(x) for x in input().split()]
        graph[a-1].append(b-1)
    return graph

def cnt(Q,graph):
    cnt = [0] * (N)
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        cnt[p-1] += x
    que = deque([0])
    while que:
        s = que.popleft()
        for i in graph[s]:
            cnt[i] += cnt[s]
            que.append(i) 
    return(cnt)

N, Q = map(int, input().split())
A = graph(N)
print(*cnt(Q, A))

Submission status: Wrong Answer

Solution code:
# グラフの作成(無向グラフでは#を消す)
from collections import deque

def graph(N):# Nのゆうこうグラフの作成
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = [int(x) for x in input().split()]
        graph[a-1].append(b-1)
    return graph

def cnt(Q,graph):
    cnt = [0] * (N)
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        cnt[p-1] += x
    que = deque([0])
    while que:
        s = que.popleft()
        for i in graph[s]:
            cnt[i] += cnt[s]
            que.append(i) 
    return(cnt)

N, Q = map(int, input().split())
A = graph(N)
print(cnt(Q, A))

Submission status: Wrong Answer

Solution code:
# 深さ優先探索（行きがけ）

# グラフの作成(無向グラフでは#を消す)
from collections import deque

def graph(N):# Nの有効グラフの作成
    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = [int(x) for x in input().split()]
        graph[a-1].append(b-1)
    return graph

def cnt(Q,graph):
    cnt = [0] * (N)
    for _ in range(Q):
        p, x = [int(x) for x in input().split()]
        cnt[p-1] += x
    que = deque([0])
    while que:
        s = que.popleft()
        for i in graph[s]:
            cnt[i] += cnt[s]
            que.append(i) 
    return(cnt)

N, Q = map(int, input().split())
A = graph(N)
print(cnt(Q, A))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
def input():
    return sys.stdin.readline()[:-1]
N , Q = map(int,input().split())
graph = [[] for _ in range(N)]
# The directly connecting nodes of each node are written in "graph".
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
    # graph can be

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

def dfs(now , prev = -1):
    for nx in graph[now]:
        if nx == prev:
            continue
        point[nx] += point[now]
        dfs(nx, now)

dfs(0)
print(*point)

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]
p=[0 for i in range(n+1)]
edge=[[] for i in range(n+1)]
que=deque([])
que.append(1)
visited=[False for i in range(n+1)]
visited[0]=True
for i,j in ab:
  edge[i].append(j)
  edge[j].append(i)
for i in range(q):
  a,b=map(int,input().split())
  p[a]=b

while que:
  v=que.pop()
  visited[v]=True
  for nv in edge[v]:
    if visited[nv]==True:
      continue
    p[nv]+=p[v]
    que.append(nv)
p.pop(0)
print(*p)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
pointTree = [0 for i in range(n)]
tree = [[] for i in range(n)]
p = [0 for i in range(n)]


def solve(now,point,prev):
    pointTree[now] += point
    for node in tree[now]:
        if node == prev:
            continue
        solve(node,point+p[node],now)


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

print(0)
exit(0)
    
solve(0,p[0],-1)
s = ''
for i in range(n):
    s += str(pointTree[i])+' '
print(s[:-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
tree = []
cnt = []


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

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

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

for i in range(1,n):
    for p in tree[i]:
        cnt[p] += cnt[i]

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(99999)
input = sys.stdin.readline
tree = []
cnt = []

def add_cnt(p,x):
    cnt[p] += x
    for b in tree[p]:
        add_cnt(b,cnt[p])

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(99999)
input = sys.stdin.readline
tree = []
cnt = []

def add_cnt(p,x):
    cnt[p] += x
    for b in tree[p]:
        add_cnt(b,x)

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

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

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
K=[[] for _ in range(n)]
V=[False]*n
P=[0]*n
for i in range(n-1):
   a,b=map(int,input().split())
   K[a-1].append(b-1)
   K[b-1].append(a-1)
for i in range(q):
   q,x=map(int,input().split())
   P[q-1]=x
que=deque([0])
while que:
   s=que.popleft()
   V[s]=True
   for ni in K[s]:
      if V[ni]==True:
         continue
      P[ni]+=P[s]
      que.append(ni)
print(*P)