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:
class Node:

    def __init__(self, name):
        self.name = name
        self.parent = None
        self.progeny = None
        self.count = 0
        self.children = []
    
    def __str__(self):
        return "{}({}):{}".format(self.name, self.count, len(self.children))
        
    def set_parent(self, parent):
        self.parent = parent
        
    def add_child(self, child):
        self.children.append(child)
        child.set_parent(self)
        
    def num_child(self):
        return len(self.children)
    
    def add_count(self, count):
        self.count += count
    
    def get_progeny(self):
        if self.progeny==None:
            self.progeny = []
            self.progeny.extend(self.children)
            for child in self.children:
                self.progeny.extend(child.get_progeny())
        return self.progeny


def print_tree(node, tab):
    print("{}{}({})".format("\t"*tab, node.name, node.count))
    num = node.num_child()
    if num != 0:
        for i in range(num):
            print_tree(node.children[i], tab+1)

if __name__ == "__main__":
    n, q = map(int, input().split())
    nodes = [0] + [Node(i) for i in range(1,n+1)]
    for i in range(1,n):
        a, b = map(int, input().split())
        nodes[a].add_child(nodes[b])
    for _ in range(q):
        p, x = map(int, input().split())
        nodes[p].add_count(x)
        for node in nodes[p].get_progeny():
            node.add_count(x)
    print(" ".join([str(nodes[i].count) for i in range(1,n+1)]))

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):  
  for i in graph[v]:
    if i == parent:
      continue
    value[i] += value[v]
    dfs(i, v)

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

Submission status: Runtime Error

Solution code:

def dfs(start,graph,score):
    stack = []
    stack.append(start)
    while len(stack) != 0:
        next = int(stack.pop())
        graph[next-1].remove(next)
        for num in graph[next-1]:
            stack.append(num)
            score[num-1] += score[next-1]

nq = str.split(input())
n = int(nq[0])
q = int(nq[1])
graph = []
parent = []
score = []
start = 0
end = 0
for i in range(n):
    graph.append([])
    parent.append([])
    score.append(0)
for i in range(n-1):
    startEnd = str.split(input())
    start = int(startEnd[0])
    end = int(startEnd[1])
    graph[start-1].append(end)
for i in range(q):
    xAmount = str.split(input())
    x = int(xAmount[0])
    amount = int(xAmount[1])
    score[x-1] += amount
dfs(1,graph,score)
for num in score:
    print(str(num)+' ',end='')

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

# 隣接グラフ
to=[[] for i in range(n)] 
ans=[0 for i in range(n)]

# 深さ優先探索
def dfs(v, p=-1):
    global to,ans
    for u in to[v]:
        if u==p:
            continue
        ans[u]+=ans[v]
        dfs(u,v)

# 隣接グラフ作成
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)

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

