Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

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

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
a=[[] for i in range(n)]
p=[]*n
l=[0]*n

def dfs(parent,current):
    for i in a[current-1]:
        if i != parent:
            l[i-1] += l[current-1]
            dfs(current,i)


for i in range(n-1):
    t1,t2 = map(int,input().split())
    a[t1-1].append(t2)
    a[t2-1].append(t1)
    
for i in range(q):
    t1,t2 = map(int,input().split())
    l[t1-1]+=t2
dfs(-1,1)

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline
sys.setrecursionlimit(5000)
 
def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)
 
d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)
 
for i in range(q):
    p,x=map(int,input().split())
    w[p]+=x
dfs(1)
 
print(*w[1:])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline
sys.setrecursionlimit(5000)
 
def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)
 
d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)
 
for i in range(q):
    p,x=map(int,input().split())
    w[p]+=x
dfs(1)
 
print(*w[1:])
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline
 
def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)
 
d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)
 
for i in range(q):
    p,x=map(int,input().split())
    w[p]+=x
dfs(1)
 
print(*w[1:])
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline
sys.setrecursionlimit(5000)
 
def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)
 
d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)
 
for i in range(q):
    p,x=map(int,input().split())
    w[p]+=x
dfs(1)
 
print(*w[1:])
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline

def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)

d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)

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

print(*w[1:])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline
sys.setrecursionlimit(5000)
 
def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)
 
d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)
 
for i in range(q):
    p,x=map(int,input().split())
    w[p]+=x
dfs(1)
 
print(*w[1:])
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
input=sys.stdin.readline

def dfs(v,p=-1):
    if v in d:
        for i in d[v]:
            if (i==p): continue
            w[i]+=w[v];
            dfs(i,v)

d={}
n,q=map(int,input().split())
for i in range(n-1):
    a,b=map(int,input().split())
    if a in d:
        d[a].add(b)
    else:
        d[a]=set([b])
    if b in d:
        d[b].add(a)
    else:
        d[b]=set([a])
w=[0]*(n+1)

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

print(*w[1:])

Submission status: Runtime Error

Solution code:
main()

def main():    
    i, q = [int(x) for x in input().split(' ')]

    root = Node(1)
    for idx in range(i-1):
        a, b = [int(x) for x in input().split(' ')]
        root.add_child(a, b)

    for _ in range(q):
        p, x = [int(x) for x in input().split(' ')]
        root.set_count(p, x)

    root.add_count()

    counts = sorted(root.get_counts(), key=itemgetter(0))
    print(' '.join([str(x[1]) for x in counts]))


class Node:
    def __init__(self, id):
        self.id = id
        self.count = 0
        self.children = []

    def add_child(self, parent_id, child_id):
        if parent_id < self.id:
            return

        if parent_id == self.id:
            # 追加
            self.children.append(Node(child_id))
            return

        for child in self.children:
            child.add_child(parent_id, child_id)
        return

    def set_count(self, target_id, num, is_count=False):
        # if target_id == self.id:
        #     is_count = True

        # if is_count:
        #     self.count += num

        # for child in self.children:
        #     child.add_count(target_id, num, is_count)
        if target_id < self.id:
            return
        if target_id == self.id:
            self.count += num
            return
        for child in self.children:
            child.set_count(target_id, num, is_count)
        return

    def add_count(self, num=0):
        self.count += num
        for child in self.children:
            child.add_count(self.count)
        return

    def get_counts(self):
        res = [(self.id, self.count)]
        # print(self.id, self.count)
        for child in self.children:
            res += child.get_counts()
        return res

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(n-1)]
p_x = [list(map(int, input().split())) for _ in range(q)]

v = [[] for i in range(n)]
for e in a_b:
    v[e[0]-1].append(e[1]-1)
    v[e[1]-1].append(e[0]-1)
score = [0 for i in range(n)]
for e in p_x:
    score[e[0]-1]+=e[1]
result = [0 for i in range(n)]
def dfs(p, visited):
    if visited ==[]:
        visited.append(p)
    for c in v[p]:
        if not c in visited:
            visited.append(c)
            score[c]+=score[p]
            dfs(c, visited)
