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: Runtime Error

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

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

operations = []
for _ in range(Q):
    operations.append(list(map(int, input().split())))

# print(points)
# print(operations)


def dfs(points, counters, i, count):
    counters[i - 1] += count
    for next_point in points[i - 1]:
        dfs(points, counters, next_point, count)


for operation in operations:
    dfs(points, counters, operation[0], operation[1])
# print(counters)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
D - Ki
https://atcoder.jp/contests/abc138/tasks/abc138_d

"""
import sys


from functools import lru_cache
from collections import deque

def solve(N, Q):
    @lru_cache(maxsize=None)
    def calc_score(n):
        if n == 1 or nodes[n][3]:
            return nodes[n][0]
        res = calc_score(nodes[n][2]) + nodes[n][0]
        nodes[n][3] = True
        return res

    nodes = [[0, -1, -1, False] for _ in range(N+1)]
    edges = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[a].append(b)
    for _ in range(Q):
        p, x = map(int, input().split())
        nodes[p][0] += x

    visited = set()
    q = deque([1])
    while q:
        n = q.popleft()
        if n not in visited:
            visited.add(n)
            for m in edges[n]:
                q.appendleft(m)
                nodes[m][2] = n
    ans = [calc_score(i) for i in range(1, N+1)]
    return ans


def main(args):
    N, Q = map(int, input().split())
    ans = solve(N, Q)
    print(*ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
D - Ki
https://atcoder.jp/contests/abc138/tasks/abc138_d

"""
import sys


from functools import lru_cache

def solve(N, Q):
    @lru_cache(maxsize=None)
    def calc_score(n):
        if n == 1 or nodes[n][3] == True:
            return nodes[n][0]
        res = calc_score(nodes[n][2]) + nodes[n][0]
        nodes[n][3] = True
        return res

    nodes = [[0, -1, -1, False] for _ in range(N+1)]

    for _ in range(N-1):
        a, b = map(int, input().split())
        nodes[b][2] = a
    for _ in range(Q):
        p, x = map(int, input().split())
        nodes[p][0] += x
    ans = [calc_score(i) for i in range(1, N+1)]
    return ans


def main(args):
    N, Q = map(int, input().split())
    ans = solve(N, Q)
    print(*ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

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

g = [[] for _ in range(n)]
c = [0] * n
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i] = True
    for j in g[i]:
        if visited[j] == True:
            continue
        c[j]+=c[i]
        dfs(j)

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())
    c[p - 1]+=x

dfs(0)

c=[str(i) for i in c]
print(' '.join(c))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
g = [[] for _ in range(n)]
score = [0]*n
visited = [False]*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 j in range(q):
  p, x = map(int, input().split())
  p -= 1
  score[p] += x

def dfs(i):
  visited[i] = True
  for j in g[i]:
    if visited[j] == True:
      continue
    score[j] += score[i]
    dfs(j)
dfs(0)
print(*score)

Submission status: Runtime Error

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

g = [[] for _ in range(n)]
c = [0] * n
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i] = True
    for j in g[i]:
        if visited[j] == True:
            continue
        c[j]+=c[i]
        dfs(j)

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())
    c[p - 1]+=x

dfs(0)
print(*c)

Submission status: Runtime Error

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

g = [[] for _ in range(n)]
c = [0] * n
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i] = True
    for j in g[i]:
        if visited[j]:
            continue
        c[j]+=c[i]
        dfs(j)

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())
    c[p - 1]+=x

dfs(0)
print(*c)

Submission status: Runtime Error

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

g = [[] for _ in range(n)]
c = [0 for _ in range(n)]
visited = [False] * n

#根に近い順に累積和
def dfs(i):
    visited[i]=True
    for j in g[i]:
        if visited[j]:
            continue
        c[j]+=c[i]
        dfs(j)

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())
    c[p - 1]+=x

dfs(0)
print(*c)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
G = [[] for _ in range(N)]
score = [0]*N
visited = [False]*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 j in range(Q):
  p, x = map(int, input().split())
  p -= 1
  score[p] += x

def dfs(pos):
  visited[pos] = True
  for next_pos in G[pos]:
    if visited[next_pos] == True:
      continue
    score[next_pos] += score[pos]
    dfs(next_pos)
dfs(0)
score = [str(s)  for s in score]
ans = ' '.join(score)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

N, Q = map(int, input().split())
info = [[] for _ in range(N)]
sum = [0 for _ in range(N)]

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


