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:
# -*- coding utf-8 -*-
from collections import defaultdict

MOD = 10 ** 9 + 7
N, Q = map(int, input().split())

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

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

cur_node = set()
cur_node.add(1)
while len(cur_node) != 0:
    next_node = set()
    for ni in cur_node:
        for nj in tree[ni]:
            counter[nj] += counter[ni]
            next_node.add(nj)
    cur_node = next_node

print(' '.join((str(counter[i]) for i in range(1, N + 1))))

Submission status: Wrong Answer

Solution code:
from collections import deque


class Node:
    def __init__(self, parent, weight):
        self.parent = parent
        self.weight = weight


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


G = [Node(0, 0) for _ in range(N + 1)]

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

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

for i in range(1, N + 1):
    G[i].weight += G[G[i].parent].weight

print(" ".join([str(G[i].weight) for i in range(1, N + 1)]))

Submission status: Wrong Answer

Solution code:

n,q = map(int, input().split())
tree = {}
for i in range(n-1):
    a,b = map(int,input().split())
    if not(a in tree):
        tree[a] = [b]
    else:
        tree[a].append(b)

ans = [0]*n
for i in range(q):
    p,x = map(int,input().split())
    ans[p-1]+=x
    tmp = [p]
    while len(tmp)!=0:
        for tansaku in tmp[:]:
            if tansaku in tree:
                for node in tree[tansaku]:
                    ans[node-1]+=x
                    if node in tree:
                        tmp.append(node)
                tmp.remove(tansaku)
            else:
                tmp.remove(tansaku)

print(*ans)

Submission status: Wrong Answer

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

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

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

for i in range(1, N):
    ans[i] += ans[d[i]]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

import sys
input = sys.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)]

query = [0] * N
ans = [0] * N

for p,x in px:
    query[p-1] = query[p-1] + x
    
adj_list = [[] for _ in range(N)]
for a,b in ab:
    adj_list[a-1].append(b-1)
    adj_list[b-1].append(b-1)

memo = [True] * N

def dfs(v,val):
    child = adj_list[v]
    memo[v] = False
    val += query[v]
    ans[v] = val
    for s in child:
        if memo[s]:
            dfs(s,val)
    return 
        
dfs(0,0)
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
 
def main():
    N,Q = map(int,sys.stdin.readline().split())
    e = [ [ ] for i in range(N)]
    for i in range(N-1):
        a,b = map(int,sys.stdin.readline().split())
        e[a-1].append(b-1)
 
    from collections import defaultdict
    d = defaultdict(int)
    for i in range(Q):
        p,x = map(int,sys.stdin.readline().split())
        d[p-1] += x
 
    ansl = [0 for i in range(N)]
 
    def dfs(x,c):
        c += d[x]
        ansl[x] = c
        for v in e[x]:
            dfs(v,c)
 
    dfs(0,0)
    print(" ".join(map(str,ansl)))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
ab.sort()
c = [0]*n
for i in range(q):
  p, x = map(int, input().split())
  c[p-1] += x
for i in ab:
  c[i[1]-1] += c[i[0]-1]
print(' '.join(map(str, c)))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import lru_cache
 
def main():
    n, q = map(int, input().split())
    ab = [[],[]]
    
    for i in range(n-1):
        na,nb = map(int, input().split())
        ab[0].append(na)
        ab[1].append(nb)
 
    px = [[],[]]
    for i in range(q):
        np,nx = map(int, input().split())        
        px[0].append(np)
        px[1].append(nx)
    
    # print(n,q,ab,px)
    count = [0 for i in range(n)]
 
    oya_list = [0 for i in range(n)]
    for i in range(len(ab[1])):
        oya_list[ab[1][i]-1] = ab[0][i]
    
    for i in range(len(px[1])):
        count[px[0][i]-1] += px[1][i]
        
    for i in range(1,n):
 
        count[i] += count[oya_list[i]-1]
    
    print(' '.join(map(str,count)))
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# グラフの実装 -> 隣接リスト表現, 隣接行列表現
# 隣接リスト表現：各頂点に対して隣接頂点を連結リストで管理する

import collections