dfs(0, [])
print(*score)

Submission status: Runtime Error

Solution code:
   
i, q = [int(x) for x in input().split(' ')]
root = Node(1)
for idx in range(i-1):
    a, b = [int(x) for x in input().split(' ')]
    root.add_child(a, b)

for _ in range(q):
    p, x = [int(x) for x in input().split(' ')]
    root.add_count(p, x)

counts = sorted(root.get_counts(), key=itemgetter(0))
print(' '.join([str(x[1]) for x in counts]))



class Node:
    def __init__(self, id):
        self.id = id
        self.count = 0
        self.children = []

    def add_child(self, parent_id, child_id):
        if parent_id < self.id:
            return

        if parent_id == self.id:
            # 追加
            self.children.append(Node(child_id))
            return

        for child in self.children:
            child.add_child(parent_id, child_id)
        return

    def add_count(self, target_id, num, is_count=False):
        if target_id == self.id:
            is_count = True

        if is_count:
            self.count += num

        for child in self.children:
            child.add_count(target_id, num, is_count)
        return

    def get_counts(self):
        res = [(self.id, self.count)]
        # print(self.id, self.count)
        for child in self.children:
            res += child.get_counts()
        return res

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(n-1)]
p_x = [list(map(int, input().split())) for _ in range(q)]

v = [[] for i in range(n)]
for e in a_b:
    v[e[0]-1].append(e[1]-1)
    v[e[1]-1].append(e[0]-1)
score = [0 for i in range(n)]
for e in p_x:
    score[e[0]-1]+=e[1]
result = [0 for i in range(n)]
def dfs(p, visited):
    if len(visited)==n:
        print(*score)
        return
    if visited ==[]:
        visited.append(p)
    for c in v[p]:
        if not c in visited:
            visited.append(c)
            score[c]+=score[p]
            dfs(c, visited)
dfs(0, [])

Submission status: Runtime Error

Solution code:
Answer = []
NQ = input().split(" ")
#NQ[0]が頂点数N　NQ[1]が操作回数Q
N = int(NQ[0])
Q = int(NQ[1])

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

pxlist = []
for i in range(Q):
    pxlist.append(input().split(" "))

for i in range(Q):
    for j in range(N-1):
        if pxlist[i][0] == ablist[j][0]:
            Answer.insert(int(ablist[j][0]),+int(pxlist[i][1]))            
            Answer.insert(int(ablist[j][1]),+int(pxlist[i][1]))

for i in range(N-1):
    print(str(Answer[i])+" ")
print(str(Answer[N]))

Submission status: Runtime Error

Solution code:

Answer = []
NQ = input().split(" ")
#NQ[0]が頂点数N　NQ[1]が操作回数Q
N = int(NQ[0])
Q = int(NQ[1])

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

pxlist = []
for i in range(Q):
    pxlist.append(input().split(" "))

for i in range(Q):
    for j in range(N):
        if int(pxlist[i[0]]) == int(ablist[j[0]]):
            Answer.insert(int(ablist[j[0]]),+int(pxlist[i[1]]))            
            Answer.insert(int(ablist[j[1]]),+int(pxlist[i[1]]))

for i in range(N-1):
    print(str(Answer[i])+" ")
print(str(Answer[N]))

Submission status: Runtime Error

Solution code:
import sys


def main():
    
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    sys.setrecursionlimit(200000)
    rl = sys.stdin.readline

    n, q = [int(x) for x in rl().split()]

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

    cnt = [0] * (n + 1)
    for _ in range(q):
        p, x = [int(x) for x in rl().split()]
        cnt[p] += x

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(200000)


def main():
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    n, q = [int(x) for x in sys.stdin.readline().split()]

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

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

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

Submission status: Runtime Error

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(200000)


