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
input = sys.stdin.readline

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

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

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

Submission status: Runtime Error

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

N, Q = map(int, input().split())
edges = [None]*N
edges[0] = 0
for _ in range(N-1):
    a, b = map(int, input().split())
    edges[max(a, b)-1] = min(a, b)-1

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

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

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

t = dict()
c = dict()
for i in range(n):
    t[i] = []
    c[i] = 0

for a, b in ab:
    t[a-1].append(b-1)
    t[b-1].append(a-1)

for p, x in px:
    c[p-1] += x

ans = [0] * n

def func(x, v, p):
    w = v + c[x]
    ans[x] = w
    for s in t[x]:
        if s != p:
            func(s, w, x)
    return

func(0,0,0)

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

Submission status: Runtime Error

Solution code:
class Node(object):
    def __init__(self, vertex, counter):
        self.vertex = vertex
        self.counter = counter
        self.siblings = []


class Tree(object):
    def __init__(self, root):
        self.root = Node(root, 0)
        self.size = 1

    def add(self, target, value):
        node = self.find_node(target)
        node.siblings.append(Node(value, 0))
        self.size += 1

    def find_node(self, target):
        queue = [self.root]
        while queue:
            q = queue.pop(0)
            if q.vertex == target:
                return q
            for n in q.siblings:
                queue.append(n)
        return None

    def increment(self, target, count):
        node = self.find_node(target)
        stack = [node]
        while stack:
            sb = stack.pop()
            sb.counter += count 
            for i in sb.siblings:                    
                stack.append(i)

            

l1 = list(map(int, input().split()))
n = l1[0]
q = l1[1]
tree = Tree(1)
# build tree
for i in range(n - 1):
    li = list(map(int, input().split()))
    target = li[0]
    value = li[1]
    tree.add(target, value)

# operation
for i in range(q):
    li = list(map(int, input().split()))
    target = li[0]
    counter = li[1]
    tree.increment(target, counter)

queue = [tree.root]
result = [''] * tree.size
result[0] = str(tree.root.counter)
while queue:
    q = queue.pop(0)
    for n in q.siblings:
        result[n.vertex - 1] = str(n.counter)
        queue.append(n)
print(' '.join(result))

Submission status: Runtime Error

Solution code:
class Node(object):
    def __init__(self, vertex, counter):
        self.vertex = vertex
        self.counter = counter
        self.siblings = []


class Tree(object):
    def __init__(self, root):
        self.root = Node(root, 0)
        self.size = 1

    def add(self, target, value):
        node = self.find_node(target)
        node.siblings.append(Node(value, 0))
        self.size += 1

    def find_node(self, target):
        queue = [self.root]
        while queue:
            q = queue.pop(0)
            if q.vertex == target:
                return q
            for n in q.siblings:
                queue.append(n)
        return None

    def increment(self, target, count):
        node = self.find_node(target)
        stack = [node]
        while stack:
            sb = stack.pop()
            sb.counter += count 
            for i in sb.siblings:                    
                stack.append(i)

            

l1 = list(map(int, input().split()))
n = l1[0]
q = l1[1]
tree = Tree(1)
# build tree
for i in range(n - 1):
    li = list(map(int, input().split()))
    print(li)
    target = li[0]
    value = li[1]
    tree.add(target, value)

# operation
for i in range(q):
    li = list(map(int, input().split()))
    print(li)
    target = li[0]
    counter = li[1]
    tree.increment(target, counter)

queue = [tree.root]
result = [''] * tree.size
result[0] = str(tree.root.counter)
while queue:
    q = queue.pop(0)
    for n in q.siblings:
        result[n.vertex - 1] = str(n.counter)
        queue.append(n)
print(' '.join(result))

Submission status: Runtime Error

Solution code:
class Node(object):
    def __init__(self, vertex, counter):
        self.vertex = vertex
        self.counter = counter
        self.siblings = []


class Tree(object):
    def __init__(self, root):
        self.root = Node(root, 0)
        self.size = 1
        
    def add(self, target, value):
        node = self.find_node(target)
        node.siblings.append(value)
        self.size += 1
        
    def find_node(self, target):
        queue = [self.root]
        while queue:
            q = queue.pop(0)
            for n in q.siblings:
                if n.vertex == target: 
                    return n
                queue.append(n)
        return None
    
    def increment(self, target, count):
        node = self.find_node(target)
        node.counter += count
        for n in node.siblings:
            n.counter += count


