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

graph = [[] for _ in range(N)]
for ai, bi in ab:
    graph[ai - 1].append(bi - 1)
    graph[bi - 1].append(ai - 1)

parent = [None] * N
parent[0] = 0

def dfs(src):
    for dst in graph[src]:
        if parent[dst] != None: continue
        parent[dst] = src
        dfs(dst)

dfs(0)
# print("#", parent)

score = [0] * N
for pi, xi in px:
    score[pi - 1] += xi
        
memo = [None] * N
def dp(arg):
    if memo[arg] != None: return memo[arg]
    ret = score[arg]
    if arg != parent[arg]: ret += dp(parent[arg])
    memo[arg] = ret
    return ret

ans = []
for i in range(N):
    ans.append(dp(i))

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]
es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)
ps = [0] * N
for p,x in PX:
    ps[p-1] += x

ans = [0] * N
def dfs(v,p=-1, c=0):
    ans[v] = c + ps[v]
    for to in es[v]:
        if to==p: continue
        dfs(to, v, ans[v])
dfs(0)
print(*ans)

Submission status: Runtime Error

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

n_nodes, q_query = map(int, input().split())

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

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

# print(n_nodes, q_query)
# print(edges, queries)

res = [0 for _ in range(n_nodes)]
for node, point in queries:
    res[node] += point


def dfs(v, parent):
    for v_next in edges[v]:
        if v_next != parent:
            res[v_next] += res[v]
            dfs(v_next, v)


dfs(0, None)