dfs(0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
 
n,q = map(int,input().split())
 
a = [0] * (n-1)
b = [0] * (n-1)
info = [[] for _ in range(n)]
 
for i in range(n-1):
    a[i],b[i] = map(int,input().split())
    a[i] -= 1
    b[i] -= 1
    info[a[i]].append(b[i])
    info[b[i]].append(a[i])
 
p = [0] * q
x = [0] * q
 
 
for j in range(q):
    p[j],x[j] = map(int,input().split())
    p[j] -= 1
 
plus = [0] * n
 
for j in range(q):
    plus[p[j]] += x[j]
 
state = [0] * n
state[0] = 1
cur = [0]
 
child = [[] for _ in range(n)]
par = [0] * n
 
ranklist = [[1]]
 
#print(cur)
#print(info)
 
while cur != []:
    cur2 = []
    ranklist.append([])
    for obj in cur:
        for obj2 in info[obj]:
            if state[obj2] == 0:
                state[obj2] = 1
                #child[obj].append(obj2)
                par[obj2] = obj
                cur2.append(obj2)
                ranklist[-1].append(obj2)
    cur = cur2[:]
 
ans = [0] * n
ans[0] = plus[0]
 
for l in ranklist:
    for t in l:
        ans[t] = ans[par[t]] + plus[t]
 
print(" ".join(map(str,ans)))
#from collections import deque

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

a = [0] * (n-1)
b = [0] * (n-1)
info = [[] for _ in range(n)]

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

p = [0] * q
x = [0] * q


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

plus = [0] * n

for j in range(q):
    plus[p[j]] += x[j]

state = [0] * n
state[0] = 1
cur = [0]

child = [[] for _ in range(n)]
par = [0] * n

ranklist = [[1]]

#print(cur)
#print(info)

while cur != []:
    cur2 = []
    ranklist.append([])
    for obj in cur:
        for obj2 in info[obj]:
            if state[obj2] == 0:
                state[obj2] = 1
                #child[obj].append(obj2)
                par[obj2] = obj
                cur2.append(obj2)
                ranklist[-1].append(obj2)
    cur = cur2[:]

ans = [0] * n
ans[0] = plus[0]

for l in ranklist:
    for t in l:
        ans[t] = ans[par[t]] + plus[t]

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

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    E = [[] for i in range(N+1)]
    counter = [0] * (N+1)

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

    def mktree(v, parent, E):
        children = E[v]
        children.remove(parent)
        for child in children:
            mktree(child, v, E)

    for child in E[1]:
        mktree(child, 1, E)

    for i in range(Q):
        # 頂点p を根とする部分木に含まれるカウンターにxを足す
        p, x = map(int, input().split())
        counter[p] += x

    def get(v, children, counter):
        c = counter[v]
        for child in children[v]:
            counter[child] += c
            get(child, children, counter)

    #get(1, E, counter)
    #print(" ".join(str(x) for x in counter[1:]))

main()

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    E = [[] for i in range(N+1)]
    counter = [0] * (N+1)

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

    def mktree(v, parent, E):
        children = E[v]
        if parent not in children:
            return
        children.remove(parent)
        for child in children:
            mktree(child, v, E)

    for child in E[1]:
        mktree(child, 1, E)

    for i in range(Q):
        # 頂点p を根とする部分木に含まれるカウンターにxを足す
        p, x = map(int, input().split())
        counter[p] += x

    def get(v, children, counter):
        c = counter[v]
        for child in children[v]:
            counter[child] += c
            get(child, children, counter)

    get(1, E, counter)
    print(" ".join(str(x) for x in counter[1:]))

main()

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    E = [[] for i in range(N+1)]
    counter = [0] * (N+1)

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

    def mktree(v, parent, E):
        children = E[v]
        #print(v, ", parent=%d"%parent, E[v])
        children.remove(parent)
        for child in children:
            mktree(child, v, E)

    for child in E[1]:
        mktree(child, 1, E)

    if 1==0:
        for child in E[1:]:
            i+=1
            print(i, child)


    for i in range(Q):
        # 頂点p を根とする部分木に含まれるカウンターにxを足す
        p, x = map(int, input().split())
        counter[p] += x

    def get(v, children, counter):
        c = counter[v]
        for child in children[v]:
            counter[child] += c
            get(child, children, counter)

    get(1, E, counter)
    print(" ".join(str(x) for x in counter[1:]))

main()

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    E = [[] for i in range(N+1)]
    counter = [0] * (N+1)

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

    def mktree(v, parent, E):
        children = E[v]
        #print(v, ", parent=%d"%parent, E[v])
        children.remove(parent)
        for child in children:
            mktree(child, v, E)

    for child in E[1]:
        mktree(child, 1, E)

    if 1==0:
        for child in E[1:]:
            i+=1
            print(i, child)


    for i in range(Q):
        # 頂点p を根とする部分木に含まれるカウンターにxを足す
        p, x = map(int, input().split())
        counter[p] += x

    def get(v, children, counter):
        c = counter[v]
        for child in children[v]:
            counter[child] += c
            get(child, children, counter)

    get(1, E, counter)
    print(*[counter[1:]])

main()

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    
    N,Q=MI()
    edges = [[] for _ in range(N)]
    for _ in range(N-1):
        a,b = MI_()
        edges[a].append(b)
        edges[b].append(a)
    counter = [0]*N
    for _ in range(Q):
        p,x = MI()
        counter[p-1]+=x

    serched = {0}
    def dfs(now_sum,node):
        now_sum += counter[node]

        ans[node] = now_sum
        for b in edges[node]:
            if b not in serched:
                serched.add(b)
                dfs(now_sum,b)
            

    ans = [0]*N
    dfs(0,0)
    print(*ans)


    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
to = [[] for i in range(n)]
ans = [0 for i in range(n)]
def dfs(v, p = -1):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    #to[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(" ".join(map(str, ans)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
to = [[] for i in range(n)]
ans = [0 for i in range(n)]
def dfs(v, p = -1):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(" ".join(map(str, ans)))

Submission status: Runtime Error

Solution code:
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            if a == 1:
                continue
            a -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)
        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            results[q] += x
            if q == 0:
                for i in range(1, N):
                    results[i] += x
            elif verts[q] is not None:
                rests = verts[q][:]
                while rests:
                    i = rests.pop(0)
                    results[i-1] += x
                    if verts[i-1] is not None:
                        rests.extend(verts[i])
        print(" ".join(map(str, results)))
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################