l1 = list(map(int, input().split()))
n = l1[0]
q = l1[1]
tree = Tree(1)
# build tree
for i in range(n):
    li = list(map(int, input().split()))
    target = li[0]
    value = li[1]
    tree.add(target, value)

# operation
for i in range(q):
    li = list(map(int, input().split()))
    target = li[0]
    counter = li[1]
    tree.increment(target, counter)

queue = [tree.root]
result = [] * tree.size
while queue:
    q = queue.pop(0)
    for n in q.siblings:
        result[n.vertex - 1] = n.counter
        queue.append(n)
print(' '.join(result))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
vec = [ [] for _ in range(N)]
for _ in range(N-1) :
    u, v = map(int, input().split())
    vec[u-1].append(v-1)
links= []
num = [0] * N
#print(num)

def update(vec,t,a):
  a.extend(vec[t])
  v = vec[t]
  for i in v:
    a = update(vec,i,a)
  return a


for i in range(N):
  a = []
  s = update(vec,i,a)
  links.append(s)

for _ in range(Q) :
  p, x = map(int, input().split())
  num[p-1] += x
  su = links[p-1]
  for i in su:
    num[i] += x
#print(vec)
#print(com)

#print(links)

  
for i in range(len(num)):
  print(num[i],"",end = "")

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
vec = [ [] for _ in range(N)]
for _ in range(N-1) :
    u, v = map(int, input().split())
    vec[u-1].append(v-1)
links= []
num = [0] * N
#print(num)
 
for i in range(N):
  a = []
  s = update(vec,i,a)
  links.append(s)
def update(vec,t,a):
  a.extend(vec[t])
  v = vec[t]
  for i in v:
    a = update(vec,i,a)
  return a
for _ in range(Q) :
    p, x = map(int, input().split())
 
  　num[p] += x
  　su = links[p]
　　for i in su:
      num[i] += x
#print(vec)
#print(com)

#print(links)
 
for t in range(len(com)):
  p,x = com[t]
  num[p] += x
  su = links[p]
  for i in su:
    #print(i)
    num[i] += x
  
for i in range(len(num)):
  print(num[i],"",end = "")

Submission status: Runtime Error

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

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

pxlist = [list(map(int,input().split())) for _ in range(Q)]

for q,x in pxlist:
    anslist[q-1] += x

def dfs(nownode,oya=-1):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        anslist[nextnode] += anslist[nownode]
        dfs(nextnode,nownode)
dfs(0,-1)
print(*anslist)

Submission status: Runtime Error

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

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

pxlist = [list(map(int,input().split())) for _ in range(Q)]

for q,x in pxlist:
    anslist[q-1] += x

def dfs(nownode,oya=-1):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        anslist[nextnode] += anslist[nownode]
        dfs(nextnode,nownode)
dfs(0,-1)
print(*anslist)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 

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

T = [[]for i in range(N)]

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

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

# def dfs(x, p):
#     for j in T[x]:
#         if j == p:
#             continue
#         X[j] += X[x]
#         dfs(j, x)
def dfs(nownode,oya):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        X[nextnode] += X[nownode]
        dfs(nextnode,nownode)

dfs(0, -1)

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 

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

T = [[]for i in range(N)]

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

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

# def dfs(x, p):
#     for j in T[x]:
#         if j == p:
#             continue
#         X[j] += X[x]
#         dfs(j, x)
def dfs(nownode,oya=-1):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        anslist[nextnode] += anslist[nownode]
        dfs(nextnode,nownode)

dfs(0, -1)

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

Submission status: Runtime Error

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

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

pxlist = [list(map(int,input().split())) for _ in range(Q)]

for q,x in pxlist:
    anslist[q-1] += x

def dfs(nownode,oya=-1):
    for nextnode in ablist[nownode]:
        if oya == nextnode:
            continue
        anslist[nextnode] += anslist[nownode]
        dfs(nextnode,nownode)
dfs(0)
print(*anslist)

Submission status: Runtime Error

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

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)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

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

#print(graph)

def dfs(x,parent,add):
    values[x]+=add
    for i in graph[x]:
        if i==parent:
            continue
        dfs(i,x,values[x])

