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
  
class tree:
    def __init__(self):
        self.baby_list=[]
        self.counter=0
    
    def add_value(self):
        for i in self.baby_list:
            hoge[i].counter+=self.counter
            hoge[i].add_value()
            
N,Q=list(map(int, input().split() ))

hoge={i:tree() for i in range(1,N+1)}
    
for i in range(N-1):
    a,b=list(map(int, input().split() ))
    hoge[a].baby_list.append(b)

for i in range(Q):
    root, value = list(map(int, input().split() ))
    hoge[root].counter+=value

hoge[1].add_value()
output = []
for t in hoge:
    output.append(hoge[t].counter)

    
output = list(map(str, output))
print(" ".join(output))

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)

values = [0] * (N+1)
for p,x in PX:
    values[p] += x
def dfs(v,parent,add):
    values[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,values[v])
        
dfs(1,0,0)
answer = ' '.join(map(str,values[1:]))
print(answer)

Submission status: Runtime Error

Solution code:
def main():
        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)]
        ab.sort()
        
        plus = [0 for _ in range(N)]
        for _ in range(Q):
        	p,x = map(int,input().split())
        	plus[p - 1] += x 

        for a,b in ab:
                plus[b - 1] += plus[a - 1]
        for p in plus:
                print(p)

if __name__ == '__main__':
        main()

Submission status: Runtime Error

Solution code:
N, Q = [int(x) for x in input().split()]
ab_list = [[int(x) for x in input().split()] for _ in range(N-1)]
px_list = [[int(x) for x in input().split()] for _ in range(Q)]
to = [[] for _ in range(N+1)]
for a, b in ab_list:
    a -= 1
    b -= 1
    to[b].append(a)
    to[a].append(b)
ans = [0] * N
for p, x in px_list:
    ans[p-1] += x

def dfs(v, p=-1):
    for u in to[v]:
        if (u == p): continue
        ans[u] += ans[v]
        dfs(u, v)        
dfs(0)
print(' '.join([str(i) for i in 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)

values = [0] * (N+1)
for p,x in PX:
    values[p] += x
def dfs(v,parent,add):
    values[v]+=add
    for x in graph[v]:
        if x==parent:
            continue
        dfs(x,v,values[v])
        
dfs(1,0,0)
answer=' '.join(map(str,values[1:]))
print(answer)

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)

values = [0] * (N+1)
for p,x in PX:
    value[p] += x
def dfs(v,parent,add):
    values[v]+=add
    for x in graph[v]:
        if x==parent:
            continue
        dfs(x,v,values[v])
        
dfs(1,0,0)
answer=' '.join(map(str,values[1:]))
print(answer)

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):
    values[v]+=add
    for x in graph[v]:
        if x==parent:
            continue
        dfs(x,v,values[v])
        
dfs(1,0,0)
answer=' '.join(map(str,values[1:]))
print(answer)

Submission status: Runtime Error

Solution code:
def d_ki():
    import sys
    input = sys.stdin.readline
    N, Q = [int(i) for i in input().split()]

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

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

    # 1次元配列だったら単純に累積和を取ればいいのだが、木なので
    # DFSをしなければならない
    def dfs(current, parent=-1):
        for child in tree[current]:
            if child == parent:
                continue
            ans[child] += ans[current]
            dfs(child, current)
    dfs(0)
    return ' '.join(map(str, ans))

print(d_ki())

Submission status: Runtime Error

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

values=[0]*(N+1)
for _ in range(Q):
    p,x=map(int,input().split())
    values[p]+=x
def dfs(v,parent,add):
    values[v]+=add
    for x in graph[v]:
        if x==parent:
            continue
        dfs(x,v,values[v])
        
dfs(1,0,0)
answer=' '.join(map(str,values[1:]))
print(answer)

Submission status: Runtime Error

Solution code:
NQ = input().split(' ')
N = int(NQ[0])
Q = int(NQ[1])
tree_list = {0: []}
out_put_list = [0] * N

for i in range(N-1):
    tree_list[i+1] = []
    ab = input().split(' ')
    a = int(ab[0]) -1
    b = int(ab[1]) -1
    tree_list[a].append(b)