def main():
    def walk(p, c):
        if visited[p]:
            return
        visited[p] = True
        cnt[p] += c
        for n in tree[p]:
            walk(n, cnt[p])

    n, q = [int(x) for x in stdin.readline().split()]

    tree = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a, b = [int(x) for x in stdin.readline().split()]
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)
    for _ in range(q):
        p, x = [int(x) for x in stdin.readline().split()]
        cnt[p] += x

    visited = [False] * (n + 1)
    walk(1, 0)

    print(*cnt[1:])


main()

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())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)

        maxmap = [None] * N
        map_b = [None] * N
        def trav(a, x):
            map_b[x] = a
            if verts[a] is None:
                return x
            for i in verts[a]:
                maxmap[i] = x = trav(i, x + 1)
            return x

        maxmap[0] = trav(0, 0)

        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            if q == 0:
                for i in range(N):
                    results[i] += x
            elif verts[q] is not None:
                for i in range(map_b.index(q), maxmap[q] + 1):
                    results[i] += x
            else:
                results[q] += x
        print(" ".join(map(str, results)))
    except EOFError:
        break

Submission status: Runtime Error

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

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


def add_count(G, C, v):
    for child in G[v]:
        C[child] += C[v]
        add_count(G, C, child)


def main():
    G = [[] for _ in range(N)]
    C = [0 for _ in range(N)]
    for _ in range(N-1):
        p, c = map(int, input().split())
        G[p-1].append(c-1)

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

    add_count(G,C, 0)

    print(*C)

main()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
ki = {1:0}
for i in range(N-1):
    a, b = map(int, input().split())
    ki[b] = a

ki2 = [[] for i in range(N)]
for i in range(1, N+1):
    ki2[i-1].append(i-1)
    n = ki[i]
    while n > 0:
        ki2[n-1].append(i-1)
        n = ki[n]

res = [0 for i in range(N)]
for i in range(Q):
    a, b = map(int, input().split())
    for j in ki2[a-1]:
        res[j] += b

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

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)

ans = []
lines_dict =None
d=None
def main():
    global AB, PX
    if 'LOCAL' in os.environ:
        N = 2* 10** 5
        Q = 2 * 10 ** 5
        AB = [[i, i + 1] for i in range(1, N)]
        PX = [[i, 10] for i in range(Q)]
    else:
        N, Q = LI()
        AB = {}
        PX = {}
        for i in range(N-1):
            #  AB.append(LI())
            a, b = LI()
            if a not in AB.keys():
                AB[a] = set()
            if b not in AB.keys():
                AB[b] = set()
            AB[a].add(b)
            AB[b].add(a)

        for i in range(Q):
            p, x = LI()
            if p not in PX.keys():
                PX[p] = 0
            PX[p] += x
        perr(AB)
        perr(PX)
        dfs(1, -1)
        for i in sorted(PX.keys()):
            print(PX[i], end=' ')
        #  ans = [0 for _ in range(N)]

def dfs(parent, parent_parent):
    perr(PX)
    for node in AB[parent]:
        if node != parent_parent:
            perr(node)
            if node not in PX.keys():
                PX[node] = 0
            PX[node] += PX[parent]
            dfs(node, parent)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N ,Q = map(int,input().split())
pd = {}
counter = []
result = [0 for i in range(N)]
for i in range(N-1):
  p,c = map(int,input().split())
  pd[c] = p
for i in range(Q):
  counter.append(list(map(int,input().split())))
for p,x in counter:
  result[p-1] += x
for i in range(1,N):
    result[i] += result[pd[i+1]-1]
for a in result:
  print(a,end=' ')

Submission status: Runtime Error

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

def isparent(n, t):
    while n > 1:
        if ki[n] == t:
            return True
        else:
            n = ki[n]
    return False

res = [0 for i in range(N)]
for i in range(Q):
    a, b = map(int, input().split())
    res[a-1] += b
    for j in range(a+1, N+1):
        if isparent(j, a):
            res[j-1] += b

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

Submission status: Runtime Error

Solution code:
# input
n, q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n-1)]
PX = [tuple(map(int, input().split())) for _ in range(q)]

# other_setting
child = [0] * n
parent = [0] * n
value = [0] * n

for i in range(n) : 
    child[i] = []

for a, b in AB : 
    child[a-1].append(b)
    parent[b-1] = a