def plus(n, val):
    sum[n] += val
    for i in info[n]:
        plus(i, val)


for _ in range(Q):
    p, x = map(int, input().split())
    plus(p - 1, x)
for i in range(N):
    sum[i] = str(sum[i])
print(" ".join(sum))

Submission status: Runtime Error

Solution code:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

N, Q = map(int, input().split())
info = [[] for _ in range(N)]
sum = [0 for _ in range(N)]

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


def plus(n, val):
    sum[n] += val
    for i in info[n]:
        plus(i, val)


for _ in range(Q):
    p, x = map(int, input().split())
    plus(p-1, x)
for i in sum:
    print(i)

Submission status: Runtime Error

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

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

node = [[[],0] for i in range(N+1)] #[childrens],counter
    
def dfs(parent,i):
    for k in edge[i]:
        if parent != k:
            node[i][0].append(k)
            dfs(i,k)

def dfs_solve(p,x):
    node[p][1] += x
    for i in node[p][0]:
        dfs_solve(i,x)
        
dfs(-1,1)

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

ans = []

for i in range(1,N+1):
    ans.append(node[i][1])

print(*ans)

Submission status: Runtime Error

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

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

node = [[[],0] for i in range(N+1)] #[childrens],counter
    
def dfs(parent,i):
    for k in edge[i]:
        if parent != k:
            node[i][0].append(k)
            dfs(i,k)

def dfs_solve(p,x):
    node[p][1] += x
    for i in node[p][0]:
        dfs_solve(i,x)
        
dfs(-1,1)

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

for i in range(1,N+1):
    print(node[i][1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(500000)

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

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

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

result = [0 for _ in range(n)]


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(50000)

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

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

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

result = [0 for _ in range(n)]


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(5000)

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

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

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

result = [0 for _ in range(n)]


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
count = [0] * (N + 1)
g = [[] for _ in range(N+1)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
for _ in range(Q):
    v, val = map(int, input().split())
    count[v] += val
q = collections.deque()
q.append(1)
check = [0] * (N + 1)
while q:
    v = q.pop()
    check[v] = 1
    for u in g[v]:
        if check[u] == 1:
            continue
        count[u] += count[v]
        q.append(u)
print(*count[1:])

Submission status: Runtime Error

Solution code:
N, Q = map(int, raw_input().split())
G = [[] for i in range(N)]

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

seen = [False]*N
root_list = [[i] for i in range(N)]
todo = []

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

def dfs(G, v):
    seen[v] = True
    for next_v in G[v]:
        ans[next_v] += ans[v]
        if seen[next_v]:
            break
        else:
            dfs(G, next_v)

dfs(G,0)

#print ans                                                                                             
print ' '.join(map(str, ans))

Submission status: Runtime Error

Solution code:
N, Q = map(int, raw_input().split())
G = [[] for i in range(N)]

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

seen = [False]*N
root_list = [[i] for i in range(N)]
todo = []

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

def dfs(G, v):
    seen[v] = True
    for next_v in G[v]:
        ans[next_v] += ans[v]
        if seen[next_v]:
            break
        else:
            dfs(G, next_v)

dfs(G,0)

print ans

Submission status: Runtime Error

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

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

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

def dfs(x, d, s):
    for y in v[x]:
        if d[y]:
            s += f[y]
            ans[y] = s
            d[y] = 0
            dfs(y, d, s)
            s -= f[y]

ans = [0]*n
d = [1]*n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0, d, s)
print(*ans)

Submission status: Runtime Error

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

counter = [0]*(N+1)
def dfs(p,x):
  counter[p] += x
  if len(tree[p])==0:
    return None
  for q in tree[p]:
    dfs(q,x)
  return None

for i in range(Q):
  p,x = map(int, input().split())
  dfs(p,x)
  
for i in range(1,N+1):
  print(counter[i],end=' ')

Submission status: Runtime Error

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

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

def f(n,x):
    if d[n]==[]:
        return 0
    for m in d[n]:
        ans[m-1]+=x
        f(m,x)

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

for a in ans:
    print(a,end=' ')

Submission status: Runtime Error

Solution code:
N,Q = [int(i) for i in input().split()]
aaabbb = [[int(i) for i in input().split()] for j in range(N-1)]
pppxxx = [[int(i) for i in input().split()] for j in range(Q)]

tree = [[] for i in range(N+1)]

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

#print(tree)

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

def dfs(pos,x):
    #print(pos)
    counter[pos]+=x
    for next_pos in tree[pos]:
        dfs(next_pos,x)

for p,x in pppxxx:
    #print("newdfs")
    dfs(p,x)

print(" ".join([str(i) for i in counter[1:]]))

Submission status: Runtime Error

Solution code:
N,Q = [int(i) for i in input().split()]
aaabbb = [[int(i) for i in input().split()] for j in range(N-1)]
pppxxx = [[int(i) for i in input().split()] for j in range(Q)]

tree = [[] for i in range(N+1)]

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

#print(tree)

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

def dfs(pos,x):
    #print(pos)
    counter[pos]+=x
    for next_pos in tree[pos]:
        dfs(next_pos,x)

for p,x in pppxxx:
    #print("newdfs")
    dfs(p,x)

print(counter[1:])

Submission status: Runtime Error

Solution code:
# Nがノードの数, Qが操作回数
N, Q = map(int, input().split())

points = [0] * N
nodes = [set() for _ in range(N)]
parents = [-1] * N

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

def fill_parent(parent, n):
  global parents, nodes
  parents[n] = parent
  if parent in nodes[n] and len(nodes[n]) == 1:
    return
  for i in nodes[n]:
    if i != parent:
      fill_parent(n, i)

for i in nodes[0]:
  fill_parent(0, i)

h = {}
for i in range(N):
  if parents[i] not in h:
    h[parents[i]] = set()
  h[parents[i]].add(i)

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

c = [0] * N

def calc(n):
  if n == 0:
    c[n] = points[n]
  else:
    c[n] = points[n] + c[parents[n]]
  if n in h:
    for i in h[n]:
      calc(i)

calc(0)
print(*c)

Submission status: Runtime Error

Solution code:
input = sys.stdin.readline
N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N-1)]
P = [0] * Q
X = [0] * Q
V = [0] * (N + 1)
for i in range(Q):
    P, X = map(int, input().split())
    V[P] += X