N, Q = map(int, input().split())
cnt = [0] * (N + 1)
G = [set() for _ in range(N + 1)]

# 隣接リストの作成
for _ in range(N - 1):
    pare, kid = map(int, input().split())
    G[pare].add(kid)

# 各頂点についてxの合計を計算
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

# BFSの実装
qu = collections.deque()
qu.append(1)#親の初期値
checked = [0] * (N+1)
while qu:
    v = qu.popleft()
    checked[v] = 1
    for u in G[v]:
        if checked[u] == 1:
            continue
        cnt[u] += cnt[v]
        qu.append(u)
print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
# グラフの実装 -> 隣接リスト表現, 隣接行列表現
# 隣接リスト表現：各頂点に対して隣接頂点を連結リストで管理する

import collections

N, Q = map(int, input().split())
cnt = [0] * (N + 1)
G = [set() for _ in range(N + 1)]

# 隣接リストの作成
for _ in range(N - 1):
    pare, kid = map(int, input().split())
    G[pare].add(kid)

# 各頂点についてxの合計を計算
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

# BFSの実装
qu = collections.deque()
qu.append(1)#親の初期値
checked = [0] * (N+1)
while qu:
    v = qu.pop()
    checked[v] = 1
    for u in G[v]:
        if checked[u] == 1:
            continue
        cnt[u] += cnt[v]
        qu.append(u)
print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
# グラフの実装 -> 隣接リスト表現, 隣接行列表現
# 隣接リスト表現：各頂点に対して隣接頂点を連結リストで管理する

import collections

N, Q = map(int, input().split())
cnt = [0] * (N + 1)
G = [set() for _ in range(N + 1)]

# 隣接リストの作成
for _ in range(N - 1):
    pare, kid = map(int, input().split())
    G[pare].add(kid)

# 各頂点についてxの合計を計算
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p] += x

# BFSの実装
qu = collections.deque()
qu.append(1)#親の初期値
checked = [0] * (N+1)
while qu:
    print(qu, G)
    v = qu.pop()
    checked[v] = 1
    for u in G[v]:
        if checked[u] == 1:
            continue
        cnt[u] += cnt[v]
        qu.append(u)
print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(2*10**5)
n,q=map(int,input().split())
graph=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  graph[a-1].append(b-1)
counter=[0]*n
for _ in range(q):
  p,x=map(int,input().split())
  counter[p-1]+=x
score=[0]*(n+1)
stack=[(0,-1)]
while stack!=[]:
  node,prev=stack.pop()
  score[node]=score[prev]+counter[node]
  for i in graph[node]:
    stack.append((i,node))
print(*score[:-1])

Submission status: Wrong Answer

Solution code:
class Leef(object):
	def __init__(self):
		self.children = []
		self.counter = 0

	def hasChild(self):
		return len(self.children) > 0

N, Q = map(int, input().split())
tree = {}
for n in range(1,N):
	a, b = map(int, input().split())
	if a not in tree:
		leef = Leef()
		leef.children.append(b)
		tree[a] = leef
	else:
		tree[a].children.append(b)

	if b not in tree:
		leef = Leef()
		tree[b] = leef

for q in range(Q):
	p, x = map(int, input().split())
	target_leef = [p]
	tmp_leef = [p]
	while len(tmp_leef) > 0:
		child_tmp = []
		for tl in tmp_leef:
			child_tmp.extend(tree[tl].children)
		target_leef.extend(child_tmp)
		tmp_leef = child_tmp
	
	for n in target_leef:
		tree[n].counter += x

print(' '.join(list(map(lambda t:str(t.counter), tree.values()))))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

N,Q = map(int,input().split())
ab_list = [tuple(map(int,input().split())) for _ in range(N-1)]
px_list = [tuple(map(int,input().split())) for _ in range(Q)]

px_dict = {}
for i in range(N+1):
    px_dict[i] = 0

for p, x in px_list:
    px_dict[p] += x

ab_dict = {}
for i in range(N+1):
    ab_dict[i] = []

for a,b in ab_list:
    ab_dict[a].append(b)

scores = [0 for _ in range(N+1)] # 1子おおい

