Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10 ** 6)

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

graph   = [ [] for _ in range(N) ]
queries = { p: 0 for p in range(N) }

for _ in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)

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

    queries[p - 1] += q

def dfs(p, acc):
    acc += queries[p]

    print(acc)

    for child in graph[p]:
        dfs(child, acc)

dfs(0, 0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000000)
n,q = map(int, input().split())
graph = [[] for _ in range(n+1)]
ans=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph[a].append(b)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  for j in graph[i]:
    ans[j] += ans[i]
    dfs(j)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
from collections import deque
edge=[[] for _ in range(n)]
ans=[0]*n
for i in range(n-1):
  a,b=map(lambda x:int(x)-1,input().split())
  edge[a].append(b)
#  edge[b].append(a)

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

q=deque([])
q.append(0)
while q:
  v=q.popleft()
  for c in edge[v]:
    ans[c]+=ans[v]
    q.append(c)
    
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import *
f=lambda:map(int,input().split())
n,q=f()
a=[[]for _ in range(n+1)]
for _ in range(n-1):
    i,j=f()
    a[i]+=[j]
b=[0]*(n+1)
for _ in range(q):
    i,j=f()
    b[i]+=j
d=deque([1])
while d:
    i=d.pop()
    for j in a[i]:
        b[j]+=b[i]
        d+=[j]
print(*b[1:])

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,q=f()
a=[[]for _ in range(n+1)]
for _ in range(n-1):
    i,j=f()
    a[i]+=[j]
b=[0]*(n+1)
for _ in range(q):
    i,j=f()
    b[i]+=j
d=[1]
while d:
    i=d.pop(0)
    for j in a[i]:
        b[j]+=b[i]
        d+=[j]
print(*b[1:])

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,q=f()
a=[[]for _ in range(n+1)]
for _ in range(n-1):
    i,j=f()
    a[i]+=[j]
b=[0]*(n+1)
for _ in range(q):
    i,j=sorted(f())
    b[i]+=j
for i in range(n):
    for j in a[i]:
        b[j]+=b[i]
print(*b[1:])

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,q=f()
a=[[]for _ in range(n+1)]
for _ in range(n-1):
    i,j=f()
    a[i]+=[j]
b=[0]*(n+1)
for _ in range(q):
    i,j=f()
    b[i]+=j
for i in range(n):
    for j in a[i]:
        b[j]+=b[i]
print(*b[1:])

Submission status: Wrong Answer

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

def MI(): return map(int, input().split())

N,Q=MI()
Edge=[[] for _ in range(N)]
Point=[0]*(N)
for _ in range(N-1):
  a,b=MI()
  Edge[a-1].append(b-1)
for _ in range(Q):
  p,x=MI()
  Point[p-1]+=x
  #print(p,Point[p-1])
  
def dfs(now,pre=-1):
  for nxt in Edge[now]:
    if nxt==pre:
      continue
    Point[nxt]+=Point[now]
    #print(*Point)
    dfs(nxt,now)
dfs(0)
print(*Point)

Submission status: Wrong Answer

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

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 dfs(m):
    if m in vdic:
        for i in range(len(vdic[m])):
            point[vdic[m][i]] += point[m]
            dfs(vdic[m][i])

point = [0]*(n+1)

vdic = {}
for i in range(n-1):
    if ab[i][0] not in vdic:
        vdic[ab[i][0]] = []
    vdic[ab[i][0]].append(ab[i][1])

for i in range(q):
    point[px[i][0]] += px[i][1]
'''
for v in vdic:
    for c in vdic[v]:
        point[c] += point[v]
'''
dfs(1)

for i in range(1,len(point)):
    print(point[i],end = ' ')

Submission status: Wrong Answer

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

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

point = [0]*(n+1)

vdic = {}
for i in range(n-1):
    if ab[i][0] not in vdic:
        vdic[ab[i][0]] = []
    vdic[ab[i][0]].append(ab[i][1])

for i in range(q):
    point[px[i][0]] += px[i][1]

