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:
import sys


def count(i, r):
    counter[i] += counter[r] if r >= 0 else 0
    for e in es[i]:
        count(e, i)


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    es = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a-1].append(b-1)
    counter = [0]*N
    for _ in range(Q):
        p, x = map(int, input().split())
        counter[p-1] += x
    count(0, -1)
    print(*counter)

Submission status: Runtime Error

Solution code:
import sys


def count(i, c):
    counter[i] = c+rc[i]
    for e in es[i]:
        if counter[e] == 0:
            count(e, counter[i])


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    es = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a-1].append(b-1)
    counter = [0]*N
    rc = [0]*N
    for _ in range(Q):
        p, x = map(int, input().split())
        rc[p-1] += x
    count(0, 0)
    print(*counter)

Submission status: Runtime Error

Solution code:
import copy

class Node(object):
    def __init__(self, num):
        self.num = num
        self.adjacency_list = []
        self.values = 0

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

node = [Node(num=i) for i in range(N)]

for i in range(N - 1): # 0 ~ N-2
#     exec("a"+str(ab[i][0])+".adjacency_list.append("+str(ab[i][1])+")")#a>b
#     exec("a"+str(ab[i][1])+".adjacency_list.append("+str(ab[i][0])+")")#b>a
    node[ab[i][1]-1].adjacency_list.append(ab[i][0]-1)
    node[ab[i][0]-1].adjacency_list.append(ab[i][1]-1)
    #append使った

for i in range(Q):
    node[px[i][0]-1].values +=px[i][1]

def dfs138(parent, n): #children is list
    global node
    node[n].values += node[parent].values  #parent kara keisyou
    for i in node[n].adjacency_list:
        if i == parent:
            continue
        dfs138(n, i) #val_nを流す
#finish

dfs138([],0)

output = []
for n in range(N):
    val_n = node[n].values
    output.append(val_n)
print(' '.join([str(i) for i in output]))

Submission status: Runtime Error

Solution code:
import sys


def count(i, x):
    counter[i] += x
    for e in es[i]:
        count(e, x)


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    N, Q = map(int, input().split())
    es = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a].append(b)
    counter = [0 for _ in range(N+1)]
    for _ in range(Q):
        p, x = map(int, input().split())
        count(p, x)
    for i in range(1, N+1):
        if i == N:
            print(counter[i], end="\n")
        else:
            print(counter[i], end=" ")

Submission status: Runtime Error

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

edge = [[] for i in range(N)]
ans = [0] * N
query = [0] * N

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

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

is_visited = [False] * N

def dfs(cur, q):
    is_visited[cur] = True
    q = q + query[cur]
    ans[cur] += q

    for next in edge[cur]:
        if is_visited[next]:
            continue
        dfs(next, q)

dfs(0, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

edge = [[] for i in range(N)]
child = [{i} for i in range(N)]
ans = np.array([0] * N)

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

is_visited = [False] * N

def dfs(cur):
    is_visited[cur] = True

    for next in edge[cur]:
        if is_visited[next]:
            continue
        dfs(next)
        child[cur] = child[next] | child[cur]

dfs(0)

for q in range(Q):
    p, x = map(int, input().split())
    ans[list(child[p-1])] += x
    
print(*ans)

Submission status: Runtime Error

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

edge = [[] for i in range(N)]
child = [{i} for i in range(N)]
ans = [0] * N 

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

is_visited = [False] * N

def dfs(cur):
    is_visited[cur] = True

    for next in edge[cur]:
        if is_visited[next]:
            continue
        dfs(next)
        child[cur] = child[next] | child[cur]

dfs(0)

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

print(*ans)

Submission status: Runtime Error

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]

n,q = LI()

v = [[] for i in range(n)]
for i in range(n-1):
	a,b = LI()
	a -= 1
	b -= 1
	v[a].append(b)
	v[b].append(a)

f = [0]*n
for i in range(q):
	p,x = LI()
	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:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]

n,q = LI()

v = [[] for i in range(n)]
for i in range(n-1):
	a,b = LI()
	a -= 1
	b -= 1
	v[a].append(b)

f = [0]*n
for i in range(q):
	p,x = LI()
	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)
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:
import sys
sys.setrecursionlimit(20000)

N, Q = map(int, input().split())
T = [[] for _ in range(N)]
ans = [0] * N

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

#再帰は危険かも？
def dfs(s):
  ans[s-1] += x
  for i in T[s-1]:
    dfs(i)

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