for _ in range(Q):
    px = input().split(' ')
    p = int(px[0])
    x = int(px[1])
    out_put_list[p-1] += x

for i in range(len(tree_list)):
    for num in tree_list[i]:
        out_put_list[num] += out_put_list[i]

print(*out_put_list)

Submission status: Runtime Error

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

values=[0]*(N+1)
for _ in range(q):
    p,x=map(int,input().split())
    values[p]+=x
def dfs(v,parent,add):
    values[v]+=add
    for x in graph[v]:
        if x==parent:
            continue
        dfs(x,v,values[v])

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

Submission status: Runtime Error

Solution code:
    class Node:
        def __init__(self, index):
            self.count = 0
            self.fr = -1
            self.index = index
     
     
    N, Q = map(int, input().split())
    node = [Node(i) for i in range(N)]
    for i in range(N - 1):
        fr, to = map(int, input().split())
        node[to - 1].fr = fr - 1
     
    for i in range(Q):
        fr, c = map(int, input().split())
        node[fr - 1].count += c
     
    print(node[0].count, end=' ')
     
    for i in range(1, N):
        node[i].count += node[node[i].fr].count
        if not i == N:
            print(node[i].count, end=' ')
        else:
            print(node[i].count)

Submission status: Runtime Error

Solution code:
#-------------------------------------------------------------------
import sys
def pp(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
n, q = map(int, input().split())		# 5 7 2

to = [ [] for _ in range(n+1) ]	#=> [[], [], [], [], [], [], [], []]
ans=[0]*(n)

def dfs(v,p):
	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())		# 5 7 2
	a-=1
	b-=1
	to[a].append(b)
	to[b].append(a)

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


dfs(0,-1)



print( *ans )
	
pp("to ",to)
pp("ans",ans)

Submission status: Runtime Error

Solution code:
#-------------------------------------------------------------------
import sys
def pp(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
n, q = map(int, input().split())		# 5 7 2

to = [ [] for _ in range(N+1) ]	#=> [[], [], [], [], [], [], [], []]
ans=[0]*(N)

def dfs(v,p):
	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())		# 5 7 2
	a-=1
	b-=1
	to[a].append(b)
	to[b].append(a)

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


dfs(0,-1)



print( *ans )

Submission status: Runtime Error

Solution code:
NQ = input().split(' ')
N = int(NQ[0])
Q = int(NQ[1])
candidate_list = []
tree_list = [[]] * N
out_put_list = [0] * N
def f(p,x):
    out_put_list[p] += x
    for idx in tree_list[p]:
        f(idx,x)

for i in range(N-1):
    ab = input().split(' ')
    a = int(ab[0])
    b = int(ab[1])
    tree_list[a-1] = tree_list[a-1] + [b-1]

print(tree_list)
for _ in range(Q):
    px = input().split(' ')
    p = int(px[0])
    x = int(px[1])
    f(p-1,x)

print(*out_put_list)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <limits>
#include <numeric>
#include <map>
#include <cstring>
using namespace std;