print(*res)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1E+7)
def main():
    N, Q = map(int, input().split())
    link = [[] for _ in range(N+1)]
    for n in range(N-1):
        a, b = map(int, input().split())
        link[a].append(b)
        link[b].append(a)
    V = [0] * (N+1)
    ans = [0] * (N+1)
    for q in range(Q):
        p, x = map(int, input().split())
        V[p] += x

    def dfs(i, parent, acc):
        V[i] += acc
        for j in link[i]:
            if j != parent:
                dfs(j,i,V[i])
    cur = 1
    parent = 0
    acc = 0
    dfs(cur, parent , acc)
    V = [str(v) for v in V]
    print (" ".join(V[1:]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

n_nodes, q_query = map(int, input().split())

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

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

# print(n_nodes, q_query)
# print(edges, queries)


def dfs(v, parents):
    for v_next in edges[v]:
        if parents[v_next] == -1:
            parents[v_next] = v
            dfs(v_next, parents)


parents = [-1 for _ in range(n_nodes)]
parents[0] = 0
dfs(0, parents)
# print(parents)

res = [0 for _ in range(n_nodes)]
for node, point in queries:
    res[node] += point

for node in range(n_nodes):
    if node > 0:
        p = parents[node]
        res[node] += res[p]

print(*res)

Submission status: Runtime Error

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

n_nodes, q_query = map(int, input().split())

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

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

# print(n_nodes, q_query)
# print(edges, queries)

def dfs(v, parents):
    for v_next in edges[v]:
        if parents[v_next] == -1:
            parents[v_next] = v
            dfs(v_next, parents)


parents = [-1 for _ in range(n_nodes)]
parents[0] = 0
dfs(0, parents)
# print(parents)

res = [0 for _ in range(n_nodes)]
for node, point in queries:
    res[node] += point

for node in range(n_nodes):
    if node > 0:
        p = parents[node]
        res[node] += res[p]

print(res)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
#link = [[0]*(N+1) for _ in range(N+1)]
link = [[] for _ in range(N+1)]
for n in range(N-1):
    a, b = map(int, input().split())
    # link[a][b] = 1
    # link[b][a] = 1
    link[a].append(b)
    link[b].append(a)
V = [0] * (N+1)
ans = [0] * (N+1)
for q in range(Q):
    p, x = map(int, input().split())
    V[p] += x

def dfs(i, parent, acc):
    V[i] += acc
    # for j in range(1,N+1):
    #     if link[i][j] == 1 and parent!=j:
    #         dfs(j,i,V[i])
    for j in link[i]:
        if j==parent:
            continue
        dfs(j,i,V[i])
cur = 1
parent = 0
acc = 0

dfs(cur, parent , acc)
V = [str(v) for v in V]
print (" ".join(V[1:]))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
#link = [[0]*(N+1) for _ in range(N+1)]
link = [[] for _ in range(N+1)]
for n in range(N-1):
    a, b = map(int, input().split())
    # link[a][b] = 1
    # link[b][a] = 1
    link[a].append(b)
    link[b].append(a)
V = [0] * (N+1)
ans = [0] * (N+1)
for q in range(Q):
    p, x = map(int, input().split())
    V[p] += x

def dfs(i, parent, acc):
    V[i] += acc
    # for j in range(1,N+1):
    #     if link[i][j] == 1 and parent!=j:
    #         dfs(j,i,V[i])
    for j in link[i]:
        if j!=parent:
            dfs(j,i,V[i])
cur = 1
parent = 0
acc = 0

dfs(cur, parent , acc)
V = [str(v) for v in V]
print (" ".join(V[1:]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100001)

n, k = list(map(int,input().split(' ')))
path = []
for i in range(n+1):
    path.append([])
for i in range(n-1):
    a, b = list(map(int,input().split(' ')))
    path[a].append(b)
val = [0] * (n+1)
for i in range(k):
    v, pt = list(map(int,input().split(' ')))
    val[v] += pt
# DFS
def f(current):
    for next_v in path[current]:
        val[next_v] += val[current]
        if path[next_v]:
            f(next_v)

f(1)

print(' '.join([str(pt) for pt in val[1:]]))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N, Q = map(int, input().split())

parent = [None] * N
child = defaultdict(list)
counter = [0] * N
pcounter = [0] * N

for i in range(N-1):
    a, b = map(int, input().split())
    parent[b-1] = a-1
    child[a-1].append(b-1)
for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x

def propagate(node, acc):
    pcounter[node] = counter[node] + acc
    for c in child[node]:
        propagate(c, pcounter[node])

propagate(0, 0)
print(*pcounter, sep=' ')

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
K = [[] for _ in range(N+1)]
nums = [0 for _ in range(N+1)]

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

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

def dfs(now,parent):
    for i in K[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i,now)

dfs(1,0)
print(*nums[1:])

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
K = [[] for _ in range(N+1)]
nums = [0] * (N+1)

def dfs(now,parent):
    for i in K[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i,now)

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

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
K = [[] for _ in range(N+1)]
ans = [0] * (N+1)
nums = [0] * (N+1)

def dfs(now,parent):
    for i in K[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i,now)

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

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

print(' '.join(str(i) for i in nums[1:]))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
K = [[] for _ in range(N)]
ans = [0] * N
def dfs(p,x):
    start[p] = 1
    ans[p] += x
    for v in K[p]:
        if start[v] != -1:
            continue
        dfs(v,x)

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

for _ in range(Q):
    p,x = map(int,input().split())
    p -= 1
    start = [-1] * N
    dfs(p,x)
print(' '.join(str(i) for i in ans))

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
K = [[] for _ in range(N)]
ans = [0] * N
def dfs(p,x):
    ans[p] += x
    for v in K[p]:
        dfs(v,x)

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

for _ in range(Q):
    p,x = map(int,input().split())
    p -= 1
    dfs(p,x)
print(' '.join(str(i) for i in ans))

Submission status: Runtime Error

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

class Node(object):
	def __init__(self, num):
		self.num = num #各ノードの数字0〜9
		self.children = []
		self.c = 0
	#heapq.heapify()する際に必要となる比較関数
	def __lt__(self, x):
		return self.value < x.value
	#子どもをセットする
	def setChildren(self, x):
		self.children.append(x)
	def solve(self,x):
		self.c += x
		for child in self.children:
			table[child].solve(x)


table = [Node(i+1) for i in range(n)]
counter = [0 for _ in range(n)]

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

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

for i in range(n):
	if counter[i] != 0:
		table[i].solve(counter[i])

ans = [str(table[i].c) for i in range(n)]
print(" ".join(ans))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
D = {i: [] for i in range(N)}
E = {i: [] for i in range(N)}
par = [-1 for _ in range(N)]
par[0] = 5000000000
for _ in range(N - 1):
  a, b = map(int, input().split())
  D[a-1].append(b-1)
  D[b-1].append(a-1)
p = [0]
cnt = 1
pnt = 0
while cnt < N:
  n = cnt
  for i in range(pnt, cnt):
    for j in D[i]:
      if par[j] == -1:
        E[i].append(j)
        par[j] = i
        cnt += 1
        p.append(i)
  pnt = n
counter = [0 for _ in range(N)]
for _ in range(Q):
  p, x = map(int, input().split())
  counter[p-1] += x
def dfs(k):
  if E[k] != []:
    for m in E[k]:
      counter[m] += counter[k]
      dfs(m)
dfs(0)
for i in range(N-1):
  print(counter[i], end = ' ')
print(counter[N-1])

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
D = {i: [] for i in range(N)}
E = {i: [] for i in range(N)}
par = [-1 for _ in range(N)]
par[0] = 5000000000
for _ in range(N - 1):
  a, b = map(int, input().split())
  D[a-1].append(b-1)
  D[b-1].append(a-1)
p = [0]
cnt = 1
pnt = 0
while cnt < N:
  n = cnt
  for i in range(pnt, cnt):
    for j in D[i]:
      if par[j] == -1:
        E[i].append(j)
        par[j] = i
        cnt += 1
        p.append(i)
  pnt = n
counter = [0 for _ in range(N)]
def dfs(k, v):
  if E[k] == []:
    counter[k] += v
  else:
    counter[k] += v
    for l in E[k]:
      dfs(l, v)
for _ in range(Q):
  p, x = map(int, input().split())
  dfs(p-1, x)
for i in range(N-1):
  print(counter[i], end = ' ')
print(counter[N-1])

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(100000)
N, Q = map(int, input().split())
A, B = [], []

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

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


def dfs(now, parent):
    for i in edges[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i, now)


dfs(1, 0)
for n in nums[1:]:
    print(n)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
A, B = [], []

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

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


def dfs(now, parent):
    for i in edges[now]:
        if i != parent:
            nums[i] += nums[now]
            dfs(i, now)


dfs(1, 0)
for n in nums[1:]:
    print(n)

Submission status: Runtime Error

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

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

par = [-1]*n
for i in range(n-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

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

def dfs(now, prev):
    # リストはglobal宣言の必要がない？
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        if next != prev:
            chk[next] += chk[now]
            dfs(next, now)

dfs(0,-1)
chk = list(map(str,chk))
print(' '.join(chk))

Submission status: Runtime Error

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

def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return False
    else:
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True
      
par = [-1]*n
for i in range(n-1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)
    unite(u,v)

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

def dfs(now, prev):
    # リストはglobal宣言の必要がない？
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        if next != prev:
            chk[next] += chk[now]
            dfs(next, now)

par = tuple(par)            
for i in range(n):
  if par[i] < 0:
    dfs(i,-1)

chk = list(map(str,chk))
print(' '.join(chk))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
px = [list(map(int, input().split())) for i in range(q)]
work = [[] for i in range(n)]
counter = [0 for i in range(n)]
for a, b in ab:
    work[a-1].append(b)
def add(n, x):
    temp = work[n-1]
    if temp == []:
        return
    for i in temp:
        counter[i-1] += x
        add(i, x)
for p, x in px:
    counter[p-1] += x
    add(p, x)
print(" ".join(map(str, counter)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
px = [list(map(int, input().split())) for i in range(q)]
work = [[] for i in range(n)]
counter = [0 for i in range(n)]
for a, b in ab:
    work[a-1].append(b)
def add(n, x):
    temp = work[n-1]
    if temp == []:
        return
    for i in temp:
        counter[i-1] += x
        add(i, x)
for p, x in px:
    counter[p-1] += x
    add(p, x)
print(counter)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

## BFS

#入力受け取り
N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    #x[i][0] -= 1
    #x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

global ans
ans = [0]*(N+1)

def dfs(fr, nx):
    ans[nx] += ans[fr] + cnt[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(1, 1)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

## BFS

#入力受け取り
N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

ans = [0]*N

def dfs(fr, nx):
    global ans
    ans[nx] += ans[fr] + c[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

## BFS

#入力受け取り
N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

ans = [0]*N

global ans
def dfs(fr, nx):
    ans[nx] += ans[fr] + c[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

## BFS

#入力受け取り
N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

ans = [0]*N

global ans
def dfs(fr, nx):
    ans[nx] += ans[fr] + c[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(0, 0)

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

Submission status: Runtime Error

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


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

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (N+1)
for p,x in PX:
    value[p] += x

def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

answer = ' '.join(map(str,value[1:]))
print(answer)

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
graph = [[] for _ in range(N)]
point = [0]*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)
for i in range(Q):
    p, x = map(int, input().split())
    point[p-1] += x
def dfs(s,parent):
    for i in graph[s]:
        if i == parent:
            continue
        point[i] += point[s]
        dfs(i,s)
dfs(0,-1)
print(*point)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
'''
from collections import deque
N, Q = mi()
D = li2(N-1)
px = li2(Q)
que = deque()

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

for i in range(N-1):
    adj[D[i][0]].append(D[i][1])

ans = [0]*(N+1)
for j in range(Q):
    ind, a = px[j][0], px[j][1]
    que.append(ind)
    while(que!=deque()):
        v = que.popleft()
        ans[v] += a
        for e in adj[v]:
            que.append(e)


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

from collections import deque
sys.setrecursionlimit(10**6)

#DFS
def dfs(x):
    if flag[x]:
        return
    else:
        flag[x] = 1
        for a in adj[x]:
            dfs(a)
        global ans
        ans.appendleft(x)

#入力受け取り
N, Q = mi()
x = li2(N-1)
p = li2(Q)

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])

#DFSを利用したトポロジカルソート
flag = [0]*N
ans = deque([])
for i in range(N):
    dfs(i)
#print(ans)
cnt = [0]*N
for i in range(Q):
    ind = bisect.bisect_left(ans, p[i][0]-1)
    cnt[ind] += p[i][1]

#print(adj)
#print(cnt)
que = deque()
kotae = [0]*N

for a in ans:
    que.append(a)
    kotae[a] += cnt[a]
    while que!=deque():
        v = que.popleft()
        for next in adj[v]:
            que.append(next)
            kotae[next] += cnt[a]

for k in kotae:
    print(k, '', end='')

Submission status: Runtime Error

Solution code:
N,Q = [int(i) for i in input().split()] #N個の頂点、Q回の操作
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:
    tree[a].append(b)

#print(tree)

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

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

#print(counter)

def dfs(pos):
    for next_pos in tree[pos]:
        counter[next_pos] += counter[pos]
        dfs(next_pos)

dfs(1)

#print(counter)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
AB = [[int(i) for i in input().split()] for _ in range(N-1)]
PX = [[int(j) for j in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
    
counters = [0] * (N+1)
for p,x in PX:
    counters[p] += x
    
def dfs(v,parent,add):
    counters[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,counters[v])

dfs(1,0,0)

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

Submission status: Runtime Error

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


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

ans=[0]*N
def dfs(start, pre, preval):
  addval=preval+add[start]
  ans[start]+=addval
  for next_node in tree[start]:
    dfs(next_node, start,addval)
dfs(0,-1,0)  
for a in ans:
  print(a, end=" ")

Submission status: Runtime Error

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

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

ans=[0]*N
def dfs(start, pre, preval):
  addval=preval+add[start]
  ans[start]+=addval
  for next_node in tree[start]:
    if next_node==pre:
      continue
    dfs(next_node, start,addval)
dfs(0,-1,0)  
for a in ans:
  print(a, end=" ")

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)
from collections import deque
N,Q = (int(x) for x in input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]
par = [0] * N
ans = [0] * N
par[0] = 500000
for i in ab:
    temp = [par[j-1] for j in i]
    wch = temp.index(0)
    par[i[wch]-1] = i[abs(wch-1)]
def count(vert):
    if vert == 1:
        prev = 0
    else:
        prev = ans[par[vert-1]-1]
    ans[vert-1] = prev + sum([px[i][1] for i in range(Q) if px[i][0] == vert])
q = deque([1])
while len(q) > 0:
    temp = q.popleft()
    if temp is not None:
        count(temp)
        children = [x for x, y in enumerate(par) if y == temp]
        for child in children:
            q.append(child+1)
print(*ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N,Q = (int(x) for x in input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]
par = [0] * N
ans = [0] * N
par[0] = 500000
for i in ab:
    temp = [par[j-1] for j in i]
    wch = temp.index(0)
    par[i[wch]-1] = i[abs(wch-1)]
def count(vert):
    if vert == 1:
        prev = 0
    else:
        prev = ans[par[vert-1]-1]
    ans[vert-1] = prev + sum([px[i][1] for i in range(Q) if px[i][0] == vert])
q = deque([1])
while len(q) > 0:
    temp = q.popleft()
    if temp is not None:
        count(temp)
        children = [x for x, y in enumerate(par) if y == temp]
        for child in children:
            q.append(child+1)
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    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)]

    edge = [[] for _ in range(N)]
    for i in ab:
        edge[i[0]-1].append(i[1]-1)
        edge[i[1]-1].append(i[0]-1)
    cnt = [0]*N
    for i in px:
        cnt[i[0]-1] += i[1]

    stk = []
    visited = set()
    stk.append(0)

    def dfs(v, par):
        if v>0:
            cnt[v] += cnt[par]
        for i in edge[v]:
            if i!=par:
                dfs(i, v)

    dfs(0, -1)

    # while len(stk):
    #     print(stk, visited)
    #     v = stk.pop()
    #     if not v in visited:
    #         visited.add(v)
    #     if v>0:
    #         cnt[v] += cnt[par]
    #     for i in edge[v]:
    #         if not i in visited:
    #             stk.append(i)
    #     par = v

    print(*cnt)

if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)
n,q = map(int,input().split())
tree = [[] for _ in range(n)]
point = [0] * n

for _ in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    
for _ in range(q):
    a,b = map(int,input().split())
    a -= 1
    point[a] += b
    
def dfs(now, prev = -1):
    for next in tree[now]:
        if next == prev:
            continue
        point[next] += point[now]
        
        dfs(next, now)
        
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
tree = [[] for _ in range(n)]
point = [0] * n

for _ in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    
for _ in range(q):
    a,b = map(int,input().split())
    a -= 1
    point[a] += b
    
def dfs(now, prev = -1):
    for next in tree[now]:
        if next == prev:
            continue
        point[next] += point[now]
        
        dfs(next, now)
        
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
tree = [[] for _ in range(n)]
point = [0] * n

for _ in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
    
for _ in range(q):
    a,b = map(int,input().split())
    point[a-1] += b
    
def dfs(now, prev = -1):
    for next in tree[now]:
        if next == prev:
            continue
        point[next] += point[now]
        
        dfs(next,now)
        
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
G = dict([(i,[]) for i in range(1,N+1)])
for i in range(N-1):
  a,b = map(int,input().split())
  G[a].append(b)
  G[b].append(a)
lo = [0 for i in range(N+1)]
for i in range(Q):
  p,x = map(int,input().split())
  lo[p] += x

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

def dfs(G,p):
  for v in G[p]:
    if seen[v] == 0:
      seen[v] = 1
      cnt[v] = cnt[p] + lo[v]
      dfs(G,v)
    else:
      continue

###探索
cnt[1] = lo[1]
seen[1] = 1
dfs(G,1)
###
print(*cnt[1:])