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
sys.setrecursionlimit(100000)


def rootedTree(node,parent,root,point):
    B.append([node,parent,root,point])
    for i in root:
        rootedTree(i,node,tree[i],point+C[i])


n,q=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n-1)]
B=[]
C=[0 for j in range(n+1)]
for i in range(q):
    add,point=map(int,input().split())
    C[add] +=point

tree=[[] for i in range(n+1)]
for i,j in A:tree[i] +=[j]

rootedTree(1,-1,tree[1],C[1])
ans=[0]*n
for i in B:
    ans[i[0]-1]=i[3]
print(*ans)

Submission status: Runtime Error

Solution code:
def Constrcuted(A,C,n):
    tree=[[] for i in range(n+1)]
    for i,j in A:
        tree[i] +=[j]
    top=[k+1 for k in range(n)]
    top=top[0]
    rootedTree(top,-1,tree[top],tree,C[top])


def rootedTree(node,parent,root,tree,point):
    B.append([node,parent,tree[node],point])
    for i in root:
        rootedTree(i,node,tree[i],tree,point+C[i])


n,q=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n-1)]
B=[]
C=[0 for i in range(n+1)]
for i in range(q):
    add,point=map(int,input().split())
    C[add] +=point

Constrcuted(A,C,n)
ans=[0]*n
for i in B:
    ans[i[0]-1]=i[3]
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6) 
input = sys.stdin.readline 
N,Q= map(int, input().split())
es=[]
ope=[]
for _ in range(N-1):
    a,b=map(int, input().split())
    es.append((a,b))
for _ in range(Q):
    p,x=map(int, input().split())
    ope.append((p,x))
point=[0]*N
import numpy as np
point=np.array(point)
nodes=np.eye(N, dtype=int)
for e in es:
    if sum(nodes[e[0]-1])>=sum(nodes[e[1]-1]):
        nodes[e[0]-1][e[1]-1]=1
        nodes[:,e[1]-1]=np.logical_or(nodes[:,e[1]-1],nodes[:,e[0]-1])
    else:
        nodes[e[1]-1][e[0]-1]=1
        nodes[:,e[0]-1]=np.logical_or(nodes[:,e[1]-1],nodes[:,e[0]-1])
for o in ope:
    point+=nodes[o[0]-1]*o[1]
string=""
for i in range(N):
    string+=str(int(point[i]))+" "
print(string)

Submission status: Runtime Error

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

### Scoreの初期化( 今書くなら[0]*N+1 でよい)
scores = [0 for _ in range(N + 1)]
### それへのScore代入
for _ in range(Q):
    tag, p = map(int, input().split())
    scores[tag] += p
###


# k -> 訪問先 bef -> 前回の訪問先
def dfs(k=1,bef=-1):
    global scores
    global fins
    kids = connections[k]
    # グラフの端っこかつ唯一の接続先が前回の訪問先なら終了
    if len(kids)==1 and bef==kids[0]:
        return
    for kid in kids:
        # print(f'k:{k} kids:{kids}')
        if bef==kid:
            continue
        scores[kid]+=scores[k]
        dfs(kid,k)


dfs()



print(*scores[1:])

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for _ in range(1,N)]

query_list = [input().split() for _ in range(Q)]
query_list_int = [[int(k) for k in i]  for i in query_list]

val_list = [0 for _ in range(N)]
linkde_node_list = [[] for _ in range(N)]

#無向グラフのリンクを作成
for a, b in tree_list:
    linkde_node_list[int(a) -1].append(int(b) - 1)
    # linkde_node_list[int(b) -1].append(int(a) - 1)


for index, val in query_list_int:
    val_list[index-1] += val

def dfs(node_index, pairent_index):
    for child in linkde_node_list[node_index]:
        if child == pairent_index:
            continue

        val_list[child] += val_list[node_index]
        dfs(child, node_index)

    return val_list

res = dfs(0, -1) 
#0はルート、-1は親を持っていない事

print(*res)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input().split() for _ in range(1,N)]

query_list = [input().split() for _ in range(Q)]
query_list_int = [[int(k) for k in i]  for i in query_list]

val_list = [0 for _ in range(N)]
linkde_node_list = [[] for _ in range(N)]

# 無向グラフのリンクを作成
for a, b in tree_list:
    linkde_node_list[int(a) -1].append(int(b) - 1)
    linkde_node_list[int(b) -1].append(int(a) - 1)


for index, val in query_list_int:
    val_list[index-1] += val