def make_tree_naive(edges):
	tree = [ None ] + map(cNode, range(len(edges)+1))
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node2 = tree[v2]
		node1.children.append(node2)
	return tree

def f_naive():
	edges, operations = read_input()
	tree = make_tree_naive(edges)
	for v, n in operations:
		tree[v].add(n)
	a = sorted(tree[1].walk(0))
	print ' '.join(str(n) for i, n in a)


#################### process ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	return (edges, Q)

def make_tree(edges):
	tree = [ [] ] + [ [0] for _ in range(len(edges)+1) ]
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node1.append(v2)
	return tree

def walk(tree, v, n):
	yield (v, tree[v][0] + n)
	for child in tree[v][1:]:
		for a in walk(tree, child, tree[v][0] + n):
			yield a

def f():
	edges, Q = read_input()
	tree = make_tree(edges)
	for _ in range(Q):
		v, n = map(int, raw_input().split())
		tree[v][0] += n
	a = sorted(walk(tree, 1, 0))
	print ' '.join(str(n) for i, n in a)


#################### main ####################

f()

Submission status: Runtime Error

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################

def make_tree_naive(edges):
	tree = [ None ] + map(cNode, range(len(edges)+1))
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node2 = tree[v2]
		node1.children.append(node2)
	return tree

def f_naive():
	edges, operations = read_input()
	tree = make_tree_naive(edges)
	for v, n in operations:
		tree[v].add(n)
	a = sorted(tree[1].walk(0))
	print ' '.join(str(n) for i, n in a)


#################### process ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	operations = [ map(int, raw_input().split()) for _ in range(Q) ]
	return (edges, operations)

def make_tree(edges):
	tree = [ [] ] + [ [0] for _ in range(len(edges)+1) ]
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node1.append(v2)
	return tree

def walk(tree, v, n):
	yield (v, tree[v][0] + n)
	for child in tree[v][1:]:
		for a in walk(tree, child, tree[v][0] + n):
			yield a

def f():
	edges, operations = read_input()
	tree = make_tree(edges)
	for v, n in operations:
		tree[v][0] += n
	a = sorted(walk(tree, 1, 0))
	print ' '.join(str(n) for i, n in a)


#################### main ####################

f()

Submission status: Runtime Error

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	operations = [ map(int, raw_input().split()) for _ in range(Q) ]
	return (edges, operations)

