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:
import sys
sys.setrecursionlimit(10**6)

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

def dfs(now, total, tree, counter, result):
    if result[now] > 0:
        return
    result[now] = total + counter[now]
    for child in tree[now]:
        dfs(child, result[now], tree, counter, result)

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

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

    for a,b in edge:
        tree[a].append(b)
        tree[b].append(a)

    for p,x in P:
        counter[p] += x

    result = [0]*(N+1)
    dfs(1, 0, tree, counter, result)

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

main()

Submission status: Wrong Answer

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

def dfs(now, total, tree, counter, result):
    if result[now] > 0:
        return
    result[now] = total + counter[now]
    for child in tree[now]:
        dfs(child, result[now], tree, counter, result)

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

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

    for a,b in edge:
        tree[a].append(b)
        tree[b].append(a)

    for p,x in P:
        counter[p] += x

    result = [0]*(N+1)
    dfs(1, 0, tree, counter, result)

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

main()

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a = [0]*(n-1)
for i in range(n-1):
    a[i] = list(map(int,input().split()))
p = [0]*q
for i in range(q):
    p[i] = list(map(int,input().split()))
node = [[] for i in range(n+1)]
for A,B in a:
    node[A].append(B)
s = [0]*(n+1)
for P,X in p:
    s[P] += X
from collections import deque
r = deque([])
c = deque([])
r.append(1)
c.append(s[0])
while r:
    x = r.popleft()
    C = c.popleft()
    s[x] += C
    for z in node[x]:
        r.append(z)
        c.append(s[x])
for i in range(1,n+1):
    print(s[i],end="")
    print(" ",end="")
print("")

Submission status: Wrong Answer

Solution code:
#ABC138D
def main():
    import sys
    N, Q = map(int, sys.stdin.readline().split())
    ab = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    px = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    
    edge = [[] for _ in range(N)]
    #有向グラフ
    for row in ab:
        edge[row[0]-1].append(row[1]-1)
    #print(edge)
    point_list = [0 for i in range(N)]
    for row in px:
        point_list[row[0]-1] += row[1]
    #print(point_list)
    
    sys.setrecursionlimit(1000000)
    visited = [-1 for _ in range(N)]
    visited[0] = 0
    def dfs(p):
        for c in edge[p]:
            if visited[c]==-1:
                visited[c]=0
                point_list[c] +=point_list[p]
                dfs(c)
            
    
    dfs(0)
    #print(visited)
    print(*point_list)
        



if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split(' '))
tree = [[] for i in range(N + 1)]
for i in range(N - 1):
    a, b = map(int, input().split(' '))
    tree[a].append([a,b])
val = [0] * (N + 1)
for i in range(Q):
    p, x = map(int, input().split(' '))
    val[p] += x
ans = [-1] * (N + 1)
ans[1] = val[1]
bfs = deque(tree[1])
while bfs:
    p, n = bfs.popleft()
    if ans[n] == -1:
        ans[n] = val[n] + ans[p]
        for i in tree[n]:
            bfs.append(i)
for i in ans[1:-1]:
    print(i,'',end="")
print(ans[-1])

Submission status: Wrong Answer

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

while len(q)>0:
    nn, np = q.popleft()
    if count[nn] ==0:
        count[nn] = np + point[nn]
        for i in edge[nn]:
            if count[i] == 0:
                q.append((i, count[nn]))

print(" ".join(map(str, count[1::])))

Submission status: Wrong Answer

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

while len(q)>0:
    nn, np = q.popleft()
    count[nn] = np + point[nn]
    for i in edge[nn]:
        if count[i] == 0:
            q.append((i, count[nn]))