def dfs(node_index, pairent_index):
    for child in linkde_node_list[node_index]:

        if child == pairent_index:
            continue

        val_list[child] += val_list[node_index]
        dfs(child, node_index)

    return val_list

res = dfs(0, -1) #0はルート、-1は親を持っていない事

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

val_list = [0 for _ in range(N)]
node_index = [[] for _ in range(N)]
cumulated_sum_list = [0] * N


for a, b in tree_list_int:
    node_index[a -1].append(b - 1)
    node_index[b -1].append(a - 1)

for index, val in query_list_int:
    val_list[index-1] += val

def dfs(child_index, pairent_index):
    for child in node_index[child_index]:

        if child == pairent_index:
            continue

        val_list[child] += val_list[child_index]
        dfs(child, child_index)

    return val_list

res = dfs(0, -1)

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

val_list = [0 for _ in range(N)]
node_index = [[] for _ in range(N)]
cumulated_sum_list = [0] * N

# make graph(relation)
for a, b in tree_list_int:
    node_index[a -1].append(b - 1) #親位置に子のindexを追加
    node_index[b -1].append(a - 1) #子位置に親のindexを追加

for index, val in query_list_int:
    val_list[index-1] += val #加算する値を親位置へ記載

def dfs(child_index, pairent_index):
    for child in node_index[child_index]: #とあるノード（再帰していく）の子ノードを線形探索

        if child == pairent_index: #子ノードが親ノードでないとき
            continue

        val_list[child] += val_list[child_index] #累積和の計算
        dfs(child, child_index) #再帰する。子にノード子を挿入。今回の子を親に

    return val_list

res = dfs(0, -1) #0はルート、-1は親を持っていない事

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

val_list = [0 for _ in range(N)]
node_index = [[] for _ in range(N)]
cumulated_sum_list = [0] * N

# make graph(relation)
for a, b in tree_list_int:
    node_index[a -1].append(b - 1) #親位置に子のindexを追加
    node_index[b -1].append(a - 1) #子位置に親のindexを追加

for index, val in query_list_int:
    val_list[index-1] += val #加算する値を親位置へ記載

def dfs(child_index, pairent_index):
    for child in node_index[child_index]: #とあるノード（再帰していく）の子ノードを線形探索

        if child != pairent_index: #子ノードが親ノードでないとき
            val_list[child] += val_list[child_index] #累積和の計算
            dfs(child, child_index) #再帰する。子にノード子を挿入。今回の子を親に

    return val_list

res = dfs(0, -1) #0はルート、-1は親を持っていない事

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [[int(k) for k in i.split()] for i in tree_list]
query_list = [input() for _ in range(Q)]
query_list_int = [[int(k) for k in i.split()]  for i in query_list]

val_list = [0 for _ in range(N)]
node_index = [[] for _ in range(N)]
cumulated_sum_list = [0] * N

# make graph(relation)
for a, b in tree_list_int:
    node_index[a -1].append(b - 1) #親位置に子のindexを追加
    node_index[b -1].append(a - 1)#子位置に親のindexを追加

for index, val in query_list_int:
    val_list[index-1] += val #加算する値を親位置へ記載

def dfs(child_index, pairent_index):
    for child in node_index[child_index]: #とあるノード（再帰していく）の子ノードを線形探索

        if child != pairent_index: #子ノードが親ノードでないとき
            val_list[child] += val_list[child_index] #累積和の計算
            dfs(child, child_index) #再帰する。子にノード子を挿入。今回の子を親に

    return val_list

res = dfs(0, -1) #0はルート、-1は親を持っていない事

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [list(map(int,i.split())) for i in tree_list]
action_list = [input() for _ in range(Q)]
action_list_int = [list(map(int, i.split())) for i in action_list]

class Node:
    def __init__(self, data):
        self.data = data
        self.cnt = 0
        self.child = []


class tree:
    def __init__(self, N, tree_list_int):
        self.node_list = [Node(i) for i in range(1,N+1)]

        for i, j in tree_list_int:
            self.node_list[i-1].child.append(self.node_list[j-1])


    def adder(self, node, val):
        node.cnt += val

        if not node.child:
            return
        else:
            for next_child in node.child:
                self.adder(next_child, val)

                

        
ins=tree(N, tree_list_int)

for pos, val in action_list_int:
    ins.adder(ins.node_list[pos-1], val)

res = str(ins.node_list[0].cnt)
for i in range(1, N):
    res += ' ' + str(ins.node_list[i].cnt)

print(res)

Submission status: Runtime Error

Solution code:
def add1(x,p):
    Result[x] += p
    for c in Data[x]:
        add1(c,p)

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

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

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

