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(1000000)
N, Q = map(int, input().split())
rl = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    rl[a].append(b)
    rl[b].append(a)
cnt = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    cnt[p] += x

def dfs(v, pv):
    for next_v in rl[v]:
        if next_v == pv:
            continue
        cnt[next_v] += cnt[v]
        dfs(next_v, v)
dfs(0, -1)
print(cnt)

Submission status: Wrong Answer

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

N, Q = [int(i) for i in input().split()]
path = [[] for _ in range(N - 1)]
query = [[] for _ in range(Q)]
for i in range(N - 1):
    path[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    query[i] = [int(i) for i in input().split()]
    query[i][0] -= 1
#%%
add = [0] * N
ans = [0] * N
for i, j in query:
    add[i] += j
    
children = [[]  for _ in range(N)]
for i, j in path:
    children[i].append(j)
    
def DFS(id, value):
    value += add[id]
    ans[id] = value
    for i in children[id]:
        DFS(i, value)
    
DFS(0, 0)

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

Submission status: Wrong Answer

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

N, Q = [int(i) for i in input().split()]
path = [[] for _ in range(N - 1)]
query = [[] for _ in range(Q)]
for i in range(N - 1):
    path[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    query[i] = [int(i) for i in input().split()]
    query[i][0] -= 1
#%%
add = [0] * N
ans = [0] * N
for i, j in query:
    add[i] = j
    
children = [[]  for _ in range(N)]
for i, j in path:
    children[i].append(j)
    
def DFS(id, value):
    value += add[id]
    ans[id] = value
    for i in children[id]:
        DFS(i, value)
    
DFS(0, 0)

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

Submission status: Wrong Answer

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

N, Q = [int(i) for i in input().split()]
path = [[] for _ in range(N - 1)]
query = [[] for _ in range(Q)]
for i in range(N - 1):
    path[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    query[i] = [int(i) for i in input().split()]
    query[i][0] -= 1
#%%
add = [0] * N
ans = [0] * N
for i, j in query:
    add[i] = j
    
children = [[]  for _ in range(N)]
for i, j in path:
    children[i].append(j)
    
def DFS(id, value):
    value += add[id]
    ans[id] = value
    for i in children[id]:
        DFS(i, value)
    
DFS(0, 0)

print(ans)

Submission status: Wrong Answer

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

N, Q = [int(i) for i in input().split()]
path = [[] for _ in range(N - 1)]
query = [[] for _ in range(Q)]
for i in range(N - 1):
    path[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    query[i] = [int(i) for i in input().split()]
    query[i][0] -= 1
#%%

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


def DFS_stack(id, value, list_tree):
    stack = []
    stack.append(list_tree[id])
    list_tree[id][2] += value
    while len(stack) != 0:
        u = stack.pop()
        for i in u[1]:
            list_tree[i][2] += value
            stack.append(list_tree[i])
            
list_tree = [[-1, [], 0] for _ in range(N)]
for i, j in path:
    list_tree[i][1].append(j)
    list_tree[j][0] = i
    
for i, j in query:
    DFS_stack(i, j, list_tree)
    
for i in list_tree:
    print(i[2], end=" ")

Submission status: Wrong Answer

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

N, Q = [int(i) for i in input().split()]
AB = [[] for _ in range(N - 1)]
PX = [[] for _ in range(Q)]
for i in range(N - 1):
    AB[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    PX[i] = [int(i) for i in input().split()]
    PX[i][0] -= 1
#%%

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

def DFS(node_id, value, list_tree):
    list_tree[node_id].value += value
    for i in list_tree[node_id].children:
        DFS(i, value, list_tree)

#%%     
list_node = [tree(i) for i in range(N)]
for i, j in AB:
    list_node[i].children.append(j)
    list_node[j].parent = i

for i, j in PX:
    DFS(i, j, list_node)
    
for i in list_node:
    print(i.value, end=" ")

Submission status: Wrong Answer

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

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

score=[0]*n
score[0]=px[0]
from collections import deque
que=deque([0])
while que:
    s=que.popleft()
    for g in es[s]:
        if rank[g]==-1:
            rank[g]=rank[s]+1
            score[g]=score[s]+px[g]
            que.append(g)
print(*score)

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 組合せnCr
def combinations_count(n, r):
    if n < r:
        return 0
    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

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

# ここから書き始める
n, q = map(int, input().split())
a = [0 for i in range(n - 1)]
b = [0 for i in range(n - 1)]
p = [0 for i in range(q)]
x = [0 for i in range(q)]
for i in range(n - 1):
    a[i], b[i] = map(int, input().split())
for i in range(q):
    p[i], x[i] = map(int, input().split())

tree = [[] for i in range(n)]
for i in range(n - 1):
    tree[a[i] - 1].append(b[i] - 1)
# print("tree =", tree)

cnt = [0 for i in range(n)]
for i in range(q):
    cnt[p[i] - 1] += x[i]
# print("cnt =", cnt)

ans = [0 for i in range(n)]
queue = deque([[0, 0]])
while queue:
    node, total = queue.popleft()
    total += cnt[node]
    ans[node] = total
    for i in tree[node]:
        queue.append([i, total])
print(*ans)

Submission status: Wrong Answer

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

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

for a, b in AB:
  T[a-1].append(b)

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

def dfs(node, previous):
  C[node-1] += previous
  for next in T[node-1]:
    dfs(next, C[node-1])
dfs(1, 0)
print(" ".join(map(str, C)))

Submission status: Wrong Answer

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

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

for a, b in AB:
  T[a-1].append(b)

PX = [list(map(int, input().split())) for _ in range(Q)]
C = [0 for _ in range(N)]
#print(T)
def dfs(p, x):
  C[p-1] += x
  for next in T[p-1]:
    dfs(next, x)
for p, x in PX:
  dfs(p, x)
print(" ".join(map(str, C)))

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())
    ki[a-1].append(b-1)
    ki[b-1].append(a-1)
score = [0 for i in range(n)]

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

#print(ki,score)
ans = [0 for i in range(n)]

ans[0] = score[0]
visited = [False]*n
visited[0] = True
Q = deque([0])

while Q:
    d = Q.pop()
    #print(d)
    for to in ki[d]:
        if visited[to]:
            continue
        
        ans[to] = ans[d] + score[to]
        #print(ans,ans[to],score[to],d)
        visited[to] = True
        Q.append(to)
print(*ans)

Submission status: Wrong Answer

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

for j in range(Q):#全てのpjに対して根のカウンタだけにxをプラス
    
    counter[px[j][0]-1] += px[j][1]

for i in range(N-1):#上から下へ流す
    counter[ab[i][1]-1] += counter[ab[i][0]-1]



print(*counter)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
ab = sorted([list(map(int,input().split())) for i in range(N-1)])
px = [list(map(int,input().split())) for j in range(Q)]
counter = [0]*N
print(ab)
print(px)
for j in range(Q):#全てのpjに対して根のカウンタだけにxをプラス
    
    counter[px[j][0]-1] += px[j][1]

for i in range(N-1):#上から下へ流す
    counter[ab[i][1]-1] += counter[ab[i][0]-1]



print(*counter)

Submission status: Wrong Answer

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

for j in px:#全てのpjに対して根のカウンタだけにxをプラス
    counter[j[0]-1] += j[1]

for i in range(N-1):#上から下へ流す
    counter[ab[i][1]-1] += counter[ab[i][0]-1]



print(*counter)

Submission status: Wrong Answer

Solution code:
# 26

import sys

sys.setrecursionlimit(10**9)

n, q = map(int, input().split())
ans_l = [0] * n
graph = [[] for x in range(n)]
lank = [float("Inf")] * n

def bfs(p, x):
    ans_l[p] += x
    _lank = lank[p]
    for i in range(len(graph[p])):
        _p = graph[p][i]
        if _lank < lank[_p]:
            bfs(_p, x)

for i in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
    
lank[0] = 0
for i in range(n):
    _lank = lank[i]
    for j in range(len(graph[i])):
        if lank[graph[i][j]] > n:
            lank[graph[i][j]] = _lank + 1

for j in range(q):
    p, x = map(int, input().split())
    bfs(p-1, x)
    
print(*ans_l)

Submission status: Wrong Answer

Solution code:
# 26

import sys

sys.setrecursionlimit(10**9)

n, q = map(int, input().split())
ans_l = [0] * n
graph = [[] for x in range(n)]
lank = [float("Inf")] * n

for i in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
    
lank[0] = 0
for i in range(n):
    _lank = lank[i]
    for j in range(len(graph[i])):
        if lank[graph[i][j]] > n:
            lank[graph[i][j]] = _lank + 1

def bfs(p, x):
    ans_l[p] += x
    _lank = lank[p]
    for i in range(len(graph[p])):
        _p = graph[p][i]
        if _lank < lank[_p]:
            bfs(_p, x)

for j in range(q):
    p, x = map(int, input().split())
    bfs(p-1, x)
    
print(*ans_l)

Submission status: Wrong Answer

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

graph = [[] for _ in range(N)]

flag = [True]+[False]*(N-1)
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
#print(graph)

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

for k, i in enumerate(graph):
    for j in i:
        if flag[j]:
            continue
        w[j] += w[k]
        flag[j]=True

print(" ".join(map(str, w)))

Submission status: Wrong Answer

Solution code:
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)
#print(graph)

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

for k, i in enumerate(graph):
    for j in i:
        w[j] += w[k]

#print(w)
print(" ".join(map(str, w)))

Submission status: Wrong Answer

Solution code:
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)
#print(graph)

w = [[] for _ in range(N)]
for _ in range(Q):
    p, x = map(int, input().split())
    w[p-1].append(x)
#print(w)


for k, i in enumerate(graph):
    for j in i:
        w[j] += w[k]
#print(w)

for i in range(N):
    w[i] = str(sum(w[i]))

#print(w)
print(" ".join(w))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float('inf')


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

graph = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    # undirected, 0-indexed
    graph[u - 1].append(v - 1)

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

# 方針
# 1. 各クエリに対し、頂点p_jのカウンターにx_jを足す
# (この段階では部分木に含まれるノードには操作を行わない)
# 2. クエリを全て処理したあと、深さ優先順に各ノードvのカウンタを以下のように更新する
# count[v] = count[v] + 親ノードのカウンタ
# 毎回部分木に対して処理を行うのだとO(N^2)の計算量となり遅いので、上記のように工夫して更新する


def bfs(v, parent):
    for v_adj in graph[v]:
        if v_adj == parent:
            continue
        count[v_adj] += count[v]
        bfs(v_adj, v)


count = [0] * N
for p, x in query:
    count[p] += x
# pprint(count)

v = 0
bfs(0, -1)

print(*count)

Submission status: Wrong Answer

Solution code:
#「探索候補スタック」「探索済みリスト」「現在地点」の３点セットでとらえる
n,q=map(int,input().split())
mat=[set() for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    mat[a-1].add(b-1)
count=[0]*n
for _ in range(q):
    p,score=map(int,input().split())
    count[p-1]+=score

stack=[[0,0,-1]]
while stack:
    # print(stack)
    num,cnt,pr=stack.pop()
    count[num]+=cnt
    for k in mat[num]:
        if k==pr:
            continue
        stack.append([k,count[num],num])
print(*count)

Submission status: Wrong Answer

Solution code:
#「探索候補スタック」「探索済みリスト」「現在地点」の３点セットでとらえる
n,q=map(int,input().split())
mat=[[0]*n for _ in range(n)]
ans=[0]*n
for _ in range(n-1):
    l=list(map(int,input().split()))
    mat[l[0]-1][l[1]-1]=1

for _ in range(q):
    a=list(map(int,input().split()))
    start=a[0]-1
    score=a[1]
    # ans[start]+=score
    stack=[start]
    seen=[0]*n
    while stack:
        current=stack[-1]
        for i in range(len(mat[current])):
            if mat[current][i]==1 and seen[i]==0:
                stack.append(i)
                seen[i]=1
                break
        else:
            ans[current]+=score
            current=stack.pop()
print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
N,Q = list(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)]
X = {}
for px in PX:
    if px[0] not in X.keys():
        X[px[0]] = px[1]
    else:
        X[px[0]] += px[1]
G = {}
for ab in AB:
    if ab[0] not in G.keys():
        G[ab[0]] = [ab[1]]
    else:
        G[ab[0]].append(ab[1])
W = [0]*N

def dfs(G, W, v, w):
    if v not in X.keys():
        W[v-1] = w
    else:
        W[v-1] = w+X[v]
    if v not in G.keys():
        return
    for nv in G[v]:
        dfs(G, W, nv, W[v-1])

dfs(G, W, 1, 0)

print(*W)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
tree = [set() for i in range(n)]
count = [0]*n
check = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    if a == 1:
        tree[a-1].add(b-1)
        check[b-1] = 1
    elif check[b-1] == 1:
        tree[b-1].add(a-1)
        check[a-1] = 1
    else:
        tree[a-1].add(b-1)
        check[b-1] = 1
for j in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
    
q = queue.Queue()
q.put(0)
while not q.empty():
    num = q.get()
    for k in tree[num]:
        count[k] += count[num]
        q.put(k)
        
print(*count)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
tree = [set() for i in range(n)]
count = [0]*n
check = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    if a == 0:
        tree[a-1].add(b-1)
        check[b-1] = 1
    elif check[b-1] == 1:
        tree[b-1].add(a-1)
        check[a-1] = 1
    else:
        tree[a-1].add(b-1)
        check[b-1] = 1
for j in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
    
q = queue.Queue()
q.put(0)
while not q.empty():
    num = q.get()
    for k in tree[num]:
        count[k] += count[num]
        q.put(k)
        
print(*count)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
tree = [set() for i in range(n)]
count = [0]*n
check = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    if i == 0:
        tree[a-1].add(b-1)
        check[b-1] = 1
    elif check[b-1] == 1:
        tree[b-1].add(a-1)
        check[a-1] = 1
    else:
        tree[a-1].add(b-1)
        check[b-1] = 1
for j in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
    
q = queue.Queue()
q.put(0)
while not q.empty():
    num = q.get()
    for k in tree[num]:
        count[k] += count[num]
        q.put(k)
        
print(*count)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
tree = [set() for i in range(n)]
count = [0]*n
check = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    if i == 0:
        tree[a-1].add(b-1)
        check[b-1] = 1
    elif check[b-1] == 1:
        tree[b-1].add(a-1)
    else:
        tree[a-1].add(b-1)
for j in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
    
q = queue.Queue()
q.put(0)
while not q.empty():
    num = q.get()
    for k in tree[num]:
        count[k] += count[num]
        q.put(k)
        
print(*count)

Submission status: Wrong Answer

Solution code:
import queue
n,q = map(int,input().split())
tree = [set() for i in range(n)]
count = [0]*n
ans = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].add(b-1)
for j in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
    
q = queue.Queue()
q.put(0)
while not q.empty():
    num = q.get()
    for k in tree[num]:
        count[k] += count[num]
        q.put(k)
        
print(*count)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

q = deque([1])
visited=set()
while q:
    v = q.pop()
    visited.add(v)
    for next_v in g[v]:
        if next_v not in visited:
            cnt[next_v]+=cnt[v]
            q.append(next_v)

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

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

q = deque([1])
visited=set()
while q:
    v = q.popleft()
    visited.add(v)
    for next_v in g[v]:
        if next_v not in visited:
            cnt[next_v]+=cnt[v]
            q.append(next_v)

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())

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

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