int main()
{
    long long N, Q;
    cin >> N;
    cin >> Q;

    std::map<long long, std::vector<long long>> C;
    for (long long i = 0; i < N - 1; i++) {
        long long a, b;
        cin >> a;
        cin >> b;
        if (a > b) {
            std::swap(a, b);
        }
        if (C.count(a) == 0) {
            std::vector<long long> r;
            C[a] = r;
        }
        C[a].push_back(b);
    }

    long long S[200001];
    memset(S, 0, sizeof(S));
    for (long long i = 0; i < Q; i++) {
        long long p, x;
        cin >> p;
        cin >> x;
        S[p - 1] += x;
    }

    for (long long i = 1; i < N; ++i) {
        if (C.count(i) == 0) {
            continue;
        }
        auto r = C[i];
        auto s = S[i - 1];
        for (auto j : r) {
            S[j - 1] += s;
        }
    }

    for (long long i = 0; i < N; ++i) {
        std::cout << S[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

Submission status: Runtime Error

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

to = defaultdict(list)

def dfs(v, p = -1):
    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)

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

dfs(0)

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

Submission status: Runtime Error

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

to = defaultdict(list)

def dfs(v, p = -1):
    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)

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

dfs(0)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def dfs(tree, root, counter, parent):
    counter[root] += parent
    count = counter[root]
    childs = tree[root]
    for c in childs:
        dfs(tree, c, counter, count)


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

counter = [0 for _ in range(n)]
children = [[] for _ in range(n)]

# read a_i b_i
for _ in range(n-1):
    a, b = map(int, input().split())
    children[a-1].append(b-1)

# read p_i x_i
for _ in range(q):
    p, xi = map(int, input().split())
    counter[p-1] += xi

dfs(children, 0, counter, 0)

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

Submission status: Runtime Error

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

class Node:
    def __init__(self):
        self.list = []
        self.score = 0

    def add(self, node):
        self.list.append(node)

nodes = []
for i in range(0, N):
    nodes.append(Node())

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

    nodes[a].add(nodes[b])

for i in range(0, Q):
    p, x = [int(v) for v in input().split()]

    nodes[p-1].score += x

def search(node):
    for child in node.list:
        child.score += node.score
        search(child)

search(nodes[0])

print(' '.join(map(lambda n: str(n.score), nodes)))

Submission status: Runtime Error

Solution code:
def dfs(gr,li,n,fr):
    if(len(gr[n])==1 and gr[n][0]==fr):
        li[n].add(n)
        return set([n])
    else:
        ans = set([n])
        for i in gr[n]:
            if(i==fr):continue
            ans |= dfs(gr,li,i,n)
        li[n] |= ans
        return ans
#%%
N,Q = [int(x) for x in input().split()]
nlist = [set() for _ in range(N)]
gp = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = [int(x) for x in input().split()]
    gp[a-1].append(b-1)
    gp[b-1].append(a-1)

qu = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
li = [0]*N
dfs(gp,nlist,0,-1)
for p,x in qu:
    for j in nlist[p-1]:
        li[j] += x
print(*li)

Submission status: Runtime Error

Solution code:
import sys

n, q = map(int, sys.stdin.readline().split())

near = [[] for _ in range(n+1)]
solved = [0] * n+1

child = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    near[a].append(b)
    near[b].append(a)

wait = [1]

while True:
    target = wait.pop()
    for nex in near[target]:
        if solved[nex] != 1:
            child[target].append(nex)
            wait.append(nex)
            solved[nex] = 1
        if len(wait) == 0:
            break

score = [0] * (n+1)
for i in range(q):
    p, x = map(int, sys.stdin.readline().split())
    score[p] += x

wait = [1]

while True:
    target = wait.pop()
    for nex in child[target]:
        score[nex] += score[target]
        wait.append(nex)
    if len(wait) == 0:
        break

score.pop(0)

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

Submission status: Runtime Error

Solution code:


MAX = 100002

def add_num(nn, x):
    if nn == -1:
        return
    Tree[nn][0] += x
    for ni in Tree[nn][1]:
        add_num(ni, x)


ipt = input().split()
N = int(ipt[0])
Q = int(ipt[1])



Tree = []
for m in range(MAX-1):
    Tree.append([-1, [-1]])

nodes = set()
nodes.add(1)

for i in range(N-1):
    ipti = input().split()
    a = int(ipti[0])
    b = int(ipti[1])

    Tree[a][0] = 0
    Tree[a][1].insert(0, b)
    Tree[b][0] = 0

    nodes.add(b)



for i in range(Q):
    ipti = input().split()
    p = int(ipti[0])
    x = int(ipti[1])

    cands = set()

    Tree[p][0] += x

    for pi in range(p,MAX-1):
        if Tree[pi][0] == -1:
            break
        for pii in Tree[pi][1]:
            cands.add(pii)
    for ci in cands:
        Tree[ci][0] += x

    #add_num(p, x)


for n in nodes:
    print(Tree[n][0])

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
class Tree:
   def __init__(self, n):
        self.n = n
        self.tree = {i:[] for i in xrange(1, self.n + 1)}
        self.value= dd(int)
    
   def addNode(self, u, v):
        self.tree[u].append(v)
        self.tree[v] +=[]
    
   def update(self, u, n):
       self.value[u] += n
   
   def query(self,root):
      if root <= self.n:
         for node in self.tree[root]:
            self.value[node] + = self.value[root]
            self.query(node)

   
   def ans(self):
       self.query(1)
       for i in xrange(1,self.n+1):
           print self.value[i],
       print ""

f = lambda : map(int, raw_input().split())

n, q = f()
tree = Tree(n)
for i in xrange(n - 1):
   tree.addNode(*f())
   
for i in xrange(q):
   tree.update(*f())

tree.ans()

Submission status: Runtime Error

Solution code:
#~~input~~
N,Q=map(int,input().split())
a=[]
b=[]
for i in range(N-1):
    A,B=input().split()
    a.append(int(A))
    b.append(int(B))
p=[]
x=[]
for i in range(Q):
    P,X=input().split()
    p.append(int(P))
    x.append(int(X))

#~~mk tree~~
counter=[0]*max(max(a)+1,max(b)+1)
dic = {}
for a_,b_ in zip(a,b):
  if a_ in dic.keys():
    dic[a_].append(b_)
  else:
    dic.setdefault(a_, []).append(b_)
#print(dic)
for i in range(Q):
  j=p[i]
  def search_tree(j):
    #exist child
    if j in dic.keys():
      for d_j in dic[j]:
        search_tree(d_j)
    #root add
    #print(x[i])
    counter[j]+=x[i]
  search_tree(j)
print(*counter[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)]
ab.sort()
p = [0]*n
for i in range(q):
  a, b = map(int, input().split())
  p[a - 1] += b
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(" ".join(map(str, p)

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
class Tree:
   def __init__(self, n):
        self.n = n
        self.tree = {i:[] for i in xrange(1, self.n + 1)}
        self.value= {i:0 for i in xrange(1, self.n + 1)}
    
   def addNode(self, u, v):
        self.tree[u].append(v)
    
   def update(self, u, n):
       self.value[u] += n
   
   def query(self,root):
       for node in self.tree[root]:
          #self.value[node] = self.value[node] + self.value[root]
          self.query(node)

   
   def ans(self):
       self.query(1)
       for i in xrange(1,self.n+1):
           print self.value[i],
       print ""

f = lambda : map(int, raw_input().split())

n, q = f()
tree = Tree(n)
for i in xrange(n - 1):
   tree.addNode(*f())
   
for i in xrange(q):
   tree.update(*f())

tree.ans()

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, Q = [int(s) for s in input().split()]


children = defaultdict(list)

for _i in range(N-1):
    a, b = [int(s) for s in input().split()]
    children[a].append(b)

values = defaultdict(int)

def add(node, num):
    values[node] += num
    for c in children[node]:
        add(c, num)

for _j in range (Q):
    p, x = [int(s) for s in input().split()]
    add(p, x)

print(values)

Submission status: Runtime Error

Solution code:

class Node:
    def __init__(self, parent, num):
        self.parent = parent
        self.num = num
        self.score = 0

    def add_score(self, score):
        self.score += score

class Tree:
    @classmethod
    def calc_sum(cls, node):
        if node.parent:
            return node.score + cls.calc_sum(node.parent)
        else:
            return node.score

N, Q = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(1, N):
    A[i], B[i] = map(int, input().split())

P = [0] * (Q+1)
X = [0] * (Q+1)
for i in range(1, Q+1):
    P[i], X[i] = map(int, input().split())

nodes = {1: Node(None, 1)}
for i in range(1, N):
    nodes[B[i]] = Node(nodes[A[i]], B[i])

for i in range(1, Q+1):
    nodes[P[i]].add_score(X[i])

for i in range(1, N+1):
    sum_score = Tree.calc_sum(nodes[i])
    print(sum_score, end=' ')

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
T = [[0, [], []] for _ in range(N+1)] #counter, parents, children
E = list(map(int, input().split())) for _ in range(N-1)]
for e in E:
  p, c = e
  T[c][1] += T[p][1]
  T[c][1].append(p)
  for q in T[c][1]:
    T[q][2].append(c)
for _ in range(Q):
  p, x = map(int, input().split())
  T[p][0] += x
  for c in T[p][2]:
    T[c][0] += x
ans_s = ""
for t in T[1:]:
  ans_s += "%d "%t[0]
print(ans_s)

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
class Tree:
   def __init__(self, n):
        self.n = n
        self.tree = {i:[] for i in xrange(1, self.n + 1)}
        self.value= {i:0 for i in xrange(1, self.n + 1)}
    
   def addNode(self, u, v):
        self.tree[u].append(v)
    
   def update(self, u, n):
       self.value[u] += n
   
   def query(self,root):
       for node in self.tree[root]:
          self.value[node] = self.value[node] + self.value[root]
          self.query(node)

   
   def ans(self):
       self.query(1)
       for i in xrange(1,self.n+1):
           print self.value[i],
       print ""

f = lambda : map(int, raw_input().split())

n, q = f()
tree = Tree(n)
for i in xrange(n - 1):
   tree.addNode(*f())
   
for i in xrange(q):
   tree.update(*f())

tree.ans()

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
a = [tuple(map(int, input().split())) for _ in range(N-1)]
b = [list(map(int, input().split())) for _ in range(M)]
ans=[0]*N
results = {}
for word in a:
    results.setdefault(word[0], []).append(word[1])

for i in b:
    ans[i[0]-1] += i[1]

def sumup(ne):
    global ans
    if ne in results:    
        for k in results[ne]:
            ans[k-1] += ans[ne-1]
            sumup(k)

sumup(1)
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:

def solve(N, Q, ABs, PXs):
    values = {i: 0 for i in range(Q + 1)}
    ans = [-1 for i in range(N + 1)]
    vertexes = {}
    for a, b in ABs:
        vertexes.setdefault(a, []).append(b)
        vertexes.setdefault(b, []).append(a)

    for p, x in PXs:
        values[p] += x

    queue = []
    queue.append((1, 0))
    while queue:
        index, parent_value = queue.pop()
        value = parent_value + values[index]
        ans[index] = value
        for child in vertexes[index]:
            if ans[child] == -1:
                queue.append((child, value))
    return " ".join([str(a) for a in ans[1:]])


if __name__ == "__main__":
    N, Q = tuple(map(int, input().split(" ")))
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N - 1)]
    PXs = [tuple(map(int, input().split(" "))) for _ in range(Q)]
    print(solve(N, Q, ABs, PXs))