print(*ans)

Submission status: Runtime Error

Solution code:
def c(i, p, f, g, k, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, k, vis)


def main():
    import sys
    sys.setrecursionlimit(200000)
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, k, vis)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def c(i, p, f, g, k, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, k, vis)


def main():
    import sys
    sys.setrecursionlimit(20000)
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, k, vis)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def c(i, p, f, g, k, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, k, vis)


def main():
    import sys
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, k, vis)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def c(i, p, f, g, vis):
    if vis[i]:
        return
    vis[i] = True
    w = f[i] + p
    k[i] = w
    ch = g[i]
    for x in ch:
        c(x, w, f, g, vis)


def main():
    import sys
    n, q, *abpx = map(int, sys.stdin.read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    c(1, 0, f, g, vis)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, q, *abpx = map(int, open(0).read().split())

    g = [[] for _ in range(n + 1)]
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    def c(n, p):
        if vis[n]:
            return
        vis[n] = True
        w = f[n] + p
        k[n] = w
        ch = g[n]
        for x in ch:
            c(x, w)

    c(1, 0)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def c(n, p, vis, k, f, g):
    if vis[n]:
        return
    vis[n] = True
    w = f[n] + p
    k[n] = w
    ch = g[n]
    for x in ch:
        c(x, w, vis, k, f, g)


def main():
    n, q = map(int, input().split())
    g = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        u, v = map(int, input().split())
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

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

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]
    c(1, 0, vis, k, f, g)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, q = map(int, input().split())
    g = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        u, v = map(int, input().split())
        g[u].append(v)
        g[v].append(u)

    f = [0 for _ in range(n + 1)]

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

    k = [0 for _ in range(n + 1)]
    vis = [False for _ in range(n + 1)]

    def c(n, p):
        if vis[n]:
            return
        vis[n] = True
        w = f[n] + p
        k[n] = w
        ch = g[n]
        for x in ch:
            c(x, w)

    c(1, 0)
    ans = ' '.join(map(str, k[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

stk = []

my_p = dict()

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

my_ret = dict()

stk = []
stk.append((0, my_p[0]))

visited = set()

while stk:
    ptr, pnt = stk.pop()
    visited.add(ptr)
    my_ret[ptr] = pnt
    for item in my_dict[ptr]:
        if item in visited:
            continue
        tmp = 0
        if item in my_p:
            tmp = my_p[item]
        stk.append((item, pnt + tmp))

ret = []

for i in range(n):
    ret.append(str(my_ret[i]))

print(' '.join(ret))

Submission status: Runtime Error

Solution code:
import itertools

N,Q=map(int,input().split())
tree=[[] for _ in range(N+1)]
children=[[] for _ in range(N+1)]
#tree[1]=[i for i in range(1,N+1)]
point=[0 for _ in range(N+1)]
addpoint=[]
for i in range(N-1):
    a,b=map(int,input().split())
    tree[a].append(b)
for i in range(Q):
    p,x=map(int,input().split())
    addpoint.append([p,x])

def addchildren(tree,v):
    global children
    if tree[v]==[]:
        return [v]
    for i in tree[v]:
        children[v]+=[i]
        children[v]+=addchildren(tree,i)
    return children[v]

addchildren(tree,1)
for i in range(len(children)):
    children[i]=list(set(children[i]))+[i]

for i in addpoint:
    #print(i)
    for j in children[i[0]]:
        #print("index:"+str(j))
        point[j]+=i[1]

for i in point[1:]:
    print(i,end=" ")

Submission status: Runtime Error

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

def dfs(y, x):
    cnt[y] += x
    for k in dic[y]:
        dfs(k, x)
for i in range(q):
    for j in dic[p[i][0]]:
        dfs(j, p[i][1])
    cnt[p[i][0]] += p[i][1]
for i in range(1, n+1):
    print(cnt[i], end=' ')
print()

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
node = [[]for i in range(0,N+1,1)]
for i in range(0,N-1,1):
    a,b = map(int,input().split())
    node[a].append(b)
node_score = [0]*(N+1)#スコア
for i in range(0,Q,1):
    p,x = map(int,input().split())
    node_score[p] += x

ans = [0]*(N+1)
def bfs(n,preN):
    ans[n] = ans[preN]+node_score[n]
    for to in node[n]:
        if to != preN:
            bfs(to,n)
bfs(1,0)
print(" ".join(list(map(str,ans[1:N+1]))))

Submission status: Runtime Error

Solution code:
def dfs(v,p): #点vについての処理。p：vの親
    if (p != -1): res[v] += res[p]
    
    for e in edges[v]:
        if (e == p): continue
        dfs(e,v)
#--------------------------------
N, Q = map(int, input().split())
edges = [[] for _ in range(N+1)]
for i in range(N-1):
    a,b = map(int, input().split())
    edges[a].append(b)
    edges[b].append(a)
#print(edges)
px = [list(map(int, input().split())) for _ in range(Q)]

res = [0]*(N+1) #点数リスト
for i in range(Q): #点数リストに初期値を格納
    p,x = px[i]
    res[p] += x

dfs(1,-1)
print(*res[1:])

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(2*10**5)
input = sys.stdin.readline

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

def dfs(now, parent):
    if parent != -1:
        ans[now] += ans[parent]
    for i in graph[now]:
        if i == parent:
            continue
        dfs(i, now)

dfs(0, -1)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(2*10**5)
input = sys.stdin.readline

n, q = map(int, input().split())
graph = {i: [] for i in range(n)}
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x

def dfs(now, parent):
    if parent != -1:
        ans[now] += ans[parent]
    for i in graph[now]:
        if i == parent:
            continue
        dfs(i, now)

dfs(0, -1)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(2*10**5)
input = sys.stdin.readline

n, q = map(int, input().split())
graph = {i: [] for i in range(n)}
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x

def dfs(now, parent):
    if parent != -1:
        ans[now] += ans[parent]
    for i in graph[now]:
        if i != parent:
            dfs(i, now)

dfs(0, -1)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
graph = {i: set() for i in range(n)}
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].add(b)
    graph[b].add(a)
ans = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x

def dfs(now, parent):
    if parent != -1:
        ans[now] += ans[parent]
    for i in graph[now]:
        if i != parent:
            dfs(i, now)

dfs(0, -1)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import deque
input = sys.stdin.readline

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [set() for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].add(bi-1)
        g[bi-1].add(ai-1)
    g=tuple(g)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
    xx=[0]*n
    for i in range(q):
        xx[p[i]]=xx[p[i]]+x[i]
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10    
    xx=[0]*n
    for i in range(q):
#        pi,xi=map(int,input().split())
        xx[p[i]]=xx[p[i]]+x[i]
        
#q = deque([0])
stack=[0]
parent=[-1]*n
while stack:
#    print(q)
    state = stack.pop()
#    print("------------- stack:",stack,"state:",state,"g:",g[state])
    for v in g[state]:
#        print("state:",state,"v:",v,"q:",q)
        if parent[state] == v:
            continue
        else:
            parent[v]=state
            stack.append(v)
            xx[v]+=xx[state]
#            print("parent:",parent,"xx:",xx,"stack",stack)

print(" ".join(map(str,xx[0: ])))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.buffer.readline  # これを使わないとTLEしてしまう
sys.setrecursionlimit(10**7)

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)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * (N+1)
for p, x in PX:
    P[p] += x

Count = [0] * (N+1)


def dfs(v, parent, point):
    # 現在いる頂点vの親までの値をpointとする
    Count[v] = point + p[v]
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, Count[v])