def make_tree(edges):
	tree = [ None ] + map(cNode, range(len(edges)+1))
	for v1, v2 in sorted(edges):
		node1 = tree[v1]
		node2 = tree[v2]
		node1.children.append(node2)
	return tree


#################### process ####################


#################### main ####################

#t0 = time.clock()
edges, operations = read_input()
tree = make_tree(edges)
for v, n in operations:
	tree[v].add(n)
a = sorted(tree[1].walk(0))
print ' '.join(str(n) for i, n in a)
#print >>sys.stderr, "%.6fsec" % (time.clock() - t0,)

Submission status: Runtime Error

Solution code:

def countUpChild(Child,Score,P,X):
    if P == None:
        return Score
    else:
        Score[P] += X
        for c in Child[P]:
            Score = countUpChild(Child,Score,c,X)
        return Score
    
N,Q =( int(x) for x in input().split())
A = [0]*N
B = [0]*N
P = [0]*Q
X = [0]*Q
Child =[[] for i in range (N)]
for i in range(N-1):
    A[i],B[i] = (int(x) for x in input().split())
    Child[A[i]-1].append(B[i]-1);
    
Score = [0]*N
for i in range(Q):
    P[i],X[i] = (int(x) for x in input().split())
    Score = countUpChild(Child,Score,P[i]-1,X[i])

for i in range(N):
    print(Score[i],end=" ")

Submission status: Runtime Error

Solution code:
# coding: utf-8
from itertools import *
import sys
import time


#################### library ####################


#################### cNode ####################

class cNode:
	def __init__(self, i):
		self.i = i
		self.counter = 0
		self.children = []
	
	def add(self, n):
		self.counter += n
	
	def walk(self, n):
		yield (self.i, self.counter + n)
		for child in self.children:
			for a in child.walk(self.counter + n):
				yield a


#################### naive ####################


#################### process ####################

def read_input():
	N, Q = map(int, raw_input().split())
	edges = [ map(int, raw_input().split()) for _ in range(N-1) ]
	operations = [ map(int, raw_input().split()) for _ in range(Q) ]
	return (edges, operations)

def make_tree(edges):
	dic = { 1: cNode(1) }
	for v1, v2 in sorted(edges):
		node1 = dic[v1]
		if v2 not in dic:
			dic[v2] = cNode(v2)
		node2 = dic[v2]
		node1.children.append(node2)
	return dic


#################### main ####################

#t0 = time.clock()
edges, operations = read_input()
tree = make_tree(edges)
for v, n in operations:
	tree[v].add(n)
a = sorted(tree[1].walk(0))
print ' '.join(str(n) for i, n in a)
#print >>sys.stderr, "%.6fsec" % (time.clock() - t0,)

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)

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

Submission status: Runtime Error

Solution code:
n, q = tuple([int(i) for i in input().split(' ')])

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

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

def solve(p, x):
    c[p] += x
    if len(g[p]) == 0:
        return
    for e in g[p]:
        solve(e, c[p])

for i in range(q):
    p, x = tuple([int(i) for i in input().split(' ')])
    c[p] += x

solve(1, 0)

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

Submission status: Runtime Error

Solution code:
n,q=map(int,input(),split())
tr=[]
for i in range(n-1):
    tr.append(list(map(int,input().split())))
tr.sort()
a=[0]*n
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]+=c
for i in range(n-1):
    a[tr[i][1]-1]+=a[tr[i][0]-1]
print(*a)

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
ab = [list(input().split()) for i in range(n-1)]
px = {}
for i in range(q):
    p,x = input().split()
    x = int(x)
    if p in px.keys():
        px[p] += x
    else:
        px[p] = x

class Node:
    def __init__(self,name):
        self.name = name
        self.data = 0
        self.edge = []