Submission status: Runtime Error

Solution code:
N, Q = list(map(int, input().split()))
a = [0]*(N-1)
b = [0]*(N-1)
p = [0]*Q
x = [0]*Q
for i in range(N-1):
  a[i],b[i] = list(map(int, input().split()))
for i in range(Q):
  p[i],x[i] = list(map(int, input().split()))

node_value = [0]*N

# ----------------------------------
table = [[] for i in range(N)]
for j in range(N-1):
    table[a[j]-1].append(b[j])

# ----------------------------------

def func(p, x):
    global table
    global node_value
    node_value[p-1] = node_value[p-1] + x

    leaflist =  table[p-1]
    if leaflist != []:
        for leaf in leaflist:
            func(leaf, x)

# ----------------------------------
# summation
for i in range(Q):
    func(p[i], x[i])
# ----------------------------------
    
for node in node_value:
  print(node, end=' ')

Submission status: Runtime Error

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
for _ in range(N-1): # 10**5 # ここじゃない
    ai, bi = map(int, input().split())
    info_lst[ai].append(bi)
    info_lst[bi].append(ai)

passed_place = [False] * (N+1)
passed_place[1] = True
glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない

output_str = "" #####################

try:
    pi_xi_sum_lst = [0]*(N+1)
    for _ in range(Q): # 10**5
        pi, xi = map(int, input().split())
        pi_xi_sum_lst[pi] += xi

    output_lst = [0]*(N+1)
    count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
    #output_str = ""
    for i in range(1, N+1): # ここじゃない
        output_str += str(output_lst[i]) + " "