for v in vdic:
    for c in vdic[v]:
        point[c] += point[v]

for i in range(1,len(point)):
    print(point[i],end = ' ')

Submission status: Wrong Answer

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

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

r_dic = {}
for i in range(len(ab)):
    if ab[i][0] not in r_dic:
        r_dic[ab[i][0]] = []
    r_dic[ab[i][0]].append(ab[i][1])

for key in r_dic:
    i = 0
    while i < len(r_dic[key]):
        if r_dic[key][i] in r_dic:
            r_dic[key] += r_dic[r_dic[key][i]]
        i += 1


point = [0]*(n+1)
for i in range(q):
    x = px[i][1]
    p = px[i][0]

    point[p] += x
    if p in r_dic:
        for j in range(len(r_dic[p])):
            point[r_dic[p][j]] += x

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0]*n
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = list(map(lambda x: x-1, map(int, input().split())))
    G[a].append(b)

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

for i,g in enumerate(G):
    for _g in g:
        ans[_g] += ans[i]

print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0]*n
AB = [[] for _ in range(n-1)]
for i in range(n-1):
    AB[i] = list(map(lambda x: x-1, map(int, input().split())))
AB.sort(key=lambda x: x[0])

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

for (a,b) in AB:
    ans[b] += ans[a]

print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0]*n
AB = [[] for _ in range(n-1)]
for i in range(n-1):
    AB[i] = list(map(lambda x: x-1, map(int, input().split())))
AB.sort(key=lambda x: x[1])

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

for (a,b) in AB:
    ans[b] += ans[a]

print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
T = [set([i]) for i in range(n)]
AB = [[] for _ in range(n-1)]
ans = [0]*n
for i in range(n-1):
    AB[i] = list(map(lambda x: x-1, map(int, input().split())))
AB.sort(key=lambda x: x[1], reverse=True)

for (a,b) in AB:
    T[a] = T[a].union(T[b])

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