dfs(1,0,0)

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

Submission status: Runtime Error

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


n,q=input()
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)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

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

#print(graph)

def dfs(x,parent,add):
    values[x]+=add
    for i in graph[x]:
        if i==parent:
            continue
        dfs(i,x,values[x])

dfs(1,0,0)

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

Submission status: Runtime Error

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


n,q=input()
AB=[[int(x) for x in input()] for _ in range(n-1)]
PX=[[int(x) for x in input()] for _ in range(q)]

graph=[[] for _ in range(n+1)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

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

#print(graph)

def dfs(x,parent,add):
    values[x]+=add
    for i in graph[x]:
        if i==parent:
            continue
        dfs(i,x,values[x])

dfs(1,0,0)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
ablist = [[] for _ in range(N+1)]
anslist = [0]*(N+1)

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

pxlist = [list(map(int,input().split())) for _ in range(Q)]

for q,x in pxlist:
    anslist[q] += x

def dfs(nownode,oya=-1):
    for b in ablist[nownode]:
        if oya == b:
            continue
        anslist[b] += anslist[nownode]
        dfs(b,nownode)
dfs(1)
print(' '.join(map(str,anslist[1:])))

Submission status: Runtime Error

Solution code:
import sys
 
stdin=sys.stdin
 
ni=lambda:int(ns())
na=lambda:list(map(int,stdin.readline().split()))
ns=lambda:stdin.readline().rstrip()  # ignore trailing spaces


n,q=na()
AB=[[int(x) for x in na()] for _ in range(n-1)]
PX=[[int(x) for x in na()] for _ in range(q)]

graph=[[] for _ in range(n+1)]
values=[0]*(n+1)

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

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

#print(graph)

def dfs(x,parent,add):
    values[x]+=add
    for i in graph[x]:
        if i==parent:
            continue
        dfs(i,x,values[x])

dfs(1,0,0)

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

Submission status: Runtime Error

Solution code:
N,Q = map(int,input().split())
ablist = [[] for _ in range(N+1)]
anslist = [0]*(N+1)

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

pxlist = [list(map(int,input().split())) for _ in range(Q)]

for q,x in pxlist:
    anslist[q] += x

def dfs(nownode,oya=-1):
    for b in ablist[nownode]:
        if oya == b:
            continue
        else:
            anslist[b] += anslist[nownode]
            dfs(b,nownode)
dfs(1)
print(' '.join(map(str,anslist[1:])))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
#ns = sorted(ns)
 
flags = [False]*N
dic = [[] for _ in range(N)]
rlt = [0]*N
 
for i in range(N-1):
  dic[ns[i][1]-1] += [ns[i][0]-1]
  dic[ns[i][0]-1] += [ns[i][1]-1]
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  flags[k] = Ture
  for l in dic[k]:
    if flags[l]:
      continue
    else:
      rlt[l] += rlt[k]
      flags[l] = Ture
    
print(" ".join(map(str, rlt)))

Submission status: Runtime Error

Solution code:
def resolve():
    n, q = map(int, input().split())
    ab = sorted(list(list(map(int, input().split())) for _ in range(n - 1)))
    px = list(list(map(int, input().split())) for _ in range(q))
    counters = [0] * n
    for p, x in px:
        counters[p - 1] += x
    for i in range(2, n + 1):
        parent = list(filter(lambda x: x[1] == i, ab))[0][0]
        counters[i - 1] += counters[parent - 1]
    print(" ".join([str(c) for c in counters]))


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
# ABC138D - Ki
import sys
input = sys.stdin.readline

def dfs(v, p, tree, cnt):
    for u in tree[v]:
        if u != p:
            cnt[u] += cnt[v]
            dfs(u, v, tree, cnt)


def main():
    N, Q = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(N - 1))
    P = tuple(tuple(map(int, input().split())) for _ in range(Q))
    T = [[] for _ in range(N + 1)]  # T[i] := children of vertex i
    cnt = [0] * (N + 1)
    for a, b in A:  # set tree relations
        T[a] += [b]
        T[b] += [a]
    for p, x in P:  # increment on roots
        cnt[p] += x
    dfs(1, -1, T, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())

ct = [0] * (n + 1)
e = dict()