Submission status: Runtime Error

Solution code:
input = sys.stdin.readline
N, Q = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N-1)]
P = [0] * Q
X = [0] * Q
V = [0] * (N + 1)
for i in range(Q):
    P, X = map(int, input().split())
    V[P] += X

tr = Tree(N, AB)

Submission status: Runtime Error

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

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

def dfs(v, p = -1):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

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

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

'''
#import numpy as np
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
"""dd = [(-1, 0), (0, 1), (1, 0), (0, -1)];
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)];
ddn9 = ddn + [(0, 0)]
for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""

def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]  # WideIntPoint
def ws(): return sys.stdin.readline().split()
def si(): return int(sys.stdin.readline())  # SingleInt
def ss(): return input()
def hi(n): return [si() for _ in range(n)]
def hs(n): return [ss() for _ in range(n)]  # HeightString
def s_list(): return list(input())
def mi(n): return [wi() for _ in range(n)]  # MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]  # NumberGrid
def grid(n): return [s_list() for _ in range(n)]

def main():
'''

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

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

def dfs(v, pv):
    for nv in G[v]:
        if nv != pv:
            P[nv] += P[v]
            dfs(nv, v)


dfs(0, 0)
print(*P)


'''
if __name__ == '__main__':
    main()
'''

Submission status: Runtime Error

Solution code:
vimport sys 
sys.setrecursionlimit(10**6)
input = lambda: sys.stdin.readline().rstrip()

def dfs(v, pv):
    # global P

    for nv in G[v]:
        if nv != pv:
            P[nv] += P[v]
            dfs(nv, v)

n,q = map(int, input().split())
G = [[] for _ in range(n)]              # adjacent list
for i in range(n-1):
    a,b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

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


dfs(0, 0)
print(*P)

Submission status: Runtime Error

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

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

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

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

dfs(0,-1,0)
 
print(*ans)

Submission status: Runtime Error

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

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

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

ans = [0]*n

def dfs(v,p,value): #p: parent of v
  value += opr[v]
  ans[v-1] = value
  for c in tree[v]:
    if c == p: continue
    dfs(c,v,value)

dfs(1,-1,0)

print(*ans)

Submission status: Runtime Error

Solution code:
import math 
import bisect
import heapq
from operator import mul
from collections import deque

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



def dfs(start):
    if start == 1:
        return [True] * n
    task = deque([])
    seen = [False] * n

    task.appendleft(start)
    seen[start-1] = True

    while task:
        task.popleft()
        for i in range(len(ab)):
            if ab[i][0] == start and seen[ab[i][1]-1] == False:
                next = ab[i][1]
                seen[next-1] = True
                task.appendleft(next)


    return seen