def add_cnt(v, score):
    # pxdictから持ってきてスコアを足す
    score += px_dict[v]

    #今いる頂点に現在のスコアをめも
    scores[v] = score
    
    # 繋がっている頂点に今のスコアを投げる
    for b in ab_dict[v]:
        add_cnt(b,score)

add_cnt(1,0)

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

Submission status: Wrong Answer

Solution code:
n,q = input().split()
#print(str(n))
#print(str(q))

n = int(n)
q = int(q)
a = [0] * (n-1)
b = [0] * (n-1)
p = [0] * q
x = [0] * q
counter = [0] * n
children = []
parent = [-1] * n

for c in range(n):
  children.append(str(c+1))
#print(children)

for i in range(n-1):
  a[i],b[i] = input().split()
  parent[int(b[i]) - 1] = int(a[i])-1#親の配列インデックス
  children[int(a[i])-1]+=(" "+b[i])
  children[parent[int(a[i])-1]] += (" "+b[i])

for j in range(q):
  p[j],x[j] = input().split()
  list = children[int(p[j])-1].split()
  #print(list)
  for aa in list:
    counter[int(aa)-1] += int(x[j])
for bb in counter:
  print(bb, end="")
  print(" ", end="")

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

link = [None] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    if link[a - 1] is None:
        link[a - 1] = [b]
    else:
        link[a - 1].append(b)

states = [0] * N

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

for i, state in enumerate(states):
    if state == 0:
        continue
    next_nodes = link[i]
    if next_nodes is None:
        continue
    for node in next_nodes:
        states[node - 1] += state

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
# TLE
import sys
from collections import deque

input = sys.stdin.readline

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

link = [None] * N
for _ in range(N-1):
    a, b = map(int, input().split())
    if link[a-1] is None:
        link[a-1] = [b]
    else:
        link[a-1].append(b)

states = [0] * N
stack = deque()


def dfs(x):
    p = stack.popleft()

    # Add score
    states[p-1] += x
    # Search nexts
    nexts = link[p-1]
    if nexts is None:
        return
    for next_node in nexts:
        stack.append(next_node)


def update(p, x):
    stack.append(p)
    while stack:
        dfs(x)


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

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
# TLE
import sys
from collections import deque

input = sys.stdin.readline

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

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

states = [0] * N
stack = deque()


def dfs(x):
    p = stack.popleft()

    # Add score
    states[p-1] += x
    # Search nexts
    nexts = link[p-1]
    if nexts == []:
        return
    for next_node in nexts:
        stack.append(next_node)


def update(p, x):
    stack.append(p)
    while stack:
        dfs(x)


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

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
# TLE
from collections import deque


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

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

states = [0] * N
stack = deque()


def dfs(x):
    p = stack.popleft()

    # Add score
    states[p-1] += x
    # Search nexts
    nexts = link[p-1]
    if nexts == []:
        return
    for next_node in nexts:
        stack.append(next_node)


def update(p, x):
    stack.append(p)
    while stack:
        dfs(x)


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

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter


def main():    
    i, q = [int(x) for x in input().split(' ')]

    root = Node(1)
    for idx in range(i-1):
        a, b = [int(x) for x in input().split(' ')]
        root.add_child(a, b)

    for _ in range(q):
        p, x = [int(x) for x in input().split(' ')]
        root.set_count(p, x)

    root.add_count()

    counts = sorted(root.get_counts(), key=itemgetter(0))
    print(' '.join([str(x[1]) for x in counts]))


class Node:
    def __init__(self, id):
        self.id = id
        self.count = 0
        self.children = []

    def add_child(self, parent_id, child_id):
        if parent_id < self.id:
            return

        if parent_id == self.id:
            # 追加
            self.children.append(Node(child_id))
            return

        for child in self.children:
            child.add_child(parent_id, child_id)
        return

    def set_count(self, target_id, num, is_count=False):
        # if target_id == self.id:
        #     is_count = True

        # if is_count:
        #     self.count += num

        # for child in self.children:
        #     child.add_count(target_id, num, is_count)
        if target_id < self.id:
            return
        if target_id == self.id:
            self.count += num
            return
        for child in self.children:
            child.set_count(target_id, num, is_count)
        return

    def add_count(self, num=0):
        self.count += num
        for child in self.children:
            child.add_count(self.count)
        return

    def get_counts(self):
        res = [(self.id, self.count)]
        # print(self.id, self.count)
        for child in self.children:
            res += child.get_counts()
        return res