for _ in range(n - 1):
    a, b = map(int, input().split())
    if e.get(a) is None:
      e[a] = 0
    e[a] += b

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

target = [1]
while len(target):
    t = target.pop()
    ct[t] += qs[t]
    for k in e:
        nt = e[k]
        target.append(nt)
        ct[nt] += ct[t]

print(*ct[1:])

Submission status: Runtime Error

Solution code:
# ABC138D - Ki
# https://atcoder.jp/contests/abc138/submissions/7012991
import sys
input = sys.stdin.readline

class Node:
    def __init__(self, parent=None):
        self.val = 0
        self.parent = parent


def main():
    N, Q = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(N - 1))
    P = tuple(tuple(map(int, input().split())) for _ in range(Q))
    T = [Node() for _ in range(N)]  # tree
    for a, b in A:  # set tree relationships
        T[b - 1].parent = a - 1
    for p, x in P:  # increment on roots
        T[p - 1].val += x
    for i in range(1, N):  # increment to children
        j = T[i].parent
        T[i].val += T[j].val
    print(*[node.val for node in T])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

#n = int(input())
def main():
    n,q = [int(i) for i in readline().split()]
    assert (n,q) != (4,3)
    
    g = [[] for _ in range(n)]
    for i in range(n-1):
        a,b = [int(i) for i in readline().split()]
        g[a-1].append(b-1)
        g[b-1].append(a-1)

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

    ans = [0]*n
    #ans[0] = add[0]

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

    dfs(0,-1,0)

#print(add)

    print(*ans)


main()

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
ans = [0] * n
ki = {}
for _ in range(n - 1):
  a, b = map(int, input().split())
  if not ki.get(a):
    ki[a] = [b]
  else:
    ki[a] = ki[a].append(b)

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

for e, f in ki.items():
  for g in f:
    ans[g - 1] += ans[e - 1]

for h in ans:
  print(h, end=' ')

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
import numpy as np
tree = np.eye(N, dtype=np.int)
for _ in range(N-1):
  a, b = map(int, input().split())
  tree[a-1, b-1]=1
  tree[b-1, a-1]=1
for i, link in enumerate(tree[:0:-1]):
  tree[link[:-(i+1)]==1] += link
tree = np.where(tree==0, 0, 1)
counter = np.zeros(N, np.int)
for _ in range(Q):
  p, x = map(int, input().split())
  counter[p-1:] += tree[p-1, p-1:]*x
string=""
for c in counter:
  string += "{} ".format(c)
print(string)

Submission status: Runtime Error

Solution code:
# 深さ優先探索で各直線を考える
def dfs(parent):
    for child in tree[parent]:
        # 累積和の計算
        ans[child] += ans[parent]
        dfs(child)


n, q = map(int, input().split())
# tree[i]: 頂点i+1の子どもたち
tree = [[] for i in range(n)]
ans = [0] * n

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

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

# 根ノードの頂点1から開始
dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
r = [[] for i in range(n)]
v = [0 for i in range(n)]

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

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


bfs = [0]
visit = [False in range(n)]
while len(bfs) != 0:
    tmp = []
    for i in bfs:
        for y in r[i]:
            if not visit[y]:
                v[y] += v[i]
                tmp.append(y)
        visit[i] = True
    bfs = tmp

#print(" ".join(map(str,v)))
print(*v)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
AB = []
ans = [0]*n
for i in range(n-1):
  a,b = map(int, input().split())
  AB.append([a,b])
tmp = [[] for _ in range(n)]
tmp[0].append(1)
for i in range(n-1):
  tmp[AB[i][0]].append(AB[i][1])
def add_num(num, cur):
  ans[cur-1] += num
  if len(tmp[cur-1]) <= 0:
    return
  for i in tmp[cur]:
    add_num(num, i)
PX = [list(map(int, input().split())) for _ in range(q)]
for px in PX:
  add_num(px[1], px[0])
print(*ans)

Submission status: Runtime Error

Solution code:
# 深さ優先探索で各直線を考える
def dfs(parent):
    for child in tree[parent]:
        # 累積和の計算
        ans[child] = operate[child] + ans[parent]
        dfs(child)


