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()))
ab = [[int(i) for i in input().split()] for _ in range(N-1)]
px = [[int(i) for i 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(x,parent,add):
  value[x] += add
  for i in graph[x]:
    if i == parent:
      continue
    dfs(i,x,value[x])

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

Submission status: Runtime Error

Solution code:
import sys


def dfs(i,p,edges,num):
    
    for j in edges[i]:
        if(j!=p):
            
            num[j] += num[i]
            
            dfs(j,i,edges,num)
    

N,Q = map(int,input().split())
sys.setrecursionlimit(N**2)

a=[]
b=[]

p=[]
x=[]

#隣接リスト
edges = [ [] for i in range(N)]
num = [0]*N


for i in range(N-1):
    a0,b0 = map(int,input().split())
    a.append(a0)
    b.append(b0)
    
    edges[a0-1].append(b0-1)
    edges[b0-1].append(a0-1)
    
for i in range(Q):
    p0,x0 = map(int,input().split())
    p.append(p0)
    x.append(x0)
    num[p0-1] += x0
    
dfs(0,-1, edges, num)
    
for i in range(N):
    print(num[i], end=" ")

Submission status: Runtime Error

Solution code:
def dfs(i,p,edges,num):
    
    for j in edges[i]:
        if(j!=p):
            
            num[j] += num[i]
            
            dfs(j,i,edges,num)
    

N,Q = map(int,input().split())
sys.setrecursionlimit(N**2)

a=[]
b=[]

p=[]
x=[]

#隣接リスト
edges = [ [] for i in range(N)]
num = [0]*N


for i in range(N-1):
    a0,b0 = map(int,input().split())
    a.append(a0)
    b.append(b0)
    
    edges[a0-1].append(b0-1)
    edges[b0-1].append(a0-1)
    
for i in range(Q):
    p0,x0 = map(int,input().split())
    p.append(p0)
    x.append(x0)
    num[p0-1] += x0
    
dfs(0,-1, edges, num)
    
for i in range(N):
    print(num[i], end=" ")

Submission status: Runtime Error

Solution code:
def dfs(i,p,edges,num):
    
    for j in edges[i]:
        if(j!=p):
            
            num[j] += num[i]
            
            dfs(j,i,edges,num)
    

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


a=[]
b=[]

p=[]
x=[]

#隣接リスト
edges = [ [] for i in range(N)]
num = [0]*N


for i in range(N-1):
    a0,b0 = map(int,input().split())
    a.append(a0)
    b.append(b0)
    
    edges[a0-1].append(b0-1)
    edges[b0-1].append(a0-1)
    
for i in range(Q):
    p0,x0 = map(int,input().split())
    p.append(p0)
    x.append(x0)
    num[p0-1] += x0
    
dfs(0,-1, edges, num)
    
for i in range(N):
    print(num[i], end=" ")

Submission status: Runtime Error

Solution code:
def dfs(i,p):
    
    for j in edges[i]:
        if(j!=p):
            
            num[j] += num[i]
            
            dfs(j,i)
    

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


a=[]
b=[]

p=[]
x=[]

#隣接リスト
global edges = [ [] for i in range(N)]
global num = [0]*N


for i in range(N-1):
    a0,b0 = map(int,input().split())
    a.append(a0)
    b.append(b0)
    
    edges[a0-1].append(b0-1)
    edges[b0-1].append(a0-1)
    
for i in range(Q):
    p0,x0 = map(int,input().split())
    p.append(p0)
    x.append(x0)
    num[p0-1] += x0
    
dfs(0,-1)
    
for i in range(N):
    print(num[i], end=" ")

Submission status: Runtime Error

Solution code:
def dfs(i,p):
    
    for j in edges[i]:
        if(j!=p):
            
            num[j] += num[i]
            
            dfs(j,i)
    

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


a=[]
b=[]

p=[]
x=[]

#隣接リスト
edges = [ [] for i in range(N)]
num = [0]*N


for i in range(N-1):
    a0,b0 = map(int,input().split())
    a.append(a0)
    b.append(b0)
    
    edges[a0-1].append(b0-1)
    edges[b0-1].append(a0-1)
    
for i in range(Q):
    p0,x0 = map(int,input().split())
    p.append(p0)
    x.append(x0)
    num[p0-1] += x0
    
dfs(0,-1)
    
for i in range(N):
    print(num[i], end=" ")

Submission status: Runtime Error

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

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

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

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

checked = [False] * n
checked[0] = True


def function(now, count):
    ans[now] += count
    for i in tree[now]:
        if checked[i] == False:
            checked[i] = True
            function(i, ans[now])

function(0, 0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

add = [0]*N
for _ in [0]*M:
    p, x = map(int, input().split())
    p -= 1
    add[p] += x
counter = [0]*N

def dfs(v, p, x):
    counter[v] = x
    for u in G[v]:
        if u == p:
            continue
        dfs(u, v, x+add[u])


dfs(0, -1, add[0])

print(*counter)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
tree=[list(map(int,input().split())) for i in range(n-1)]
point=[list(map(int,input().split())) for i in range(q)]

ans=['?']+[0]*n

def dfs(x,y):
  ans[x]+=y
  for i in range(n-1):
    if tree[i][0]==x:
      dfs(tree[i][1],y)

for i,j in point:
  dfs(i,j)

ans2=''
for i in range(1,n+1):
  ans2+=str(ans[i])
  if i!=n:
    ans2+=' '

print(ans2)

Submission status: Runtime Error

Solution code:
i = list(map(int,input().split()))
N = i[0]
Q = i[1]
tree = [[] for _ in range(N)]

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

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

ans = [0]*N

def dfs(u, parent=None):
    ans[u] = ans[parent]+X[u]
    for v in tree[u]:
        if v != parent:
            dfs(v,u)

dfs(0,0)
print(' '.join([str(i) for i in ans]))

Submission status: Runtime Error

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

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

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

ans = [0]*N

def dfs(u, parent=None):
    ans[u] = ans[parent]+X[u]
    for v in tree[u]:
        if v != parent:
            dfs(v,u)

dfs(0,0)
print(' '.join([str(i) for i in ans]))

Submission status: Runtime Error

Solution code:
def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    V = dict()
    for a, b in AB:
        V[b] = a

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

    Ans = [0] * n
    Ans[0] = S[1]
    for i in range(1, n):
        Ans[i] += S[i + 1] + Ans[V[i + 1] - 1]

    print(*Ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
graph = [[] for _ 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) 

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

ans = [0]*N

def dfs(u, parent):
    ans[u] = ans[parent] + X[u]
    for v in graph[u]:
        if v != parent:
            dfs(v, u)
dfs(0, 0)
print(*ans)

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
G=[list(map(int,input().split())) for _ in range(N-1)]
P=[list(map(int,input().split())) for _ in range(Q)]

gr=[[]]*N
for i in G:
    v=i[0]
    gr[v-1]=[*gr[v-1],i[1]]

cnt=[0]*N

def dfs(p,x):
    cnt[p-1]+=x
    for n in gr[p-1]:
        dfs(n,x)
    return

for p,x in P:
    dfs(p,x)
print(' '.join(map(str,cnt)))

Submission status: Runtime Error

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
import sys

sys.setrecursionlimit(50000)

n,q = nm()

tval = [0]*n
t = [[] for _ in range(n)]

for i in range(n-1):
    a = nl()
    aa, bb = sorted(a)
    t[aa-1].append(bb-1)

for i in range(q):
    p,x = nm()
    tval[p-1] += x

ans = [0]*n
def dfs(num,val):
    now = val+tval[num]
    ans[num] = now
    for nn in t[num]:
        dfs(nn,now)

dfs(0,0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
import sys

sys.setrecursionlimit(50000)

n,q = nm()

tval = [0]*n
t = [[] for _ in range(n)]

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

for i in range(q):
    p,x = nm()
    tval[p-1] += x

ans = [0]*n
def dfs(num,val):
    now = val+tval[num]
    ans[num] = now
    for nn in t[num]:
        dfs(nn,now)

dfs(0,0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,q = nm()

tval = [0]*n
t = [[] for _ in range(n)]

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

for i in range(q):
    p,x = nm()
    tval[p-1] += x

ans = [0]*n
def dfs(num,val):
    now = val+tval[num]
    ans[num] = now
    for nn in t[num]:
        dfs(nn,now)

dfs(0,0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
Graph = [[] for i in range(n)]
seen = [False]*n
point = [0]*n


def dfs(g, v, p):
    # vを訪問済みにする
    seen[v] = True
    if p != -1: point[v] += point[p]

    # vから行ける各頂点について
    for next_v in g[v]:

        # next_vが訪問済みだったらスルー
        if seen[next_v]: continue

        # 再帰的に探索
        dfs(g, next_v, v)


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

    # 無向グラフを想定
    Graph[a].append(b)
    Graph[b].append(a)


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


dfs(Graph, 0, -1)


print(*point)

Submission status: Runtime Error

Solution code:
# 頂点数と変数
n, q = map(int, input().split())
Graph = [[] for i in range(n+1)]
seen = [False]*(n+1)
point = [0]*(n+1)


def dfs(g, v, x):

    # vを訪問済みにする
    seen[v] = True
    point[v] += x

    # vから行ける各頂点について
    for next_v in g[v]:

        # next_vが訪問済みだったらスルー
        if seen[next_v]: continue
        if next_v < v: continue

        # 再帰的に探索
        dfs(g, next_v, x)


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

    # 無向グラフを想定
    Graph[a].append(b)
    Graph[b].append(a)

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


for i in point:
    if i == 0: continue
    print(i)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N , Q = map(int,input().split())
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
#print(graph)
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1
    point[a] += b
# dfsを用いて累積和を計算する
# 初期状態だと前の値がないためデフォルト引数に-1を代入
def dfs(now , prev = -1):
    for next in graph[now]:
        # 次のノードが前に参照した値の時はcontinue
        if next == prev:
            continue
        # 現在の値を次のポイントに加算することで累積和をとる
        point[next] += point[now]
        # 次のノードと現在のノードを引数にdfsを継続する
        dfs(next , now)
 
dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
N , Q = map(int,input().split())
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
#print(graph)
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1
    point[a] += b
# dfsを用いて累積和を計算する
# 初期状態だと前の値がないためデフォルト引数に-1を代入
def dfs(now , prev = -1):
    for next in graph[now]:
        # 次のノードが前に参照した値の時はcontinue
        if next == prev:
            continue
        # 現在の値を次のポイントに加算することで累積和をとる
        point[next] += point[now]
        # 次のノードと現在のノードを引数にdfsを継続する
        dfs(next , now)
 
dfs(0)
print(*point)

Submission status: Runtime Error

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

root = [[] for _ in range(N + 1)]
result = [0] * (N + 1)

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

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


def dfs(now, prev):
    for next in root[now]:
        if next == prev:
            continue
        result[next] += result[now]
        dfs(next, now)


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

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

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

for i in range(N - 1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    result[p] += x


def dfs(now, prev):
    for next in root[now]:
        if next == prev:
            continue
        result[next] += result[now]
        dfs(next, now)


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

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

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

for i in range(N - 1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    result[p] += x


def dfs(now, prev):
    for next in root[now]:
        if next == prev:
            continue
        else:
            result[next] += result[now]
            dfs(next, now)


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

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

root = [[] for _ in range(N + 1)]
result = [0] * (N + 1)

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] += x


def dfs(now, prev):
    for next in root[now]:
        if next == prev:
            continue
        else:
            result[next] += result[now]
            dfs(next, now)


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

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

root = [[] for _ in range(N + 1)]
result = [0] * (N + 1)

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(now, prev):
    for next in root[now]:
        if next == prev:
            continue
        else:
            result[next] = result[next] + result[now]
            dfs(next, now)


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

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

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

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(now, prev):
    if len(root[now]) == 0:
        return
    else:
        for i in range(len(root[now])):
            next_point = root[now][i]
            if next_point == prev:
                continue
            else:
                result[next_point] = result[next_point] + result[now]
                dfs(next_point, now)


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

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

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

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(num):
    if len(root[num]) == 0:
        return
    else:
        for i in range(len(root[num])):
            next_point = root[num][i]
            result[next_point] = result[next_point] + result[num]
            root[next_point].remove(num)
            dfs(next_point)


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

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

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

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(num):
    next_arr = root[num]
    score = result[num]
    if len(next_arr) == 0:
        return
    else:
        for i in range(len(next_arr)):
            next_point = next_arr[i]
            result[next_point] = result[next_point] + score
            root[next_point].remove(num)
            dfs(next_point)


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

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
#define rep(i, n) for (int i=0; i < n; i++)
typedef long long ll;
#define repran(i, a,b) for (int i = a; i<b;i++)
#define repd(i, n) for (int i = n-1; i > -1; i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using LP = pair<ll,ll>;
using P = pair<int,ll>;
int n;
ll inf = 1e18;

int main()
{
    int n ,q;
    cin >> n >> q;
    vector<vector<int>> G(n, vector<int>());
    rep(i, n-1){
        int a, b;
        cin >> a >> b;
        --a;--b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<ll> ans(n, 0);
    vector<ll> acc(n, 0);
    rep(i, q){
        int p; ll x;
        cin >> p >>x;
        --p;
        acc[p] += x;
    }
    //dfs
    deque<P> deq;
    deq.emplace_back(0, acc[0]);
    ans[0] = acc[0];
    vector<int> parent(n, -1);
    while(!deq.empty()){
        P now = deq.back();deq.pop_back();
        int node = now.first;
        ll cost = now.second;
        for (int child : G[node]){
            if(child == parent[node]) continue;
            parent[child] = node;
            ans[child] = ans[node]+acc[child];
            deq.emplace_back(child, acc[child]);
        }

    }
    rep(i, n) cout << ans[i] << endl;

}

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

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

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)
    root[b].append(a)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(num):
    next_arr = root[num]
    if len(next_arr) == 0:
        return
    else:
        for i in range(len(next_arr)):
            next_point = next_arr[i]
            score = result[num]
            result[next_point] = result[next_point] + score
            root[next_point].remove(num)
            dfs(next_point)


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

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

ab.sort()
px.sort()

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

for i in range(N - 1):
    a = ab[i][0]
    b = ab[i][1]
    root[a].append(b)

for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    result[p] = result[p] + x


def dfs(num):
    for i in range(len(root[num])):
        next = root[num][i]
        score = result[num]
        result[next] = result[next] + result[num]
        dfs(next)


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

Submission status: Runtime Error

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

v = [[] for i in range(N)]  #もともとの木構造
for i in range(N-1):
  a, b = map(int, input().split())
  #vのindexが頂点番号、各indexの要素が繋がっている要素を表す
  v[a-1].append(b-1) #頂点idが1から始まっているため0にずらす
  v[b-1].append(a-1)

f = [0] * N #足し算するもの
for i in range(Q):
  p, x = map(int, input().split())
  f[p-1] += x  #それぞれの頂点に足すべき項目をあらかじめ追加して整理

#print(v)
#print(f)

#再帰関数dfsの定義:終了条件はd[y]==0となったら
def dfs(x, d, s): # x:計算する頂点, d:計算了否配列(まだ1完了0), s:頂点xの値
  for y in v[x]:  #頂点xとつながる各頂点yの計算
    if d[y]:      #計算される頂点yが計算未了の場合
      s += f[y]   #頂点xの値sにf[y](頂点yの値)を足す
      ans[y] = s  #頂点yの値をsに確定
      d[y] = 0    #計算済に変更
      dfs(y,d,s)  #頂点yの下層についても再帰的に計算
      s -= f[y]   #頂点yの下層の計算が終わったのでsを頂点xの値に戻す

ans = [0] * N
d = [1] * N #全部未計算にセット
d[0] = 0 #0が"根" 0は計算済(※f[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())

v = [[] for i in range(N)]  #もともとの木構造
for i in range(N-1):
  a, b = map(int, input().split())
  #vのindexが頂点番号、各indexの要素が繋がっている要素を表す
  v[a-1].append(b-1) #頂点idが1から始まっているため0にずらす
  v[b-1].append(a-1)

f = [0] * N #足し算するもの
for i in range(Q):
  p, x = map(int, input().split())
  f[p-1] += x  #それぞれの頂点に足すべき項目をあらかじめ追加して整理

print(v)
print(f)

#再帰関数dfsの定義:終了条件はd[y]==0となったら
def dfs(x, d, s): # x:計算する頂点, d:計算了否配列(まだ1完了0), s:頂点xの値
  for y in v[x]:  #頂点xとつながる各頂点yの計算
    if d[y]:      #計算される頂点yが計算未了の場合
      s += f[y]   #頂点xの値sにf[y](頂点yの値)を足す
      ans[y] = s  #頂点yの値をsに確定
      d[y] = 0    #計算済に変更
      dfs(y,d,s)  #頂点yの下層についても再帰的に計算
      s -= f[y]   #頂点yの下層の計算が終わったのでsを頂点xの値に戻す

ans = [0] * N
d = [1] * N #全部未計算にセット
d[0] = 0 #0が"根" 0は計算済(※f[0]が答えだから)
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
print(*ans) #*と配列でスペース区切りで表示

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N , Q = map(int,input().split())
graph = [[] for _ in range(N)]
point = [0] * N
for _ in range(N - 1):
    a , b = map(int,input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
#print(graph)
for _ in range(Q):
    a , b = map(int,input().split())
    a = a - 1
    point[a] += b
# dfsを用いて累積和を計算する
# 初期状態だと前の値がないためデフォルト引数に-1を代入
def dfs(now , prev = -1):
    for next in graph[now]:
        # 次のノードが前に参照した値の時はcontinue
        if next == prev:
            continue
        # 現在の値を次のポイントに加算することで累積和をとる
        point[next] += point[now]
        # 次のノードと現在のノードを引数にdfsを継続する
        dfs(next , now)

dfs(0)
print(*point)

Submission status: Runtime Error

Solution code:
def tuika(newNode,pea):
    nodes[pea].append(newNode)
    if pea != 1:
        tuika(newNode, nodes[pea][0])

n, q = map(int, input().split())
nodes = [[] for _ in range(n+1)]
nodes[1].append(0)
counts = [0 for _ in range(n+1)]

for i in range(n-1):
    pea, chi = map(int, input().split())
    nodes[chi].append(pea)
    tuika(chi, pea)

for i in range(q):
    p, q = map(int, input().split())
    if len(nodes[p]) != 1:
        for j in range(1,len(nodes[p])):
            counts[nodes[p][j]] += q
    counts[p] += q

str_counts = map(str, counts[1:])
print(' '.join(str_counts))

Submission status: Runtime Error

Solution code:
def tuika(newNode,pea):
    nodes[pea].append(newNode)
    if pea != 1:
        tuika(newNode, nodes[pea][0])

n, q = map(int, input().split())
nodes = [[] for _ in range(n+1)]
nodes[1].append(0)
counts = [0 for _ in range(n+1)]

for i in range(n-1):
    pea, chi = map(int, input().split())
    nodes[chi].append(pea)
    tuika(chi, pea)

for i in range(q):
    p, q = map(int, input().split())
    for j in range(1,len(nodes[p])):
        counts[nodes[p][j]] += q
    counts[p] += q

#print(counts[1:])
str_counts = map(str, counts[1:])
print(' '.join(str_counts))

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self):
        self.num = None
        self.parent = None
        self.children = list()
        self.added = 0
        self.count = None
    def __repr__(self):
        return f'Node({self.num})'
    def calc_count(self):
        if self.num==1:
            self.count = self.added
            return
        if self.parent.count is None:
            self.parent.calc_count()
        self.count = self.parent.count + self.added

(n,q),*abpx = [list(map(int, s.split())) for s in open(0)]

ab,px = abpx[:n-1],abpx[n-1:]

from collections import defaultdict,deque
d = defaultdict(Node)

for a,b in ab:
    d[a].num = a
    d[b].num = b
    d[b].parent = d[a]
    d[a].children.append(d[b])

for p,x in px:
    d[p].added += x

dq = deque([d[1]])

while dq:
    cur = dq.popleft()
    if cur.children:
        dq.extend(cur.children)
    cur.calc_count()

print(*list(d[i].count for i in range(1,n+1)))

Submission status: Runtime Error

Solution code:
def dfs(G, v):
    seen[v] = True

    for nv in G[v]:
        if seen[nv]:
            continue
        point[nv] += point[v]
        dfs(G, nv)

n, q = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(n-1):
    x, y =  map(int, input().split())
    x, y = x-1, y-1
    G[x].append(y)
    G[y].append(x)

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

dfs(G, root)
print(*point)

Submission status: Runtime Error

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

N,Q = (int(x) for x in input().rstrip('\n').split())
d = {1:0}
rs = []
for _ in range(N-1):
  a,b = (int(x) for x in input().rstrip('\n').split())
  rs.append([a,b])
  if a in d:
    d[b] = a
  else:
    d[a] = b
res = [0]*(N)
for _ in range(Q):
  p,s = (int(x) for x in input().rstrip('\n').split())
  used = {p}
  ad = [x for x in rs if p in x]
  if p > 1:
    root = d[p]
    ad = [x for x in ad if root not in x]
  for i in range(N):
      hit = set(x for x in range(1,N+1) if any(x in y for y in ad))
      nu = hit - used
      if len(nu)==0:
        break
      else:
        ad = [x for x in rs if any(y in x for y in nu)]
        used = used | nu
  for x in used:
    res[x-1] += s
print(*res)

Submission status: Runtime Error

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

N,Q = (int(x) for x in input().rstrip('\n').split())
d = {1:0}
rs = set()
for _ in range(N-1):
  a,b = (int(x) for x in input().rstrip('\n').split())
  rs.add((a,b))
  if a in d:
    d[b] = a
  else:
    d[a] = b
res = [0]*(N)
for _ in range(Q):
  p,s = (int(x) for x in input().rstrip('\n').split())
  root = d[p]
  used = {p}
  ad = [x for x in rs if p in x]
  if root >0:
    if (p,root) in ad:
      ad.remove((p,root))
    else:
      ad.remove((root,p))
  for i in range(N):
      hit = set(x for x in range(1,N+1) if any(x in y for y in ad))
      nu = hit - used
      if len(nu)==0:
        break
      else:
        ad = [x for x in rs if any(y in x for y in nu)]
        used = used | nu
  for x in used:
    res[x-1] += s
print(*res)

Submission status: Runtime Error

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

N,Q = (int(x) for x in input().rstrip('\n').split())
d = {1:0}
rs = set()
for _ in range(N-1):
  a,b = (int(x) for x in input().rstrip('\n').split())
  rs.add((a,b))
  if a in d:
    d[b] = a
  else:
    d[a] = b
res = [0]*(N)
for _ in range(Q):
  p,s = (int(x) for x in input().rstrip('\n').split())
  root = d[p]
  used = {p}
  ad = [x for x in rs if p in x]
  if root >0:
    if (p,root) in ad:
      ad.remove((p,root))
    else:
      ad.remove((root,p))
  for i in range(N):
      hit = set(x for x in range(1,N+1) if any(x in y for y in ad))
      nu = hit - used
      if len(nu)==0:
        break
      else:
        ad = [x for x in rs if any(y in x for y in nu)]
        used = used | nu
  for x in used:
    res[x-1] += s
print(res)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2*10**5)

def readinput():
    n,q=map(int,input().split())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for i in range(n-1):
        a,b=map(int,input().split())
        nList[a].append(b)
        nList[b].append(a)
    px=[]
    for i in range(q):
        p,x=map(int,input().split())
        px.append((p,x))
    return n,q,nList,px

childList=[]
def setDepth(s,nList):
    global depth
    global color
    global childList

    for next in nList[s]:
        if color[next]==WHITE:
            childList[s].append(next)
            depth[next]=depth[s]+1
            color[next]=GRAY
            childList[s].extend(setDepth(next,nList))
    color[s]=BLACK
    return childList[s]

def addCounter(s,x,nList):
    global depth
    global counter

    for next in nList[s]:
        if depth[next]>depth[s]:
            counter[next]+=x
            addCounter(next,x,nList)
    

WHITE=0
GRAY=1
BLACK=2
color=[]
depth=[]
counter=[]
def main(n,q,nList,px):
    global depth
    global color
    global counter
    global childList

    color=[WHITE]*(n+1)
    depth=[0]*(n+1)
    counter=[0]*(n+1)
    for _ in range(n+1):
        childList.append([])

    #print(nList)

    depth[1]=0
    color[1]=GRAY
    setDepth(1,nList)
    #print(depth)
    #print(childList)

    for p,x in px:
        # counter[p]+=x
        # addCounter(p,x,nList)
        counter[p]+=x
        for c in childList[p]:
            counter[c]+=x

    return counter[1:]

if __name__=='__main__':
    n,q,nList,px=readinput()
    ans=main(n,q,nList,px)
    print(' '.join(map(str,ans)))