# op_function
def op(a) : 
    if a != 1 : 
        value[a-1] += value[parent[a-1]-1]

    if child[a-1] != [] : 
        for c in child[a-1] : 
            op(c)
            
# initial_state(value)
for p, x in PX : 
    value[p-1] += x
    
# exe
op(1)

# print(child)
# print(parent)
print(*value)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2*10**5)
def parent(array,count,index,root):
    count[index-1]+=count[root-1]
    for i in array[index-1]:
        if i!=root:
            parent(array,count,i,index)
num,q=map(int,input().split())
counter=[0 for _ in range(num)]
roots=[[] for _ in range(num)]
for _ in range(num-1):
    a,b=map(int,input().split())
    roots[a-1].apend(b)
    roots[b-1].apend(a)
for _ in range(q):
    p,x=map(int,input().split())
    counter[p-1]+=x
for ele in roots[0]:
    parent(roots,counter,ele,1)
print(" ".join([str(n) for n in counter]))

Submission status: Runtime Error

Solution code:
# input
n, q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n-1)]
PX = [tuple(map(int, input().split())) for _ in range(q)]

# other_setting
child = [0] * n
parent = [0] * n
value = [0] * n

for i in range(n) : 
    child[i] = []

for a, b in AB : 
    child[a-1].append(b)
    parent[b-1] = a

# op_function
def op(a) : 
    if a != 1 : 
        value[a-1] += value[parent[a-1]-1]

    if child[a-1] != [] : 
        for c in child[a-1] : 
            op(c)
            
# initial_state(value)
for p, x in PX : 
    value[p-1] += x
    
# exe
op(1)

print(*value)

Submission status: Runtime Error

Solution code:
# input
n, q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n-1)]
PX = [tuple(map(int, input().split())) for _ in range(q)]

# other_setting
child = [0] * n
parent = [0] * n
value = [0] * n

for i in range(n) : 
    child[i] = []

for a, b in AB : 
    child[a-1].append(b)
    parent[b-1] = a

# op_function
def op(a) : 
    if a != 1 : 
        value[a-1] += value[parent[a-1]-1]

    if child[a-1] != () : 
        for c in child[a-1] : 
            op(c)
            
# initial_state(value)
for p, x in PX : 
    value[p-1] += x
    
# exe
op(1)

print(*value)

Submission status: Runtime Error

Solution code:
# input
n, q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n-1)]
PX = [tuple(map(int, input().split())) for _ in range(q)]

# other_setting
child = [0] * n
parent = [0] * n
value = [0] * n

for i in range(n) : 
    child[i] = tuple()

for a, b in AB : 
    child[a-1] += tuple(str(b))
    parent[b-1] = a

# op_function
def op(a) : 
    if a != 1 : 
        value[a-1] += value[parent[a-1]-1]

    if child[a-1] != () : 
        for c in child[a-1] : 
            op(int(c))
            
# initial_state(value)
for p, x in PX : 
    value[p-1] += x
    
# exe
op(1)

print(*value)

Submission status: Runtime Error

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

ab_list = [list(map(int,list(input().split()))) for _ in range(N-1)]  
px_list = [list(map(int,list(input().split()))) for _ in range(Q)]  

graph = [[] for _ in range(N+1)]
point_list = [0] * (N+1)

for a, b in ab_list:
    graph[a].append(b)
    graph[b].append(a)
    
for p, x in px_list:
    point_list[p] += x
    
def add_point(num, root, point):
    point_list[num] += point
    for i in graph[num]:
        if i == root:
            continue
        add_point(i, num, point_list[num])

add_point(1, 0, 0)
 
print(*(point_list[1:]))

Submission status: Runtime Error

Solution code:
def dfs(v,p=-1):
  for u in to[v]:
    if u==p:
      continue
    ans[u]+=ans[v]
    dfs(u,v)


n,q=map(int,input().split())
to=[[] for i in range(10**6)]
ans=[0 for i in range(10**6)]
for i in range(n-1):
  aa,bb=map(lambda x:int(x)-1,input().split())
  to[aa].append(bb)
  to[bb].append(aa)
 