print(" ".join(map(str, count[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)]

ab.sort()

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

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

for i in range(Q):
    result[px[i][0]] = result[px[i][0]] + px[i][1]


def dfs(point, result):
    if len(p[point]) == 0:
        return
    else:
        for i in range(len(p[point])):
            next = p[point][i]
            result[next] = result[next] + result[point]
            dfs(next, result)
    return result


result = dfs(1, result)

print(*result[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)]

ab.sort()

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

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

for i in range(Q):
    result[px[i][0]] = result[px[i][0]] + px[i][1]


def dfs(point, result):
    for i in range(len(p[point])):
        next = p[point][i]
        result[next] = result[next] + result[point]
        dfs(next, result)
    return result


result = dfs(1, result)

print(*result[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)]

ab.sort()

p = [[] for i in range(N + 1)]
result = [0 for i in range(N + 1)]

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

for i in range(Q):
    result[px[i][0]] = result[px[i][0]] + px[i][1]


def dfs(point, result):
    for i in range(len(p[point])):
        next = p[point][i]
        result[next] = result[next] + result[point]
        dfs(next, result)
    return result


result = dfs(1, result)

Submission status: Wrong Answer

Solution code:
from collections import deque

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)

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

d = deque()
d.append(0)
while d:
    node = d.popleft()
    for next_node in edge[node]:
        res[next_node] += res[node]
        d.append(next_node)

for c in res:
    print(c, end=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

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)

res = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    d = deque()
    d.append(p-1)
    while d:
        node = d.popleft()
        res[node] += x
        for next_node in edge[node]:
            d.append(next_node)

for c in res:
    print(c, end=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque


def bfs():
  d.append(0)
  trace[0] = 1
 
  while d:
    now = d.pop()
    for next in g[now]:
      if trace[next] == 0:
        trace[next] = 1
        score[next] += score[now]
        d.append(next)


n,q = map(int,input().split())
g = [[] for i in range(n)]

trace = [0]*n#頂点の個数
score = [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)

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

d = deque([])

bfs()
      
print(*score)

Submission status: Wrong Answer

Solution code:
from collections import deque


def bfs():
  d.append(0)
  trace[0] = 1
 
  while d:
    now = d.pop()
    for next in g[now]:
      if trace[next] == 0:
        trace[next] = 1
        score[next] += score[now]
        d.append(next)


n,q = map(int,input().split())
g = [[] for i in range(n)]

trace = [0]*n#頂点の個数
score = [0]*n

for i in range(n-1):#各々の頂点から行ける頂点のリスト
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  g[a].append(b)
  g[a].append(a)

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

bfs()
      
print(*score)

Submission status: Wrong Answer

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

def dfs(now, count, children):
    if children == []:
        return count
    for child in children:
        ans[child] += count+counter[child]
        dfs(child, count+counter[child], tree[child])

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

tree = [[] for _ in range(N+1)]
counter = [0 for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for a,b in edge:
    tree[a].append(b)

for p,x in P:
    counter[p] += x

ans[1] = counter[1]
dfs(1, counter[1], tree[1])

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
 
AB = []
for _ in range(N-1):
    ab = tuple(map(int, input().split()))
    AB.append(ab)
AB = sorted(AB, key=lambda x: (x[1], x[0]))
 
C = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    C[p - 1] += x

for a, b in AB:
    C[a - 1] += C[b - 1]

print(*C)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
tree = [[] for _ in range(N)] 
counters = deque([0] * N)
answer = [0] * N

for _ in range(N-1):
    index, child = map(int, input().split())
    tree[index-1].append(child-1)

for _ in range(Q):
    index, count = map(int, input().split())
    counters[index-1] += count

for i in range(N):
    count = counters.popleft()
    answer[i] += count

    for j in range(len(tree[i])):
        answer[tree[i][j]] += answer[i]

for i in answer:
    print(i, end=' ')

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

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

tree = [[] for _ in range(N)]
count = [0] * N

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

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

d = deque([])


def dfs(p):
    d.append(p)

    while d:
        p = d.pop()

        for np in tree[p]:
            if count[np] == 0:
                count[np] += count[p]
                d.append(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

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

tree = [[] for _ in range(N)]
count = [0] * N

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

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

d = deque([])


def dfs(p):
    d.append(p)

    while d:
        p = d.pop()

        for np in tree[p]:
            if count[np] != 0:
                count[np] += count[p]
                d.append(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

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

tree = [[] for _ in range(N)]
count = [0] * N

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

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

d = deque([])


def dfs(p):
    d.append(p)

    while d:
        p = d.pop()

        for np in tree[p]:
            count[np] += count[p]
            d.append(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit, stdin
setrecursionlimit(10 ** 9)
input = stdin.readline

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

tree = [[] for _ in range(N)]
count = [0] * N

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

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


def dfs(p):
    for np in tree[p]:
        count[np] += count[p]
        dfs(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

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

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

tree = [[] for _ in range(N)]
count = [0] * N

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

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


def dfs(p):

    for np in tree[p]:
        count[np] += count[p]
        dfs(np)


dfs(0)

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

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

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

tree = [[] for _ in range(N)]
subtree = [[] for _ in range(N)]
count = [0] * N

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


def subtree_check(x):
    subtree[x].append(x)

    for nx in tree[x]:
        subtree_check(nx)
        for sx in subtree[nx]:
            subtree[x].append(sx)


subtree_check(0)

for p, x in px:
    for i in subtree[p - 1]:
        count[i] += x

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

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

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

tree_reverse = [[] for _ in range(N)]
subtree = [[] for _ in range(N)]
count = [0] * N

for a, b in ab:
    tree_reverse[b - 1].append(a - 1)


def subtree_check(now, start):
    subtree[now].append(start)

    for t in tree_reverse[now]:
        subtree_check(t, start)


for i in range(N):
    subtree_check(i, i)

for p, x in px:
    for i in subtree[p - 1]:
        count[i] += x

for c in count:
    print(c, end=" ")

print()

Submission status: Wrong Answer

Solution code:
def main():
  N, Q = map(int, input().split())
 
  AB = []
  graph = [[] for _ in range(N)]
  for _ in range(N-1):
      a, b = map(int, input().split())
      a, b = a - 1, b - 1
      AB.append((a, b))
      graph[a].append(b)
      graph[b].append(a)
  AB = sorted(AB, key=lambda x: (x[1], x[0]))

  root = 0
  q = [root]
  parent = [-1] * N
  while q:
      x = q.pop(0)
      p = parent[x]
      for y in graph[x]:
          if y == p:
              continue
          parent[y] = x
          q.append(y)

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

  for a, b in AB:
      if a != parent[b]:
          tmp = a
          a = b
          b = tmp
      C[b] += C[a]

  print(*C)
  

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

AB = []
for _ in range(N-1):
    ab = tuple(map(int, input().split()))
    AB.append(ab)
AB = sorted(AB, key=lambda x: (x[1], x[0]))

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

for a, b in AB:
    C[b - 1] += C[a - 1]

print(*C)

Submission status: Wrong Answer

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

def resolve():
	from collections import deque
	
	n, q = map(int, input().split())
	adj = [[] for i in range(n+1)]
	for i in range(n-1):
		a, b = map(int, input().split())
		adj[a].append(b)
		adj[b].append(a)
	p = [list(map(int, input().split())) for i in range(q)]
	
	ans = [0 for i in range(n+1)]
	tmp = [0 for i in range(n+1)]
	for i in range(q):
		tmp[p[i][0]] += p[i][1]

	for start, point in enumerate(tmp):
		q = deque([start])
		while q:
			node = q.popleft()
			ans[node] += tmp[start]
			for i in list(filter(lambda x: x>node, adj[node])):
				q.append(i)

	for i in ans[1:]:
		print(i, end=" ")

resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	from collections import deque
	
	n, q = map(int, input().split())
	adj = [[] for i in range(n+1)]
	for i in range(n-1):
		a, b = map(int, input().split())
		adj[a].append(b)
		adj[b].append(a)
	p = [list(map(int, input().split())) for i in range(q)]
	
	ans = [0 for i in range(n+1)]
	tmp = [0 for i in range(n+1)]
	for i in range(q):
		tmp[p[i][0]] += p[i][1]

	for start, point in enumerate(tmp):
		q = deque([start])
		while q:
			node = q.popleft()
			ans[node] += tmp[start]
			for i in list(filter(lambda x: x>node, adj[node])):
				q.append(i)

	for i in ans[1:]:
		print(i, end=" ")

resolve()

Submission status: Wrong Answer

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

#各節点の親と自分を頂点とする部分木のカウントの和
parent = [0] * (N+1)
count = [0] * (N+1)
for t in s:
    parent[t[1]] = t[0]
for u in p:
    count[u[0]] += u[1]

dp = [0] * (N+1)
for i in range(1,N+1):
    dp[i] = dp[parent[i]] + count[i]

for i in dp[1:]:
    print(i, end=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())

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

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

visited = [False for i in range(n)]
visited[0] = True

queue = deque()
queue.append(0)
r = [0 for i in range(n)]
r[0] = cnt[0]
while queue:
  x = queue.popleft()
  for i in ki[x]:
    if visited[i] == True:
      continue
    # cnt[i] += cnt[x]
    r[i] = cnt[i] + r[x]
    queue.append(i)
    visited[i] = True
print (*r)

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())

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

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

visited = [False for i in range(n)]
visited[0] = True

queue = deque()
queue.append(0)
while queue:
  x = queue.popleft()
  for i in ki[x]:
    if visited[i] == True:
      continue
    cnt[i] += cnt[x]
    queue.append(i)
    visited[i] = True
print (*cnt)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
edges = [[] for j in range(n+1)]
scores = [0]*(n+1)

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

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

queue = [1]
while queue != []:
  v = queue.pop(0)
  for vv in edges[v]:
    scores[vv] += scores[v]
  queue.extend(edges[v])
  
for i in range(1, n+1):
  print(scores[i])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
parent = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    parent[a].append(b)
    parent[b].append(a)
import queue
check = [0] * n

ans = [0] * n

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

a = ''
qq = queue.Queue()
qq.put(0)
check[0] = 1
while not qq.empty():
    now = qq.get()
    for j, son in enumerate(parent[now]):
        if check[son] == 1:
            continue
        ans[son] += ans[now]
        check[son] = 1
        qq.put(son)
for i in range(n):
    a += str(ans[i]) + ' '
print(a)
print(parent)

Submission status: Wrong Answer

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


ans = [0] * n

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

a = ''
qq = queue.Queue()
qq.put(0)
while not qq.empty():
    now = qq.get()
    for j, son in enumerate(parent[now]):
        ans[son] += ans[now]
        qq.put(son)
for i in range(n):
    a += str(ans[i]) + ' '
print(a)

Submission status: Wrong Answer

Solution code:
#%%
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
n,q = [int(i) for i in readline().split()]

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

add = [0]*n
for _ in range(q):
    p,x = [int(i) for i in readline().split()]
    add[p-1] += x

ans = [0]*n
#ans[0] = add[0]

def dfs(v,p,value): #p: parent of v
    value += add[v]
    ans[v] = value
    for c in g[v]:
        if not c:
            continue
        dfs(c,v,value)

dfs(0,-1,0)

#print(add)

print(*ans)

Submission status: Wrong Answer

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

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0

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

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

add = [0] * N
for i in range(Q):
    p, x = [int(i) for i in input().split()]
    add[p - 1] += x
#%%  
def DFS(start, value):
    value += add[start]
    list_ins[start].value = value
    for c in list_ins[start].children:
        DFS(c, value)
               

DFS(0, 0)
for i in range(N):
    print(list_ins[i].value, end= " ")

Submission status: Wrong Answer

Solution code:
#%%
import sys
sys.setrecursionlimit(10 ** 9)
from collections import deque

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        

def main():
    global instance_list
    N, Q = [int(i) for i in input().split()]
    instance_list = [Node() for _ in range(N)]

    for i in range(N - 1):
        temp1, temp2 = [int(i) - 1 for i in input().split()]
        instance_list[temp1].children.append(temp2)
        instance_list[temp2].parent = temp1

    for i in range(Q):
        start, value = [int(i) for i in input().split()]
        start -= 1
        que = deque()
        instance_list[start].value += value
        for i in instance_list[start].children:
            que.append(i)
        while len(que) > 0 :
            d = que.popleft()
            instance_list[d].value += value
            for i in instance_list[d].children:
                que.append(i)
    

    ans = [instance_list[i].value for i in range(N)]
    print(" ".join(map(str, ans)))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        
def DFS_138(start, value):
     
    instance_list[start].value += value
    
    for i in instance_list[start].children:
        DFS_138(i, value)
    
#%%
def main():
    global instance_list
    N, Q = [int(i) for i in input().split()]
    instance_list = [Node() for _ in range(N)]

    for i in range(N - 1):
        temp1, temp2 = [int(i) - 1 for i in input().split()]
        instance_list[temp1].children.append(temp2)
        instance_list[temp2].parent = temp1

    for i in range(Q):
        start, value = [int(i) for i in input().split()]
        start -= 1
        DFS_138(start, value)
    

    ans = [instance_list[i].value for i in range(N)]
    print(" ".join(map(str, ans)))
#%%
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        
def DFS_138(start, value):
     
    instance_list[start].value += value
    
    for i in instance_list[start].children:
        DFS_138(i, value)
    
#%%
def main():
    global instance_list
    N, Q = [int(i) for i in input().split()]
    instance_list = [Node() for _ in range(N)]

    for i in range(N - 1):
        temp1, temp2 = [int(i) - 1 for i in input().split()]
        instance_list[temp1].children.append(temp2)
        instance_list[temp2].parent = temp1

    for i in range(Q):
        start, value = [int(i) for i in input().split()]
        start -= 1
        DFS_138(start, value)
    

    ans = [instance_list[i].value for i in range(N)]
    print(" ".join(map(str, ans)))
#%%
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

class Node:
      
    def __init__(self):
        self.id = -1
        self.parent = -1
        self.children = []    
        self.value = 0
        
def DFS_138(start, value):
     
    instance_list[start].value += value
    
    for i in instance_list[start].children:
        DFS_138(i, value)
    
#%%
N, Q = [int(i) for i in input().split()]
instance_list = [Node() for _ in range(N)]

for i in range(N - 1):
    temp1, temp2 = [int(i) - 1 for i in input().split()]
    instance_list[temp1].children.append(temp2)
    instance_list[temp2].parent = temp1

for i in range(Q):
    start, value = [int(i) for i in input().split()]
    start -= 1
    DFS_138(start, value)
    

ans = [instance_list[i].value for i in range(N)]
print(" ".join(map(str, ans)))

Submission status: Wrong Answer

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

ans = [0] * n

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

a = ''
for i, son in enumerate(parent):
    for j, s in enumerate(son):
        ans[s] += ans[i]
for i in range(n):
    a += str(ans[i]) + ' '
print(a)

Submission status: Wrong Answer

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

ans = [0] * n

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

a = ''
for i, son in enumerate(parent):
    a += str(ans[i]) + ' '
    for j, s in enumerate(son):
        ans[s] += ans[i]
print(a)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
bond = []
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    if a > b:
        tmp = a
        a = b
        b = tmp
    bond.append((a,b))
bond.sort()
qry = []
for i in range(q):
    p,x = map(int,input().split())
    p -= 1
    qry.append((p,x))
parent = [[] for i in range(n)]
for i, ab in enumerate(bond):
    a,b = ab
    parent[a].append(b)

ans = [0] * n
for i, qx in enumerate(qry):
    q,x = qx
    ans[q] += x
a = ''
for i, son in enumerate(parent):
    a += str(ans[i]) + ' '
    for j, s in enumerate(son):
        ans[s] += ans[i]
print(a)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, Q = map(int, readline().split())
G = [[] for _ in range(N+1)]
score = [0]*(N+1)
for i in range(N-1):
  x,y = map(int, readline().split())
  G[x].append(y)

for _ in range(Q):
  a,p = map(int, readline().split())
  score[a] += p
  
def dfs(a,b):
  score[a] += score[b]
  for x in G[a]:
    dfs(x,a)
    
dfs(1,0)
print(*score[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
root = [[] for _ in range(N + 10)]
counter = [0 for _ in range(N + 10)]
for _ in range(N - 1):
  a, b = map(int, input().split())
  root[a].append(b)
for _ in range(Q):
  p, x = map(int, input().split())
  counter[p] += x
queue = [1]
while queue:
  vertex = queue.pop(0)
  for v in root[vertex]:
    counter[v] += counter[vertex]
    queue.append(v)
for i in range(1, N):
  print('{} '.format(counter[i]), end='')
print(counter[N])