for i in range(1,n+1):
    for next_node in g[i]:
        cnt[next_node]+=cnt[i]

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())

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

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

for i in range(1,n):
    for next_node in g[i]:
        cnt[next_node]+=cnt[i]

print(*cnt[1:])

Submission status: Wrong Answer

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

part_tree=[[] for i in range(n+1)]
for i in range(n):
    part_tree[n-i].append(n-i)
    for next_node in g[n-i]:
        part_tree[n-i] += part_tree[next_node]

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

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

visited = [False] * N
def dfs(v, acc):
    visited[v] = True
    count[v] += acc
    for u in adj[v]:
        if visited[u]:
            continue
        dfs(u, count[v])

dfs(0, 0)
print(" ".join(map(str, count)))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

visited = [False] * N
def dfs(v, acc):
    if visited[v]:
        return
    visited[v] = True
    count[v] += acc
    for u in adj[v]:
        dfs(u, count[v])

dfs(0, 0)
print(" ".join(map(str, count)))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
# sys.setrecursionlimit(10**7)
 
def main():
    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)]
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
    # dequeを使ったキューによるBFS
    # 子をindexで、親ノードを要素で
    parent = [None] * (n+1)
    q = deque([1])
    while q:
        par = q.popleft()
        for chl in adj[par]:
            print(q)
            if chl == parent[par]:
                continue
            v[chl] += v[par]
            parent[chl] = par
            q.append(chl)
    # print(parent)
    # print(v)
    ans = ' '.join(map(str,v[1:]))
    print(ans)   
 
 
    # dequeを使ったスタックによるDFS
    # 子をindexで、親ノードを要素で
    # parent = [None] * (n+1)
    # q = deque([1])
    # while q:
    #     par = q.pop()
    #     for chl in adj[par]:
    #         if chl == parent[par]:
    #             continue
    #         v[chl] += v[par]
    #         parent[chl] = par
    #         q.append(chl)
    # # print(parent)
    # # print(v)
    # ans = ' '.join(map(str,v[1:]))
    # print(ans)
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    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)]
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
    # dequeを使ったキューによるBFS
    # 子をindexで、親ノードを要素で
    parent = [None] * (n+1)
    q = deque([1])
    while q:
        par = q.popleft()
        for chl in adj[par]:
            print(q)
            if chl == parent[par]:
                continue
            v[chl] += v[par]
            parent[chl] = par
            q.append(chl)
    # print(parent)
    # print(v)
    ans = ' '.join(map(str,v[1:]))
    print(ans)   


    # dequeを使ったスタックによるDFS
    # 子をindexで、親ノードを要素で
    # parent = [None] * (n+1)
    # q = deque([1])
    # while q:
    #     par = q.pop()
    #     for chl in adj[par]:
    #         if chl == parent[par]:
    #             continue
    #         v[chl] += v[par]
    #         parent[chl] = par
    #         q.append(chl)
    # # print(parent)
    # # print(v)
    # ans = ' '.join(map(str,v[1:]))
    # print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
a = [[i] for i in range(n+1)]
r = [0 for i in range(n+1)]
count = [0] * (n+1)

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

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

for i in range(2, n+1):
    count[i] += count[r[i]]

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

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
for i in range(1,n):
    for j in node[i]:
        s[j] += s[i]
for i in range(1,n+1):
    print(s[i],end="")
    print(" ",end="")
print("")

Submission status: Wrong Answer

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

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

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

        
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)

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