print(*ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
cnt=[0]*(n+1) #答えのリスト
lst=[[] for _ in range(n+1)] #連結リスト

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

for i in range(q):
    a,val=map(int,input().split())
    cnt[a]+=val

stack=[1]
while stack:
    item=stack.pop()
    for i in lst[item]:
        stack.append(i)
        cnt[i]+=cnt[item]

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

Submission status: Wrong Answer

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

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

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

def dfs(root,point):
    visited=[None]*N
    visited[root]=1
    stack=[root]
    while stack:
        p=stack.pop()
        for i in renketsu[p]:
            stack.append(i)
            visited[i]=1
    for i in range(N):
        if visited[i]==1:
            ans[i]+=point

ans=[0]*N
for i in range(N):
    if score[i]>0:
        dfs(i,score[i])

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc138/tasks/abc138_d

import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline


class Graph:
    def __init__(self, n, dictated=False, edge=[]):
        self.n = n
        self.dictated = dictated
        self.conj = [set() for _ in range(self.n)]
        self.visit = set()
        self.second_visit = set()
        for x, y in edge:
            self.add_edge(x,y)

    def add_edge(self,x,y):
        self.conj[x].add(y)
        if self.dictated == False:
            self.conj[y].add(x)

    def dfs(self, info, start=0, goal=None, use_visit=False):
        """
        :param start: スタート地点
        :param goal: ゴール地点
        :param use_visit: 前回の探索結果を保持するかどうか
        :return: ゴール地点までの距離。存在しなければ -1
        """
        if use_visit == False:
            self.visit = set()
            self.second_visit = set()
        self.visit.add(start)

        past_p = None
        next_set = [(start, past_p, 0)]
        next_full_set = [next_set]
        while sum(map(lambda x: x!=[], next_full_set)) != 0:
            next_set = next_full_set.pop()
            p, past_p, t = next_set.pop()
            print(p)
            for q in self.conj[p]:
                if p == past_p:
                    # 逆流した際の処理
                    continue
                if q in self.visit:
                    continue
                if q == goal:
                    # ゴール時の処理
                    # return t+1
                    continue
                # p から q への引継ぎ処理
                info[q] += info[p]
                self.visit.add(q)
                next_set.append((q, p, t + 1))
            if not next_set:
                self.second_visit.add(p)
                # 帰り際の処置（壊した道を元に戻す等)
                # visit.discard(p)
            else:
                next_full_set.append(next_set)
        return info


######################################################################################################

N, Q = map(int, input().split())  # N:頂点数, Q: クエリの数
M = N-1

graph = Graph(N, dictated=False)

for _ in range(M):
    x, y = map(int, input().split())
    x -= 1                                    # デクリメントが不要な場合は消す
    y -= 1
    graph.add_edge(x,y)

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

print(*graph.dfs(info, start=0))
print(graph.second_visit)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
import sys
sys.setrecursionlimit(10**9)


N, Q = map(int, input().split())
count = [0] * N
parent = defaultdict(int)
f = [False] * N
t = [[] for i in range(N)]
for i in range(N - 1):
    a, b = map(int, input().split())
    t[a - 1].append(b)
    t[b - 1].append(a)

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


q = deque([1])
while(q):
    n = q.popleft()
    if f[n-1]:
        continue
    f[n-1] = True
    for i in t[n-1]:
        if f[i-1]:
            continue
        parent[i] = n
        q.append(i)
for i in range(1, N):
    count[i] += count[parent[i + 1] - 1]
#print(parent)
print(*count)

Submission status: Wrong Answer

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

def f(n):
    if flag[n-1]:
        return
    else:
        flag[n-1] = True
        for tij in t[n-1]:
            f(tij)
            parent[tij] = n


N,Q = map(int,input().split())
count = [0]*N
parent = defaultdict(int)
flag = [False]*N
t = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    t[a-1].append(b)
    t[b-1].append(a)
f(1)
for i in range(Q):
    p,x = map(int,input().split())
    count[p-1] += x
for i in range(1,N):
    count[i] += count[parent[i+1]-1]

print(*count)

Submission status: Wrong Answer

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

def f(n):
    if flag[n-1]:
        return
    else:
        flag[n-1] = True
        for tij in t[n-1]:
            f(tij)
            parent[tij] = n


N,Q = map(int,input().split())
count = [0]*N
parent = defaultdict(int)
flag = [False]*N
t = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    t[a-1].append(b)
    t[b-1].append(a)
print(t)
f(1)
for i in range(Q):
    p,x = map(int,input().split())
    count[p-1] += x
for i in range(1,N):
    count[i] += count[parent[i+1]-1]

print(*count)

Submission status: Wrong Answer

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

def f(n):
    for tij in t[n-1]:
        f(tij)
        parent[tij] = n


N,Q = map(int,input().split())
count = [0]*N
parent = defaultdict(int)
t = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    t[a-1].append(b)
f(1)
for i in range(Q):
    p,x = map(int,input().split())
    count[p-1] += x
for i in range(1,N):
    count[i] += count[parent[i+1]-1]

print(*count)

Submission status: Wrong Answer

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

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N, Q = list(map(int, input().split()))
    a, b = [], []
    for n in range(N - 1):
        a_n, b_n = list(map(int, input().split()))
        a.append(a_n)
        b.append(b_n)
    p, x = [], []
    for q in range(Q):
        p_q, x_q = list(map(int, input().split()))
        p.append(p_q)
        x.append(x_q)

    return N, Q, a, b, p, x


def main(N: int, a: list, b: list, p: list, x: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 頂点数
        a (list): 頂点
        b (list): 頂点
        p (list): 頂点
        x (list): 値
    """
    # グラフの作成
    g = [list() for n in range(N)]
    for a_n, b_n in zip(a, b):
        g[a_n - 1].append(b_n - 1)

    # 求解処理
    c = [0 for n in range(N)]
    for p_q, x_q in zip(p, x):
        seen, todo = [False for n in range(N)], []
        seen[p_q - 1] = True
        todo.append(p_q - 1)
        while len(todo) > 0:
            v = todo.pop()
            c[v] += x_q
            for w in g[v]:
                if not seen[w]:
                    seen[w] = True
                    todo.append(w)
    ans = " ".join(map(str, c))

    # 結果出力
    print(ans)


if __name__ == "__main__":
    # 標準入力を取得
    N, Q, a, b, p, x = get_input()

    # メイン処理
    main(N, a, b, p, x)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
N, Q = list(map(int, input().split()))
a, b = [], []
for n in range(N - 1):
    a_n, b_n = list(map(int, input().split()))
    a.append(a_n)
    b.append(b_n)
p, x = [], []
for q in range(Q):
    p_q, x_q = list(map(int, input().split()))
    p.append(p_q)
    x.append(x_q)

# 求解処理
g = [list() for n in range(N)]
for a_n, b_n in zip(a, b):
    g[a_n - 1].append(b_n - 1)

c = [0 for n in range(N)]
for p_q, x_q in zip(p, x):
    seen, todo = [False for n in range(N)], []
    seen[p_q - 1] = True
    todo.append(p_q - 1)
    while len(todo) > 0:
        v = todo.pop()
        c[v] += x_q
        for w in g[v]:
            if not seen[w]:
                seen[w] = True
                todo.append(w)

ans = " ".join(map(str, c))

# 結果出力
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
N, Q = list(map(int, input().split()))
a, b = [], []
for n in range(N - 1):
    a_n, b_n = list(map(int, input().split()))
    a.append(a_n)
    b.append(b_n)
p, x = [], []
for q in range(Q):
    p_q, x_q = list(map(int, input().split()))
    p.append(p_q)
    x.append(x_q)

# 求解処理
g = [list() for n in range(N)]
for a_n, b_n in zip(a, b):
    g[a_n - 1].append(b_n - 1)

c = [0 for n in range(N)]
for p_q, x_q in zip(p, x):
    seen, todo = [False for n in range(N)], []
    seen[p_q - 1] = True
    todo.append(p_q - 1)
    while len(todo) > 0:
        v = todo.pop()
        c[v] += x_q
        for w in g[v]:
            if not seen[w]:
                todo.append(w)

ans = " ".join(map(str, c))

# 結果出力
print(ans)

Submission status: Wrong Answer

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


def dfs(u, add):
    if visited[u]:
        return
    else:
        cnt[u] += add
        visited[u] = True
    for nu in graph[u]:
        if visited[nu] or nu < u:
            continue
        else:
            dfs(nu, add)


n, q = map(int, input().split())
graph = defaultdict(list)

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

cnt = [0] * (n + 1)

for _ in range(q):
    visited = [False] * (n + 1)
    p, x = map(int, input().split())
    dfs(p, x)

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
adj=[[]for i in range(n)]
ans=[0 for i in range(n)]
for i in range(n-1):
    a,b=map(lambda x:int(x)-1,input().split())
    adj[a].append(b)
for i in range(n):
    que=deque()
    que.append(i)
    v=set()
    v.add(i)
    while que:
        k=que.popleft()
        if k not in adj[i]:
            adj[i].append(k)
        for j in adj[k]:
            if j in v:
                continue
            
            v.add(j)
            que.append(j)
for i in range(q):
    p,x=map(int,input().split())
    p-=1
    for j in adj[p]:
        ans[j]+=x
print(*ans)

Submission status: Wrong Answer

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


def dfs(u, add):
    cnt[u] += add
    for nu in graph[u]:
        dfs(nu, add)


n, q = map(int, input().split())
graph = defaultdict(list)

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

cnt = [0] * (n + 1)


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

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

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

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

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

ans = [0] * N 
def dfs(n: int, score: int):
    score += scores[n]
    ans[n] = score
    for v in tree[n]:
        dfs(v, score)

dfs(0, 0)

print(*ans)

Submission status: Wrong Answer

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


def update(k, m):
    v[k - 1] += m
    if l[k] == []:
        pass
    else:
        for i in l[k]:
            update(i, m)


n, q = map(int, input().split())
v = [0] * n
l = {i: [] for i in range(1, n + 1)}
for i in range(n - 1):
    a, b = map(int, input().split())
    l[a].append(b)
for i in range(q):
    p, x = map(int, input().split())
    update(p, x)
print(*v)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
root = []
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root.append([a, b])
  
root.sort()
for i in range(Q):
  p, x = map(int, input().split())
  li = [p]
  for j in range(N-1):
    if root[j][0] in li:
      li.append(root[j][1])
  for k in li:
    ans[k-1] += x
    
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
root = []
ans = [0]*N

for i in range(N-1):
  a, b = map(int, input().split())
  root.append([a, b])
  
for i in range(Q):
  p, x = map(int, input().split())
  li = [p]
  for j in range(N-1):
    if root[j][0] in li:
      li.append(root[j][1])
  for k in li:
    ans[k-1] += x
    
print(' '.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
import heapq as hp

n,q=[int(x) for x in input().rstrip().split()]
l=[[] for i in range(n)]
pul=[0]*n
for i in range(n-1):
  a,b=map(int,input().split())
  l[a-1].append(b-1)
  # l[b-1].append(a-1)


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


ans=[0]*n
def dfs():
  val=0
  que=[[0,val]]
  done=[0]*n  

  while(que):
    now,val=que.pop(0)
    if done[now]==1:
      continue
    val+=pul[now]
    ans[now]=val
    done[now]=1

    for i in l[now]:
      if done[i]==0:
        que.append([i,val])

dfs()
print(*ans)

Submission status: Wrong Answer

Solution code:
import heapq as hp

n,q=[int(x) for x in input().rstrip().split()]
l=[[] for i in range(n)]
pul=[0]*n
for i in range(n-1):
  a,b=map(int,input().split())
  l[a-1].append(b-1)

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

ans=[0]*n
def dfs():
  val=0
  que=[[0,val]]
  done=[0]*n
  
  while(que):
    now,val=que.pop(0)
    # if done[now]==1:
    #   continue
    val+=pul[now]
    ans[now]=val

    for i in l[now]:
      if done[i]==0:
        que.append([i,val])

dfs()
print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
pointer = [[] for i in range(0,n+1)]
counter = [0]*(n+1)
 
for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a].append(b)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p] += x

queue = [1]
while queue != []:
  idx = queue.pop(0)
  for j in pointer[idx]:
    counter[j] += counter[idx]
    queue.append(j)

for i in range(1, len(counter)):
  print(counter[i], end=" ")

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
pointer = [[] for i in range(0,n+1)]
counter = [0]*(n+1)
 
for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a].append(b)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p] += x

queue = [1]
while queue != []:
  idx = queue.pop(0)
  print(counter[idx], end = " ")
  for j in pointer[idx]:
    counter[j] += counter[idx]
    queue.append(j)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
input = stdin.readline

setrecursionlimit(10**6)

N,Q = map(int, input().split())
childlist = [[] for _ in range(N+1)]

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

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

def DFS(v):
    if not checkedlist[v]:
        checkedlist[v] = True
        
        global point
        point += pointlist[v]
        for child in childlist[v]:
            DFS(child)
        
        totallist[v] = point
        point -= pointlist[v]
        
totallist = [0]*(N+1)
checkedlist = [False]*(N+1)
point = 0
for i in range(1,N+1):
    if not checkedlist[i]:
        DFS(i)

print(*totallist[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N, Q = map(int, input().split())
    g = [[] for _ in range(N)]
    score = [0] * N
    seen = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)

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

    q = deque()
    q.append(0)
    while q:
        v = q.pop()
        seen[v] += 1
        for i in g[v]:
            if seen[i]:
                continue
            q.append(i)
            score[i] += score[v]

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N, Q = map(int, input().split())
    g = [[] for _ in range(N)]
    score = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)

    for _ in range(Q):
        a, b = map(int, input().split())
        q = deque()
        q.append(a-1)
        seen = [0] * N
        while q:
            v = q.pop()
            score[v] += b
            seen[v] += 1
            for i in g[v]:
                if seen[i] == 0:
                    q.append(i)

    print(*score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N, Q = map(int, input().split())
    g = [[] for _ in range(N)]
    score = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)

    for _ in range(Q):
        a, b = map(int, input().split())
        q = deque()
        q.append(a-1)
        seen = [0] * N
        while q:
            v = q.pop()
            score[v] += b
            seen[v] += 1
            for i in g[v]:
                if seen[i] == 0:
                    q.append(i)

    print(score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N, Q = map(int, input().split())
    g = [[] for _ in range(N)]
    score = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)

    for _ in range(Q):
        a, b = map(int, input().split())
        print(a, b)
        q = deque()
        q.append(a-1)
        seen = [0] * N
        while q:
            v = q.pop()
            score[v] += b
            seen[v] += 1
            for i in g[v]:
                if seen[i] == 0:
                    q.append(i)

    print(score)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
pointer = [[] for i in range(0,n)]
counter = [0]*n
 
for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

queue = [0]
while queue != []:
  idx = queue.pop(0)
  print(counter[idx], end = " ")
  queue.extend(pointer[idx])
  for j in pointer[idx]:
    counter[j] += counter[idx]

Submission status: Wrong Answer

Solution code:
import collections
n, q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(n-1)]
p_x = [list(map(int, input().split())) for _ in range(q)]


ans = n*[0]

def search(i):
    for a, b in a_b:
        if a == i:
            p_list.append(b)
            q.appendleft(b)
            
q = collections.deque()

for j in range(1, n+1):
    q.appendleft(j)
    p_list = []
    while q:
        i = q.pop()
        search(i)
    p_list.append(j)
    for p, x in p_x:
        if p == j:
            for num in p_list:
                ans[num-1] += x
        
print(" ".join(map(str, ans)))

Submission status: Wrong Answer

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

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

value_list = [0]*(N+1)

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

print(value_list)
from  collections import deque

que = deque([(0,1)])

while que:
    parent,me = que.popleft()
    value_list[me]+=value_list[parent]
    print(value_list)
    for child in graph[me]:
        if child==parent:
            continue
        else:
            que.append((me,child))


print(*value_list[1:])

Submission status: Wrong Answer

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

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

value_list = [0]*(N+1)

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

print(value_list)
from  collections import deque

que = deque([[0,1]])

while que:
    parent,me = que.popleft()
    value_list[me]+=value_list[parent]
    print(value_list)
    for child in graph[me]:
        if child==parent:
            continue
        else:
            que.append([me,child])


print(*value_list[1:])

Submission status: Wrong Answer

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

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

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

result = [None]*N
stack = [(0,0)]
while stack:
    v,a = stack.pop()
    a += memo[v]
    result[v] = a
    for u in adj[v]:
        stack.append((u,a))

print(*result)

Submission status: Wrong Answer

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

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

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

result = [0]*N
stack = [0]
while stack:
    v = stack.pop()
    result[v] += memo[v]
    for u in adj[v]:
        result[u] += result[v]
        stack.append(u)

print(*result)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
#n = int(input())
n, q = map(int, sys.stdin.readline().split())
#s = input()
#a = list(map(int, sys.stdin.readline().split()))

r = [0 for i in range(n+1)]
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    r[b] = a

c = [0 for i in range(n+1)]
for i in range(q):
    p, x = map(int, sys.stdin.readline().split())
    c[p] += x

for i in range(1, n+1):
    c[i] += c[r[i]]
    print(c[i], end = " ")

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
tree=[[0]*n for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    tree[a-1][b-1]=1
    tree[b-1][a-1]=1
cnt=[0]*n
for _ in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x
colors=["white" for _ in range(n)]

stack=deque([0])
colors[0]="gray"
while stack:
    current=stack.pop()
    for i in range(n):
        if tree[current][i]==1 and colors[i]=="white":
            stack.append(i)
            colors[i]="gray"
        if tree[current][i]==1:
            cnt[i]+=cnt[current]
    colors[current]="black"
print(*cnt)