except:
    pass
print(output_str)

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
class Tree:
   def __init__(self, n):
        self.n = n
        self.tree = {i:[] for i in xrange(1, n + 1)}
        self.value= {i:0 for i in xrange(1, n + 1)}
    
   def addNode(self, u, v):
        self.tree[u].append(v)
    
   def update(self, u, n):
       self.value[u] += n
   
   def query(self,root):
       for node in self.tree[root]:
          self.value[node] = self.value[node] + self.value[root]
          self.query(node)

   
   def ans(self):
       self.query(1)
       for i in xrange(1,self.n+1):
           print self.value[i],
       print ""

f = lambda : map(int, raw_input().split())

n, q = f()
tree = Tree(n)
for i in xrange(n - 1):
   tree.addNode(*f())
   
for i in xrange(q):
   tree.update(*f())

tree.ans()

Submission status: Runtime Error

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
for _ in range(N-1): # 10**5 # ここじゃない
    ai, bi = map(int, input().split())
    info_lst[ai].append(bi)
    info_lst[bi].append(ai)

passed_place = [False] * (N+1)
passed_place[1] = True
glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない

pi_xi_sum_lst = [0]*(N+1)
try:
    for _ in range(Q): # 10**5
        pi, xi = map(int, input().split())
        pi_xi_sum_lst[pi] += xi