Submission status: Runtime Error

Solution code:
N, Q = [int(item) for item in input().split()]

tree_list = [input() for _ in range(1,N)]
tree_list_int = [list(map(int,i.split())) for i in tree_list]
action_list = [input() for _ in range(Q)]
action_list_int = [list(map(int, i.split())) for i in action_list]

class Node:
    def __init__(self, data):
        self.data = data
        self.cnt = 0
        self.child = []


class tree:
    def __init__(self, N, tree_list_int):
        self.node_list = [Node(i) for i in range(1,N+1)]

        for i, j in tree_list_int:
            self.node_list[i-1].child.append(self.node_list[j-1])


    def adder(self, node, val):
        node.cnt += val

        if not node.child:
            return node.cnt

        for next_child in node.child:
            self.adder(next_child, val)

                

        
ins=tree(N, tree_list_int)

for pos, val in action_list_int:
    ins.adder(ins.node_list[pos-1], val)

res = str(ins.node_list[0].cnt)
for i in range(1, N):
    res += ' ' + str(ins.node_list[i].cnt)

print(res)

Submission status: Runtime Error

Solution code:

def add_value(p, x):
    counters[p] += x
    if len(parents[p]) > 0:
        for c in parents[p]:
            add_value(c, x)

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

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

for i in range(q):
    p,x = map(int, input().split())
    p -= 1
    add_value(p, x)

ans = " ".join(list(map(str, counters)))
print(ans)

Submission status: Runtime Error

Solution code:

def add_value(p, x):
    counters[p] += x
    if len(parents[p]) > 0:
        for c in parents[p]:
            add_value(c, x)

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

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

for i in range(q):
    p,x = map(int, input().split())
    p -= 1
    add_value(p, x)

ans = " ".join(counters)
print(ans)

Submission status: Runtime Error

Solution code:

def add_value(p, x):
    counters[p] += x
    if len(parents[p]) > 0:
        for c in parents[p]:
            add_value(c, x)

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

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

for i in range(q):
    p,x = map(int, input().split())
    p -= 1
    add_value(p, x)

print(counters)

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split()))
adjacent_list = [[] for i in range(n+1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)
tree_value = [0]*(n+1)
for i in range(q):
    p, x = map(int, input().split())
    tree_value[p] += x
searched = set()
def dfs(node, count):
    searched.add(node)
    tree_value[node] += count
    for i in adjacent_list[node]:
        if i not in searched:
            dfs(i, tree_value[node])
dfs(1,0)
for i in range(1,n+1):
    print(tree_value[i])

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split()))
adjacent_list = [[] for i in range(n+1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)
tree_value = [0]*(n+1)
for i in range(q):
    p, x = map(int, input().split())
    tree_value[p] += x
searched = set()
def dfs(node, count):
    searched.add(node)
    tree_value[node] += count
    for i in adjacent_list[node]:
        if i not in searched:
            dfs(i, tree_value[node])
dfs(1,0)
for i in range(1,n):
    print(tree_value[i])

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())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

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

ans = [0]*n

def dfs(v,p,value):
    value += add[v]
    ans[v] = value
    for c in g[v]:
        if c == p: continue
        dfs(c,v,value)

dfs(0,-1,0)

print(*ans)

Submission status: Runtime Error

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

g = [[] for _ in range(N)]

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

add = [0]*N

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

ans = [0]*N

def func(v, p, val):
    val += add[v]
    ans[v] = val
    for c in g[v]:
        if c==p: continue
        func(c, v, val)

func(0, -1, 0)

print(*ans)

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())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

add = [0]*N

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

ans = [0]*N

def func(v, p, val):
    val += add[v]
    ans[v] = val
    for c in g[v]:
        if c==p:
            continue
        func(c, v, val)

func(0, -1, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
l=list(map(int, input().split()))
N, Q =l[0], l[1]

parent = [0]*(N+1)
for ii in range(N-1):
    l=list(map(int, input().split()))
    parent[ l[1] ] = l[0]

counter = [0]*(N+1)
for ii in range(Q):
    l=list(map(int, input().split()))
    counter[ l[0] ] += l[1]

c = [0]*(N+1)
for index in range(1, Q+1):
    c[index] += counter[index]

for index in range(2, N+1):
    c[index] += c[ parent[index] ]

for index in range(1, N+1):
    if index < N:
        print( '%d ' % c[index], end='' )
    else:
        print(c[index])

Submission status: Runtime Error

Solution code:
l=list(map(int, input().split()))
N, Q =l[0], l[1]

parent = [0]*(N+1)
for ii in range(N-1):
    l=list(map(int, input().split()))
    parent[ l[1] ] = l[0]

counter = [0]*(Q+1)
for ii in range(Q):
    l=list(map(int, input().split()))
    counter[ l[0] ] += l[1]

c = [0]*(N+1)
for index in range(1, Q+1):
    c[index] += counter[index]

for index in range(2, N+1):
    c[index] += c[ parent[index] ]

for index in range(1, N+1):
    if index < N:
        print( '%d ' % c[index], end='' )
    else:
        print(c[index])

Submission status: Runtime Error

Solution code:
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)
ans = ' '.join(map(str,value[1:]))
print(ans)