ans = [0]*n

for i in range(q):
    for j in range(n):
        ans[j] += px[i][1] if dfs(px[i][0])[j] else 0
for i in range(len(ans))
print(ans[i])

Submission status: Runtime Error

Solution code:
N, Q  = map(int, input().split())
tree = [0]*(N+1)
counts = [0]*(N+1)
pluses = [0]*(N+1)
for i in range(1,N):
  tree[i] = []
  a,b = map(int, input().split())
  tree[a].append(b)
tree[N] = []

i = N
while i>0:
  pluses[i] = [i]
  if len(tree[i])!=0:
    for p in tree[i]:
      for q in pluses[p]:
        pluses[i].append(q)
  i -= 1
#print(pluses)

for i in range(Q):
  p,x = map(int, input().split())
  for q in pluses[p]:
    counts[q]+=x
#print(tree)
for i in range(1,N+1):
  print(counts[i],end=' ')

Submission status: Runtime Error

Solution code:
N, Q  = map(int, input().split())
tree = [0]*(N+1)
counts = [0]*(N+1)
for i in range(1,N):
  tree[i] = []
  a,b = map(int, input().split())
  tree[a].append(b)
tree[N] = []

def plus(p,x):
  #print(p,x,counts)
  counts[p] += x
  if len(tree[p])==0:
  	pass
  else:
    #print(tree[p][-1],x)
    for q in tree[p]:
      plus(q,x)

for i in range(Q):
  p,x = map(int, input().split())
  plus(p,x)
#print(tree)
for i in range(1,N+1):
  print(counts[i],end=' ')

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    # AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for _ in range(N-1):
        root,leaf = map(int,input().split())
        roots[leaf-1] = root-1

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

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

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

    ans = [0 for _ in range(N)]
    for _ in range(Q):
        ans[int(p)-1] += int(x)
    # print(ans)
    for i in range(1,N):
        ans[i] += ans[roots[i+1]-1]
    print(*ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

N, Q = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
P = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    P[p-1] += x
ans = [-1] * N
#print(G)
#print(P)
"""
def dfs(v, p):
    ans[v] = P[v] + p
    for vi in G[v]:
        if ans[vi] != -1:
            continue
        dfs(vi, ans[v])
dfs(0, 0)
"""
for v 
print(' '.join(map(str, ans)))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

def dfs(cur,par):
    for chl in tree[cur]:
        if chl == par: #これで木の流れの逆行を防ぐ
            continue
        ans[chl] += ans[cur]
        dfs(chl,cur)
    
dfs(0,-111111)
print(*ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

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

counter = [0] * (N+1)

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

score(1)

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

Submission status: Runtime Error

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

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

def dfs(v,p):
  ans[v] = p + P[v]
  visited[v] = 1
  
  for nv in dic[v]:
    if visited[nv] == 0:
      dfs(nv,ans[v])
        
dfs(0,0)
print(*ans)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
tree = dict()
for _ in range(N-1):
  a, b = map(int, input().split())
  if a in tree:
    tree[a].append(b)
  else:
    tree[a] = [b]

point = [0] * (N+1)

def act(node, add):
  point[node] = point[node] + add
  if node in tree:
    for ch in tree[node]:
      act(ch, add)

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

ans = ' '.join([str(n) for n in point[1:]])
print(ans)

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
import sys
sys.setrecursionlimit(10**10)
T=[[] for i in range(N)]
S=[0 for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    T[a-1].append(b-1)
    T[b-1].append(a-1)
for i in range(Q):
    p,x=map(int,input().split())
    S[p-1]+=x
seen=[False for i in range(N)]
def dps(i):
    seen[i]=True
    for v in T[i]:
        if not seen[v]:
            S[v]+=S[i]
            dps(v)
dps(0)
print(*S)

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
import sys
sys.setrecursionlimit(10**10)
T=[set() for i in range(N)]
S=[0 for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    T[a-1].add(b-1)
    T[b-1].add(a-1)
for i in range(0,Q):
    p,x=map(int,input().split())
    S[p-1]+=x
seen=[False for i in range(N)]
def dps(i):
    seen[i]=True
    for v in T[i]:
        if seen[v]:
            continue
        else:
            S[v]+=S[i]
            dps(v)
dps(0)
print(*S)