n, q = map(int, input().split())
# tree[i]: 頂点i+1の子どもたち
tree = [[] for i in range(n)]
# 操作を行う際に根として指定する頂点と加算する値を保持
# ex. 出力例1において、2 を根とする部分木に 100 加算する場合は
# operate = [0, 100, 0, 0] とする
operate = [0] * n
ans = [0] * n

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

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

# 累積和の初期値を設定
ans[0] = operate[0]
# 根ノードの頂点1から開始
dfs(0)

print(*ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 

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

T = [[]for i in range(N)]

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

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

ans = [X[i] for i in range(N)]

def dfs(x, p):
    for j in range(len(T[x])):
        if j == p:
            continue
        ans[T[x][j]] += ans[x]
        dfs(T[x][j])

dfs(0)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from decimal import Decimal
from collections import defaultdict

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

tree = defaultdict(set)
values = defaultdict(int)

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

def add_px(p, x):
    for node in tree[p]:
        values[node] += x
        if tree[node]:
            add_px(node, x)

for n in range(Q):
    p, x = map(int, input().split())
    values[p] += x
    add_px(p, x)

print(" ".join([str(int(v)) for v in sorted(values.values())]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from decimal import Decimal
from collections import defaultdict

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

tree = defaultdict(set)
values = defaultdict(int)

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


P = []
X = []

def add_px(p, x):
    for node in tree[p]:
        values[node] += x
        if tree[node]:
            add_px(node, x)

for n in range(Q):
    p, x = map(int, input().split())
    values[p] += x
    add_px(p, x)

print(" ".join([str(int(v)) for v in sorted(values.values())]))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from decimal import Decimal
from collections import defaultdict

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

tree = defaultdict(set)
values = defaultdict(Decimal)

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


P = []
X = []

for n in range(Q):
    p, x = map(int, input().split())
    def add_px(p, x):
        for node in tree[p]:
            values[node] += x
            if tree[node]:
                add_px(node, x)

    values[p] += x
    add_px(p, x)

print(" ".join([str(int(v)) for v in sorted(values.values())]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, q = map(int, input().split())
counter = [0]*n
edges = {}

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

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

for i in range(1, n):
    counter[i] += counter[edges[i+1]-1]

print(*counter)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
 
N,Q = (int(i) for i in input().split())
hen = {}
value = [0] * N

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

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

def add(p,x):
        for node in hen[p]:
            value[node-1] = value[node-1] + x
            if node in hen:
                add(node,value[node-1])

#add(1,value[0])
for i in range(0,n):
    if i+1 in hen:
        for node in hen[i+1]:
            value[node-1] += value[i]

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

Submission status: Runtime Error

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

def dfs(v):
    for u in to[v]:
        ans[u] += ans[v]
        dfs(u)

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

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

dfs(0)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

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

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

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

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

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

Submission status: Runtime Error

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

def dfs(v, p = -1):
    for u in to[v]:
        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)

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:
def find_child(a, f, c=[]):
    for x in a[f]:
        c.append(x)
        find_child(a, x, c)
    return c

n, q = map(int, input().split())
a = [[] for _ in range(n+1)]
cached = [None] * (n+1)
vals = [0] * (n)
for _ in range(n-1):
    e, f = map(int, input().split())
    a[e].append(f)
for _ in range(q):
    e, f = map(int, input().split())
    if not cached[e]: cached[e] = find_child(a, e, [])
    vals[e-1] += f
    for t in cached[e]: vals[t-1] += f
print(' '.join(map(str, vals)))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
counter = [0]*n
edges = {}

def have(p, add_lst):
    if p not in edges.keys():
        return
    for i in edges[p]:
        add_lst += [i]
        have(i, add_lst)


for i in range(n-1):
    a, b = map(int, input().split())
    if a in edges.keys():
        edges[a] += [b]
    else:
        edges[a] = [b]

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

for i in range(n):
    print(counter[i], end=" ")

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())

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

def dfs(x, pre):
    global p
    global ki

    p[x] += p[pre]

    for i in ki[x]:
        if (i != pre):
            dfs(i, x)

dfs(1,0)

print(*(p[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,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 = list(map(int, input().split()))

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

def dfs(x, pre):
    global p
    global ki

    p[x] += p[pre]

    for i in ki[x]:
        if (i != pre):
            dfs(i, x)

dfs(1,0)

#print(*a)
print(*(p[1:]))

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)]
        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]
        print(*p)

if __name__ == '__main__':
        main()