Submission status: Runtime Error

Solution code:
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)
ans = ' '.join(map(str,value[1:]))
print(ans)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n-1)]
b=[list(map(int,input().split())) for i in range(q)]
c=[0]*(n-1)
for i in range(n-1):
  c[(a[i][1]-2)]=a[i][0]
d=[0]*n
for i in range(q):
  d[(b[i][0]-1)]+=b[i][1]
def ans(list_1,list_2,k):
  if k==0:
    return(list_2[0])
  return(list_2[k]+ans(list_1,list_2,(list_1[k-1]-1)))
for i in range(n):
  print(ans(c,d,i))

Submission status: Runtime Error

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

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

count = [0 for i in range(N + 1)]
visited = [False for i in range(N+1)]
def dfs(root, curCount):
    visited[root] = True
    curCount += tally[root]
    count[root] = curCount
    for neighbour in edges[root]:
        if visited[neighbour]: continue
        dfs(neighbour, curCount)

dfs(1, 0)

print(*count[1:])

Submission status: Runtime Error

Solution code:
tree = {}
ans = []
def resolve():
    N, Q = map(int, input().split())
    ans.extend([0 for _ in range(N)])

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

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

    dfs(0, ans[0])

    print(' '.join([str(x) for x in ans]))


def dfs(k, val, par=0):
    if k not in tree:
        return
    for c in tree.get(k, []):
        if c == par:
            continue
        ans[c] += val
        dfs(c, ans[c], k)
    return

if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

q = [1]
def dfs(q):
	if not q:
		return
	cp = q.pop()
	for np in E[cp]:
		cnt[np] += cnt[cp]
		q.append(np)

	dfs(q)
dfs(q)
cnt.pop(0)
for c in cnt:
	print(c, end = " ")
print()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
t = {}
tp = {1: 0}

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

#print(t)

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

#print(tp)

for i, ch in t.items():
    for next in ch:
        tp[next]=tp[next]+tp[i]

print(*tp.values(), sep=' ')

Submission status: Runtime Error

Solution code:
def main():
  N, Q = map(lambda i: int(i), input().split(' '))
  vertices = [[] for i in range(N)]
  points = [0] * N
  for n in range(N - 1):
    a, b = map(lambda i: int(i), input().split(' '))
    vertices[a - 1].append(b - 1)
  # print(vertices)
  for q in range(Q):
    p, x = map(lambda i: int(i), input().split(' '))
    add(points, vertices, p - 1, x)
  print(' '.join(list(map(lambda p: str(p), points))))

def add(points, vertices, i, x):
  points[i] += x
  if len(vertices[i]) > 0:
    for v in vertices[i]:
        add(points, vertices, v, x)

main()

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
P = [1]*(N+1)
for i in range(N):
    a,b = map(int,input().split())
    P[b] = a
C = [0]*(N+1)
for i in range(Q):
    p,x = map(int,input().split())
    C[p] = x
for i in range(2,N):
    q = P[i]
    C[i] += C[q]