class NST:
    def __init__(self, edge_info, data_info):
        self.root = None
        self.node_list = []

        for i in range(1,len(edge_info)+2):
            self.node_list.append(Node(str(i)))

        for node in self.node_list:
            if node.name in data_info:
                node.data += data_info[node.name]

        for ij in edge_info:
            i = ij[0]
            j = ij[1]
            self.make(self.node_list,i,j)

        self.go_forward(self.node_list[0])

    def make(self, node_list, n1, n2):
        node = node_list[int(n1)-1]
        node.edge.append(node_list[int(n2)-1])

    def go_forward(self,node):
        x = node.data
        for next in node.edge:
            next.data += x
            if next.edge == []:
                pass
            else:
                self.go_forward(next)

if __name__ == '__main__':
    ans = NST(ab, px)
    print(*[n.data for n in ans.node_list])

Submission status: Runtime Error

Solution code:
"""
N Q
a1 b1
...
an-1 bn-1
p1 x1
...
pq xq

4 3
1 2
2 3
2 4
2 10
1 100
3 1
"""

n, q = map(int,input().split())
ab = [list(input().split()) for i in range(n-1)]
px = {}
for i in range(q):
    p,x = input().split()
    x = int(x)
    if p in px.keys():
        px[p] += x
    else:
        px[p] = x

#print(ab)
#print(px)

class Node:
    def __init__(self,name):
        self.name = name
        self.data = 0
        self.edge = []

class NST:
    def __init__(self, edge_info, data_info):
        self.root = None
        self.node_list = []

        for i in range(1,len(edge_info)+2):
            self.node_list.append(Node(str(i)))

        for node in self.node_list:
            if node.name in data_info:
                node.data += data_info[node.name]

        for ij in edge_info:
            i = ij[0]
            j = ij[1]
            self.make(self.node_list,i,j)

        self.go_forward(self.node_list[0])

    def make(self, node_list, n1, n2):
        node = node_list[int(n1)-1]
        node.edge.append(node_list[int(n2)-1])

    def go_forward(self,node):
        x = node.data
        for next in node.edge:
            next.data += x
            if next.edge == []:
                pass
            else:
                self.go_forward(next)

if __name__ == '__main__':
    ans = NST(ab, px)
    print(*[n.data for n in ans.node_list])

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+1)
A = [0] * (n+1)
B = [0] * (n+1)
P = [0] * (q+1)
X = [0] * (q+1)
to = [] * n+1
cnt = [[] for i in range(n+1)] 
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
    to[A[i]].append(B[i])
    to[B[i]].append(A[i])
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]
    T[P[i]] = X[i]
NIL = -1

q =deque()
q.append((1,0,NIL))
while len(q)>0:
    p,x,prev = q.pop()
    x += T[p]
    cnt[p] = x
    for i in to[p]:
        if i != prev:
            q.append((i,x,p))

print(*cnt)

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+1)
A = [0] * (n+1)
B = [0] * (n+1)
P = [0] * (q+1)
X = [0] * (q+1)
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]

NIL = -1

class node:
    def __init__(self):
        self.value = 0
        self.child = []
        self.parent = NIL

def childadd(T,x):
    for i in range(len(T[x].child)):
        T[T[x].child[i]].value += T[x].value
        if T[T[x].child[i]].child != []:
            childadd(T,T[x].child[i])


T[1] = node()
T[1].parent = 0
for i in range(1,n):
    if T[A[i]].parent != NIL:
        T[B[i]] = node()
        T[A[i]].child.append(B[i])
        T[B[i]].parent = T[A[i]]
    else:
        T[A[i]] = node()
        T[B[i]].child.append(A[i])
        T[A[i]].parent = T[B[i]]

for i in range(1,q+1):
    T[P[i]].value += X[i]

childadd(T,1)

for i in range(1,n+1):
    print(T[i].value,end = " ")

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+1)
A = [0] * (n+1)
B = [0] * (n+1)
P = [0] * (q+1)
X = [0] * (q+1)
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]

NIL = -1