except:
    pass

output_lst = [0]*(N+1)
count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
output_str = ""
for i in range(1, N+1): # ここじゃない
    output_str += str(output_lst[i]) + " "
print(output_str)

Submission status: Runtime Error

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
try:
    for _ in range(N-1): # 10**5 # ここじゃない
        ai, bi = map(int, input().split())
        info_lst[ai].append(bi)
        info_lst[bi].append(ai)
except:
    pass

passed_place = [False] * (N+1)
passed_place[1] = True
try:
    glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない
except:
    pass
pi_xi_sum_lst = [0]*(N+1)
for _ in range(Q): # 10**5
    pi, xi = map(int, input().split())
    pi_xi_sum_lst[pi] += xi

output_lst = [0]*(N+1)
count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
output_str = ""
for i in range(1, N+1): # ここじゃない
    output_str += str(output_lst[i]) + " "
print(output_str)

Submission status: Runtime Error

Solution code:
import copy

N,Q = [int(s) for s in input().split(" ")]

def linkedAdd(edges,idx):
    treeList2[idx] = treeList2[idx] + edges[1:]
    for i in edges[1:]:
        linkedAdd(treeList[i], idx)
    
    
    
treeList = [[i] for i in range(N)]
sumList = [0 for i in range(N)]

for i in range(N-1):
    a, b = [int(s) for s in input().split(" ")]
    treeList[a-1].append(b-1)
#print(treeList)

treeList2 = copy.deepcopy(treeList)

for j in range(len(treeList)):
    #print(j, treeList[j])
    for i in treeList[j][1:]:
        linkedAdd(treeList[i],j)

for i in range(Q):
    p,x = [int(s) for s in input().split(" ")]
    for j in treeList2[p-1]:
        sumList[j] += x
sumStrList = map(str, sumList)
print(" ".join(sumStrList))

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
class Tree:
   def __init__(self, n):
        self.n = n
        self.tree = dd(list)
        self.value = dd(int)
    
   def addNode(self, u, v):
        self.tree[u].append(v)
    
   def update(self, u, n):
       self.value[u] += n
   
   def query(self,root):
      if self.tree.get(Node) != None:
        for node in self.tree[root]:
           self.value[node] += self.value[root]
           self.query(node)

   
   def ans(self):
       self.query(1)
       for i in xrange(1,self.n+1):
           print self.value[i],
       print ""

f = lambda : map(int, raw_input().split())

n, q = f()
tree = Tree(n)
for i in xrange(n - 1):
   tree.addNode(*f())
   
for i in xrange(q):
   tree.update(*f())

tree.ans()