C.pop(0)
print(*C)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(n+1)]
parent=[i for i in range(n+1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a].append(b)
  parent[b]=a
lcount=[0 for i in range(n+1)]
for i in range(q):
  p,x=map(int,input().split())
  lcount[p]+=x
ans1=[0 for i in range(n)]
def find(n):
  if parent[n]==n:
    return n
  else:
    return find(parent[n])
oya=find(1)
def dfs(n1,count):
  count+=lcount[n1]
  ans1[n1-1]=count
  if l[n1]==[]:
    return
  else:
    for i in l[n1]:
      num=count
      dfs(i,num)
    return
dfs(oya,0)
ans=[]
for i in range(n):
  ans.append(str(ans1[i]))
print(" ".join(ans))

Submission status: Runtime Error

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

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

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


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

ans=[0]*N

for i in range(N-1,-1,-1):
    tem=i
    while True:
        if tem==0:
            ans[tem]=c[0]
            break
        ans[i]+=c[R[tem][0]]
        tem=R[tem][0]


print(*ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

s = defaultdict(list)
for i in range(n-1):
    a,b = map(int,input().split())
    s[a] += [b]

t = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    t[p-1] += x
    
def re(sta,add):
    if len(s[sta]) == 0:
        return None
    else:
        for y in s[sta]:
            t[y-1] += add
            add1 = t[y-1]
            re(y,add1)
        return None

re(1,t[0])

print(" ".join(list(map(str,t))))

Submission status: Runtime Error

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

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

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

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

dfs(0)
print(*node)

Submission status: Runtime Error

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

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

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

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

dfs(0)
print(*node)

Submission status: Runtime Error

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

for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b); g[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    node[p] += x

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

dfs(0)
print(*node)

Submission status: Runtime Error

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

def dfs(v, p=-1):
    for u in g[v]:
        if u==p:
            continue
        node[u] += node[v]
        dfs(u, v)
dfs(0)
print(*node)

Submission status: Runtime Error

Solution code:
def main():
    n, q = map(int, input().split())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        g[a].append(b); g[b].append(a)
    node = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x

    def dfs(v, p=-1):
        for u in g[v]:
            if u==p:
                continue
            node[u] += node[v]
            dfs(u, v)
    dfs(0)
    print(*node)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, q = map(int, input().split())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        g[a].append(b); g[b].append(a)
    node = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        node[p] += x

    def dfs(v, p=-1):
        for u in g[node]:
            if u==p:
                continue
            node[u] += node[v]
            dfs(u, v)
    dfs(0)
    print(*node)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def dfs(c, t, node, p=-1):
    for child in t[node]:
        if child==p:
            continue
        c[child] += c[node]
        c, t = dfs(c, t, child, node)
    return c, t

def main():
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b); t[b].append(a)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x

    c, _ = dfs(c, t, 0)
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b); t[b].append(a)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x

    def dfs(node, p=-1):
        for child in t[node]:
            if child==p:
                continue
            c[child] += c[node]
            dfs(child, node)
    dfs(0)
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    from collections import deque
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b); t[b].append(a)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x
    dq = deque([0])

    def dfs(node, p=-1):
        for child in t[node]:
            if child==p:
                continue
            c[child] += c[node]
            dfs(child, node)
    dfs(0)
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    from collections import deque
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b); t[b].append(a)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x
    dq = deque([0])

    def dfs(node, p=-1):
        while t[node]:
            child = t[node].pop()
            if child==p:
                continue
            c[child] += c[node]
            dfs(child, node)
    dfs(0)
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

s = defaultdict(list)
for i in range(n-1):
    a,b = map(int,input().split())
    s[a] += [b]

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

def re(sta,add):
    if len(s[sta]) == 0:
        return None
    else:
        for y in s[sta]:
            t[y-1] += add
            add1 = t[y-1]
            re(y,add1)
        return None

re(1,t[0])

print(" ".join(list(map(str,t))))

Submission status: Runtime Error

Solution code:
n,q=list(map(int,input().split()))
a=[0]*(n-1)
b=[0]*(n-1)
x=[0]*n
p=[0]*n
lis=[0]*n
for i in range(n-1):
  a[i],b[i] = map(int,input().split())
for j in range(q):
  p[j],x[j] = map(int,input().split())
sub=[set()]*n
for i in range(n):
  sub[i]={i+1}

for i in range(n-1):
  for j in sub[b[i]-1]:
    sub[a[i]-1].add(j)
for i in range(n-1):
  for j in sub[b[i]-1]:
    sub[a[i]-1].add(j)

for j in range(q):
  for k in sub[p[j]-1]:
    lis[k-1] += x[j]

print(*lis)

Submission status: Runtime Error

Solution code:
n,q=list(map(int,input().split()))
a=[0]*(n-1)
b=[0]*(n-1)
x=[0]*n
p=[0]*n
lis=[0]*n
for i in range(n-1):
  a[i],b[i] = map(int,input().split())
for j in range(q):
  p[j],x[j] = map(int,input().split())
sub=[set()]*n
for i in range(n):
  sub[i].add(i+1)

for i in range(n-1):
  for j in sub[b[i]-1]:
    sub[a[i]-1].add(j)
for i in range(n-1):
  for j in sub[b[i]-1]:
    sub[a[i]-1].add(j)

for j in range(q):
  for k in sub[p[j]-1]:
    lis[k-1] += x[j]

print(*lis)