dfs(1, 0, 0)

res = ' '.join(map(str, Count[1:]))
print(res)

Submission status: Runtime Error

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

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, point):
    # 現在いる頂点vの親までの値をpointとする
    value[v] += point
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

res = ' '.join(map(str, P[1:]))
print(res)

Submission status: Runtime Error

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

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, point):
    # 現在いる頂点vの親までの値をpointとする
    value[v] += point
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x, v, value[v])


dfs(1, 0, 0)

res = ' '.join(map(str, P[1:]))
print(res)

Submission status: Runtime Error

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

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

# 木構造をリストで表現する
G = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a = a - 1
    b = b - 1
    G[a].append(b)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    P[p] += x

# 各頂点のカウンター
Count = [0] * N


def dfs(v, point):
    # 現在いる頂点vの親までの値をpointとし、
    new_point = point + P[v]
    Count[v] = str(new_point)

    for next_v in G[v]:
        dfs(next_v, new_point)


dfs(0, 0)

res = " ".join(Count)

print(res)

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 = map(int, input().split())
    a = a - 1
    b = b - 1
    G[a].append(b)

# 各操作で与えられる値を、各頂点ごとに集計しておく
P = [0] * N
for i in range(Q):
    p, x = map(int, input().split())
    p = p - 1
    P[p] += x