for i in range(q):
  p,x=map(int,input().split())
  p-=1
  ans[p]+=x

dfs(0)

print(' '.join([str(ans[i]) for i in range(n)]))

#print(to)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())

nodes = [-1]*N # 親ノードを保存
counts = [0]*N
for _ in range(N-1):
    a, b = input().split()
    if nodes[int(b)-1] != -1:
        raise ValueError("error!")
    nodes[int(b)-1] = int(a)-1

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

for i in range(N):
    if nodes[i] == -1:
        continue
    counts[i] += counts[nodes[i]]

print(*counts)

Submission status: Runtime Error

Solution code:
# input
n, q = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n-1)]
PX = [tuple(map(int, input().split())) for _ in range(q)]

# other_setting
child = [0] * n
parent = [0] * n
value = [0] * n

for i in range(n) : 
    child[i] = tuple()

for tmp1, tmp2 in AB : 
    if tmp1 < tmp2 : 
        child[tmp1-1] += tuple(str(tmp2))
        parent[tmp2-1] = tmp1
    else : 
        child[tmp2-1] += tuple(str(tmp1))
        parent[tmp1-1] = tmp2

# op_function
def op(a) : 
    value[a-1] += value[parent[a-1]-1]

    if child[a-1] != () : 
        for c in child[a-1] : 
            op(int(c))
            
# initial_state(value)
for p, x in PX : 
    value[p-1] += x
    
# exe
op(1)

print(*value)

Submission status: Runtime Error

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

nodes = [-1]*N # 親ノードを保存
counts = [0]*N
for _ in range(N-1):
    a, b = input().split()
    if nodes[int(b)-1] != -1:
        raise ValueError("error!")
    nodes[int(b)-1] = int(a)-1

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

for i in range(N):
    if nodes[i] == -1:
        continue
    counts[i] += counts[nodes[i]]

for count in counts:
    print(count, end=" ")

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(2*10**5)
n,q=map(int,input().split())
graph=[[] for _ in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
prev=[-1]*n
prev[0]=0
def f(n):
    for i in graph[n]:
        if prev[i]==-1:
            prev[i]=n
            f(i)
f(0)
counter=[0]*n
for _ in range(q):
    p,x=map(int,input().split())
    counter[p-1]+=x
score=[0]*n
score[0]=counter[0]
stack=[0]
while stack!=[]:
    memo=[]
    for i in stack:
        for j in graph[i]:
            if j!=prev[i]:
                score[j]=score[i]+counter[j]
                memo.append(j)
    stack=memo
print(" ".join(score))

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)

if 'LOCAL' in os.environ:
    N = 2* 10** 5
    Q = 2 * 10 ** 5
    AB = [[i, i + 1] for i in range(1, N)]
    PX = [[i, 10] for i in range(Q)]
else:
    N, Q = LI()
    AB = []
    PX = []
    for i in range(N-1):
        #  AB.append(LI())
        AB.append(LI())

    for i in range(Q):
        PX.append(LI())


"""
そもそも各頂点の部分木をdictに入れましょうか？
参照するような形で。
2には3
3には4,5
4,5はNoneだと、参照しやすいか？

カウントを若い順にやっていく
"""
#  AB = sorted(AB, key=lambda x: x[0])
#  perr('AB')
#  perr(AB)
d = {}
for i in range(N+1):
    d[i] = 0
for p, x in PX:
    if p not in d.keys():
        d[p] = 0
    d[p] += x
perr(d)
"""
根からスタートするとは？
1-5-3とかっていう組み合わせがあるため、dfsで根から辿っていく必要がある

"""
ans = [0 for _ in range(N)]
ans[0] = d[1]

lines_dict = {}
for ab in AB:
    if ab[0] not in lines_dict.keys():
        lines_dict[ab[0]] = [ab[1]]
    else:
        lines_dict[ab[0]].append(ab[1])

    if ab[1] not in lines_dict.keys():
        lines_dict[ab[1]] = [ab[0]]
    else:
        lines_dict[ab[1]].append(ab[0])