class node:
    def __init__(self):
        self.value = 0
        self.child = []
        self.parent = NIL

def childadd(T,x):
    for i in range(len(T[x].child)):
        T[T[x].child[i]].value += T[x].value
        if T[T[x].child[i]].child != []:
            childadd(T,T[x].child[i])

for i in range(1,n+1):
    T[i] = node()

for i in range(1,n):
    T[A[i]].child.append(B[i])

for i in range(1,q+1):
    T[P[i]].value += X[i]

childadd(T,1)

for i in range(1,n+1):
    print(T[i].value,end = " ")

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+1)
A = [0] * (n+1)
B = [0] * (n+1)
P = [0] * (q+1)
X = [0] * (q+1)
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]

NIL = -1

class node:
    def __init__(self):
        self.value = 0
        self.child = []
        self.parent = NIL

def childadd(T,x):
    for i in range(len(T[x].child)):
        T[T[x].child[i]].value += T[x].value
        childadd(T,T[x].child[i])

for i in range(1,n+1):
    T[i] = node()

for i in range(1,n):
    T[A[i]].child.append(B[i])

for i in range(1,q+1):
    T[P[i]].value += X[i]

childadd(T,1)

for i in range(1,n+1):
    print(T[i].value,end = " ")

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+5)
A = [0] * (n+5)
B = [0] * (n+5)
P = [0] * (q+5)
X = [0] * (q+5)
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]

NIL = -1

class node:
    def __init__(self):
        self.value = 0
        self.child = []
        self.parent = NIL

def childadd(T,x):
    for i in range(len(T[x].child)):
        T[T[x].child[i]].value += T[x].value
        childadd(T,T[x].child[i])

for i in range(1,n+1):
    T[i] = node()

for i in range(1,n):
    T[B[i]].parent = T[A[i]]
    T[A[i]].child.append(B[i])

for i in range(1,q+1):
    T[P[i]].value += X[i]

childadd(T,1)

for i in range(1,n+1):
    print(T[i].value,end = " ")

Submission status: Runtime Error

Solution code:
n,q = input().split()
n = int(n)
q = int(q)
T = [0] * (n+5)
A = [0] * (n+5)
B = [0] * (n+5)
P = [0] * (q+5)
X = [0] * (q+5)
for i in range(1,n):
    A[i],B[i] = [int(j) for j in input().split()]
for i in range(1,q+1):
    P[i],X[i] = [int(j) for j in input().split()]

NIL = -1

class node:
    def __init__(self):
        self.value = 0
        self.child = []
        self.parent = NIL

def childadd(T,x):
    if T[x].child != []:
        for i in range(len(T[x].child)):
            T[T[x].child[i]].value += T[x].value
            childadd(T,T[x].child[i])

for i in range(1,n+1):
    T[i] = node()

for i in range(1,n):
    T[B[i]].parent = T[A[i]]
    T[A[i]].child.append(B[i])

for i in range(1,q+1):
    T[P[i]].value += X[i]

childadd(T,1)

for i in range(1,n+1):
    print(T[i].value,end = " ")

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

n , q = map(int, sys.stdin.readline().split())
# n, q = 3, 2
# arr = [(1, 3), (2, 3)]

graph = defaultdict(list)
for i in range(n-1):
	x, y = map(int, sys.stdin.readline().split())
	# x, y = arr[i]
	graph[x].append(y)
	graph[y].append(x)

visited =set()
def dfs(src):
	global graph, lazy, visited
	visited.add(src)
	for neighbour in graph[src]:
		if neighbour not in visited:
			lazy[neighbour] += lazy[src]
			dfs(neighbour)

lazy = [0]*(n+1)
queries = [(1, 1), (2, 2)]
for i in range(q):
	idx, val = map(int, sys.stdin.readline().split())
	# idx, val = queries[i]
	lazy[idx] += val
dfs(1)
print(*lazy[1:])