# 各頂点のカウンター
Count = [0] * N


def dfs(v, point):
    # 現在いる頂点vの親までの値をpointとし、
    new_point = point + P[v]
    Count[v] = str(new_point)

    for next_v in G[v]:
        dfs(next_v, new_point)


dfs(0, 0)

res = " ".join(Count)

print(res)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)    
from collections import deque

#seen=[]
seen=dict()
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

#seen=[False]*n
for i in range(n):
    seen[i]=False
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)    
from collections import deque

seen=[]
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

seen=[False]*n
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)    
from collections import deque

seen=[]
path=[]

def dfs(g, v):
    path.append(v)
    seen[v]=True
    for next_v in g[v]:
        if seen[next_v]:
            continue
        dfs(g, next_v)
    path.append(v)

    return

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    xx=[0]*n
    for i in range(q):
        pi,xi=map(int,input().split())
        xx[pi-1]=xx[pi-1]+xi
    
c=[0]*n

seen=[False]*n
dfs(g,0)

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(20000)    
from collections import deque

seen=[]
first_order =[]
last_order = []
path=[]

def dfs(g, v, first_ptr, last_ptr,xi):
    first_ptr=first_ptr+1
    last_ptr=first_ptr
    first_order[v] = first_ptr
    path.append(v)
    seen[v]=True
#    c[v]+=xi
#    print(v,c,g[v])          
    for next_v in g[v]:
        if seen[next_v]:
            continue
        first_ptr, last_ptr=dfs(g, next_v, first_ptr, last_ptr,xi)
    last_ptr=last_ptr+1
    first_ptr=last_ptr
    last_order[v] = last_ptr
    path.append(v)

    return first_ptr, last_ptr

icase=0
if icase==0:
    n,q=map(int,input().split())
    g = [deque([]) for _ in range(n)]
    for i in range(n-1):
        ai,bi=map(int,input().split())
        g[ai-1].append(bi-1)
        g[bi-1].append(ai-1)
    p=[0]*q
    x=[0]*q
    for i in range(q):
        pi,xi=map(int,input().split())
        p[i]=pi-1
        x[i]=xi
elif icase==1:
    n,q=3,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([2])
    g[1]=deque([2])
    g[2]=deque([0,1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=2
    x[1]=100
    p[2]=0
    x[2]=1    
#ans:1 111 101
elif icase==2:
    n,q=4,3
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1])
    g[1]=deque([0,2,3])
    g[2]=deque([1])
    g[3]=deque([1])
    p=[0]*q
    x=[0]*q
    p[0]=1
    x[0]=10    
    p[1]=0
    x[1]=100
    p[2]=2
    x[2]=1    
#ans:100 110 111 110
elif icase==3:
    n,q=6,2
    g = [deque([]) for _ in range(n)]
    g[0]=deque([1,2])
    g[1]=deque([0,3,4])
    g[2]=deque([0,5])
    g[3]=deque([1])
    g[4]=deque([1])
    g[5]=deque([2])
    p=[0]*q
    x=[0]*q
    p[0]=0
    x[0]=10    
    p[1]=0
    x[1]=10
#ans:100 110 111 110

xx=[0]*n
for i in range(q):
    xx[p[i]]=xx[p[i]]+x[i]
    
c=[0]*n

#for i in range(q):
#    seen=[False]*n
#    first_ptr=0
#    last_ptr=0
#    first_order =[-1]*n
#    last_order = [-1]*n
#    first_ptr, last_ptr=dfs(g,p[i],first_ptr, last_ptr,x[i])

seen=[False]*n
first_ptr=0
last_ptr=0
first_order =[-1]*n
last_order = [-1]*n
first_ptr, last_ptr=dfs(g,0,first_ptr, last_ptr,x[0])

#print(path)

seen=[False]*n

c=[0]*n
cpt=0
for pt in path:
    if seen[pt]==False:
        seen[pt]=True
#        print("pt:",pt,"cpt:",cpt,"xx[pt]:",xx[pt],"c[pt]:",c[pt]+cpt+xx[pt])
        cpt=cpt+xx[pt]
        c[pt]=c[pt]+cpt
    else:
        seen[pt]=False
        cpt=cpt-xx[pt]
           
for i in range(n):
    print(c[i],end=" ")
print()