main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
def d():
    tree_map = defaultdict(list)
    value_map = defaultdict(int)
    N, Q = map(int,input().split())
    
    for _ in range(1, N):
        a, b = map(int,input().split())
        tree_map[a].append(b)

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

    res = [0] * N
    queue = deque()
    queue.append((1, 0))
    while queue:
        i, s = queue.popleft()
        if value_map[i] != 0:
            s += value_map[i]
        res[i-1] += s
        
        for j in tree_map[i]:
            queue.append((j, s))

    print(" ".join([str(r) for r in res]))


if __name__ == "__main__":
    d()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
ab.sort()
p = [0]*n
for i in range(q):
  a, b = map(int, input().split())
  p[a - 1] += b
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(" ".join(map(str, p)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def d():
    tree_map = defaultdict(list)
    value_map = defaultdict(int)
    N, Q = map(int,input().split())
    
    visited, head = set(), set()
    for _ in range(1, N):
        a, b = map(int,input().split())
        if a not in visited:
            head.add(a)

        visited.add(a); visited.add(b)
        tree_map[a].append(b)

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

    res = [0] * N
    stack = []
    for h in head:
        stack.append((h, 0))
    while stack:
        i, s = stack.pop()
        if value_map[i] != 0:
            s += value_map[i]
        res[i-1] += s
        
        for j in tree_map[i]:
            stack.append((j, s))

    print(" ".join([str(r) for r in res]))


if __name__ == "__main__":
    d()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def d():
    tree_map = defaultdict(list)
    value_map = defaultdict(int)
    N, Q = map(int,input().split())
 
    for _ in range(1, N):
        a, b = map(int,input().split())
        tree_map[a].append(b)

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

    res = [0] * N
    stack = []
    stack.append((1, 0))
    while stack:
        i, s = stack.pop()
        if value_map[i] != 0:
            s += value_map[i]
        res[i-1] += s
        
        for j in tree_map[i]:
            stack.append((j, s))

    print(" ".join([str(r) for r in res]))


if __name__ == "__main__":
    d()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(200000)


def main():
    def walk(p, c):
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    n, q = [int(x) for x in input().split()]

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

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

    walk(1, 0)

    print(*cnt[1:])


main()

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n-1)]
pq = [list(map(int,input().split())) for i in range(q)]
ab.sort()
c = [0]*n
for i in range(q):
  c[pq[i][0]-1] += pq[i][1]
for j in range(n-1):
  c[ab[j][1]-1] += c[ab[j][0]-1]
for i in c:
  print(i,end= " ")

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
c=[[] for i in range(N)]
for i in range(N-1):
    a,b=list(map(int,input().split()))
    c[a-1].append(b-1)
    c[b-1].append(a-1)
X=[0]*N
for i in range(Q):
    p,x=list(map(int,input().split()))
    X[p-1]+=x
A=[0]*N
A[0]+=X[0]
import sys
sys.setrecursionlimit(10**6)
def dfs(p):
    for n in c[p]:
        if A[n]==0:
            A[n]=X[n]+A[p]
            dfs(n)
dfs(0)
print(*A)

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)]
c=[[] for i in range(N)]
for a,b in ab:
    c[a-1].append(b-1)
X=[0]*N
for p,x in px:
    X[p-1]+=x
A=[0]*N
A[0]+=X[0]
import sys
sys.setrecursionlimit(10**6)
def dfs(p):
    for n in c[p]:
        if A[n]==0:
            A[n]=X[n]+A[p]
            dfs(n)
dfs(0)
print(*A)

Submission status: Wrong Answer

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


N, Q = map(int, input().split())
 
class Node():
    def __init__(self):
        self.connected = []
        self.count = 0
 
 
 
def add_count(v, p):
    for c in G[v].connected:
        if c == p:
            continue
        G[c].count += G[v].count
        add_count(c, v)