pdb.set_trace()
perr(lines_dict)
#  exit()


def dfs(current_node, val, parent_node):
    for other_node in lines_dict[current_node]:
        if other_node == parent_node:
            continue
        # まだ訪れていないので訪問
        new_val = val + d[other_node]
        ans[other_node-1] = new_val
        dfs(other_node, new_val, current_node)


    #  new_AB = []
    #  target_lines = []
    #  for ab in AB:
    #      if current_node in ab and ab[0] not in done_list and ab[1] not in done_list:
    #          target_lines.append(ab)
    #      else:
    #          new_AB.append(ab)
    #  target_lines = [ab for ab in AB if current_node in ab and ab[0] not in done_list and ab[1] not in done_list]
    #  if current_node not in lines_dict.keys():
    #      return
    #  target_lines = [num for num in lines_dict[current_node] if num not in done_list]
    #  done_list.append(current_node)
    #  for to_node in target_lines:
    #      perr(to_node)
    #      ans[to_node-1] = val + d[to_node]
    #      dfs(to_node, ans[to_node-1], done_list)


dfs(1, d[1], None)
perr(ans)
print(' '.join(map(lambda x: str(x), ans)))

Submission status: Runtime Error

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

def op(a, x) : #初期の根の値と, 何を足していくか
    value[a-1] += x

    if tree[a-1] != () : 
        for i in tree[a-1] : 
            op (int(i), x)
            
    # else : 
    #     for j in range(N) : 
    #         print(value[j])

tree = [0] * N
value = [0] * N

for i in range(N) : 
    tree[i] = tuple()
    
for i in range(N-1) : 
    tmp1, tmp2 = map(int, input().split())
    if tmp1 < tmp2 : 
        a = tmp1
        b = tmp2
    else : 
        a = tmp2
        b = tmp1
        
    tree[a-1] += tuple(str(b))
    
for i in range(Q) : 
    a, x = map(int, input().split())
    # print(a, x)
    op(a, x)
    
# print("tree")    
# for i in range(N) : 
#     print(tree[i])
    
# print("value")    
# for i in range(N) : 
#     print(value[i])

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

Submission status: Runtime Error

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

def op(a, x) : #初期の根の値と, 何を足していくか
    value[a-1] += x

    if tree[a-1] != () : 
        for i in tree[a-1] : 
            op (int(i), x)
            
    # else : 
    #     for j in range(N) : 
    #         print(value[j])

tree = [0] * N
value = [0] * N

for i in range(N) : 
    tree[i] = tuple()
    
for i in range(N-1) : 
    a, b = map(int, input().split())
    tree[a-1] += tuple(str(b))
    
for i in range(Q) : 
    a, x = map(int, input().split())
    # print(a, x)
    op(a, x)
    
# print("tree")    
# for i in range(N) : 
#     print(tree[i])
    
# print("value")    
# for i in range(N) : 
#     print(value[i])

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

Submission status: Runtime Error

Solution code:
import sys
inputs = sys.stdin.readlines()

from collections import deque, defaultdict

def dfs_generator(nodes, start):
    nexts = deque([start])
    while nexts:
        node = nexts.popleft()
        nexts.extend(nodes[node]['childs'])
        yield node

nodes = dict({1: dict({'val': 0, 'childs': []})})
for x in inputs[1:n]:
    from_, to_ = map(int, x.split())
    nodes[from_]['childs'].append(to_)
    nodes[to_] = dict({'val': 0, 'childs': []})
'''
subtree_roots = defaultdict(int)
for x in inputs[n:]:
    n, v = map(int, x.split())
    subtree_roots[n] += v

subtrees = defaultdict(list)
for r, v in subtree_roots.items():
    gen = dfs_generator(nodes, r)
    try:
        while True:
            node = next(gen)
            subtrees[r].append(node) 
    except StopIteration:
        pass
    for n in subtrees[r]:
        nodes[n]['val'] += v

for i in range(1, n):
    print(str(nodes[i]['val']), end=' ')
print(str(nodes[i+1]['val']))
'''