def add_count2():
    s = [(0, -1)]
    while s:
        v, p = s.pop()
        G[v].count += G[p].count
        for c in G[v].connected:
            if c == p:
                continue
            s.append((c, v))

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

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

# add_count(0, -1)
add_count2()
counts = [node.count for node in G]
print(*counts)

Submission status: Wrong Answer

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


N, Q = map(int, input().split())
 
class Node():
    def __init__(self):
        self.children = []
        self.count = 0
 
 
 
def add_count(G, v):
    for child in G[v].children:
        G[child].count += G[v].count
        add_count(G, child)
 
 
def main():
    G = [Node() for _ in range(N)]
    for _ in range(N-1):
        p, c = map(int, input().split())
        G[p-1].children.append(c-1)
 
    for _ in range(Q):
        p, x = map(int, input().split())
        G[p-1].count += x
 
    add_count(G, 0)
 
    counts = [node.count for node in G]
    print(*counts)
 
main()

Submission status: Wrong Answer

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


N, Q = map(int, input().split())
PC = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)] 

class Node():
    def __init__(self):
        self.children = []
        self.count = 0
 
def add_count(G, v):
    for child in G[v].children:
        G[child].count += G[v].count
        add_count(G, child)


G = [Node() for _ in range(N)]

for p, c in PC:
    G[p-1].children.append(c-1)

for p, x in PX:
    G[p-1].count += x

add_count(G, 0)

counts = [node.count for node in G]
print(*counts)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
tree = []
ops = []
for _ in range(N-1):
    tree.append(tuple(map(int, input().split())))
for _ in range(Q):
    ops.append(tuple(map(int, input().split())))

ans = [0] * N
tree.sort(key=lambda x:x[0])
ops.sort(key=lambda x:x[0])

for op in ops:
    ans[op[0]-1] += op[1]
    
for edge in tree:
    ans[edge[1]-1] += ans[edge[0]-1]
    
print(*ans)

Submission status: Wrong Answer

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


N, Q = map(int, input().split())
 
class Node():
    def __init__(self):
        self.children = []
        self.count = 0
 
 
 
def add_count(G, v):
    for child in G[v].children:
        G[child].count += G[v].count
        add_count(G, child)
 
 
def main():
    G = [Node() for _ in range(N)]
    for _ in range(N-1):
        p, c = map(int, input().split())
        G[p-1].children.append(c-1)
 
    for _ in range(Q):
        p, x = map(int, input().split())
        G[p-1].count += x
 
    add_count(G, 0)
 
    counts = [node.count for node in G]
    print(*counts)
 
main()

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
tree = []
ops = []
for _ in range(N-1):
    tree.append(tuple(map(int, input().split())))
for _ in range(Q):
    ops.append(tuple(map(int, input().split())))

ans = [0] * N
# ops.sort(key=lambda x:x[0])

for op in ops:
    ans[op[0]-1] += op[1]
    
for edge in tree:
    ans[edge[1]-1] += ans[edge[0]-1]
    
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def d():
    tree_map = defaultdict(list)
    value_map = defaultdict(int)
    N, Q = map(int,input().split())
 
    for _ in range(1, N):
        a, b = map(int,input().split())
        tree_map[a].append(b)

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

    res = [0] * N
    stack = []
    stack.append((1, 0))
    while stack:
        i, s = stack.pop()
        if value_map[i] != 0:
            s += value_map[i]
        res[i-1] = s
        
        for j in tree_map[i]:
            stack.append((j, s))

    print(" ".join([str(r) for r in res]))


if __name__ == "__main__":
    d()

Submission status: Wrong Answer

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


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

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

a_s = sorted(a)
ans= [ 0 for _ in range(N)]

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

for i in range(N-1):
    ans[a_s[i][1]-1] += ans[a_s[i][0]-1]

print(*ans)

Submission status: Wrong Answer

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

def main():
    N,Q = map(int,input().split())

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

    a_s = sorted(a)
    ans= [ 0 for _ in range(N)]

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

    for i in range(N-1):
       ans[a_s[i][1]-1] += ans[a_s[i][0]-1]

    print(*ans)

if __name__ == "__main__":
    main()