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:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):

    answer = [0]*N
    linked_list = [[] for _ in range(N)]
    for i in range(N-1):
        linked_list[a[i]-1].append(b[i]-1)
        linked_list[b[i]-1].append(a[i]-1)
    
    c = [0]*N
    is_Visited = [False]*N

    for j in range(Q):
        c[p[j]-1] += x[j]
    answer[0] = c[0]
    
    def dfs(node,counter):
        counter += c[node]
        is_Visited[node] = True
        for n in linked_list[node]:
            if is_Visited[n] == False:
                answer[n] = dfs(n,counter)
        
        return counter
        
    dfs(0,0)
    print(*answer)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]"
    x = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.next_node = []

def dfs(root,x):
  tree_stack = [root]
  while len(tree_queue) > 0:
    node = tree_stack.pop(-1)
    node.val += x
    for n in node.next_node:
      tree_queue.append(n)

N,Q = (int(i) for i in input().split())

Nodes = []
for i in range(N):
  Nodes.append(TreeNode(0))

AB = []
for i in range(N-1):
  A,B = (int(i) for i in input().split())
  Nodes[A-1].next_node.append(Nodes[B-1])

PX = {}
for i in range(Q):
  P,X = (int(i) for i in input().split())
  PX[P] = PX.get(P,0) + X

for p,x in PX.items():
  dfs(Nodes[p-1],x)

ans = []
for node in Nodes:
  ans.append(node.val)

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

Submission status: Runtime Error

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

data2 = getSomeInputListInt(n-1)

data3 = getSomeInputListInt(q)

ans = np.zeros(n)

for i in range(len(data3)):
    ans[data3[i][0]-1] += data3[i][1]

treeList = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  treeList[a].append(b) # a<bとは限らないため、双方向にする
  treeList[b].append(a)

def treeTrace(crnt, prnt):
    for i in range(len(treeList[crnt])):
        if prnt == treeList[crnt][i]:
            continue
        ans[treeList[crnt][i]] += ans[crnt]

        treeTrace(treeList[crnt][i], crnt)

treeTrace(0, -1)

for s in ans:
    print(int(s), end =" ")

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
G=[[] for _ in range(N+1)]
for i in range(N-1):
    a,b=map(int,input().split())
    G[a].append(b)
#このような木に関する問題では、木の代わりに 1−2−3−...−N という直線的なグラフを考えると助け になることがある
cnt=[0]*(N+1)
from collections import deque
for _ in range(N-1):
    a,b=map(int,input().split())
    G[a].append(b)
    G[b].append(a)

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

queue=deque([1])
visited={1}
while queue:
    tmp=queue.popleft()
    visited.add(tmp)
    for t in G[tmp]:
        if t in visited:
            pass
        else:
            queue.append(t)
            cnt[t]+=cnt[tmp]

print(*cnt[1:])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
def dfs(node_i):
    is_visited[node_i] = 1
    for bi in tree[node_i]:
        if is_visited[bi]:
            continue
        tree_x[bi] += tree_x[node_i]
        dfs(bi)

N, Q = map(int, input().split())
# tree = [[] for i in range(N)]
tree = defaultdict(list)
tree_x = [0 for i in range(N)]
is_visited = [0]*N
for i in range(N-1):
    a, b = map(int, input().split())
    am1 = a-1
    bm1 = b-1
    tree[bm1].append(am1)
    tree[am1].append(bm1)

# print(tree)
for q in range(Q):
    p, x = map(int, input().split())
    tree_x[p-1] += x

dfs(0)

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

Submission status: Runtime Error

Solution code:
p=[]
for i in range(q):
    tmp = list(map(int,input().split()))
    p.append(tmp)

link = [[] for _ in range(n)]

for tmp in a:
    link[tmp[0]-1].append(tmp[1]-1)

from collections import defaultdict
d = defaultdict(int)

for tmp in p:
    d[tmp[0]-1]+=tmp[1]

from collections import deque
ans =[0]*n

Q = deque()
Q.append([0,0])

while Q:
    node= Q.popleft()

    val = node[1] + d[node[0]]
    ans[node[0]]+=val

    for nxt in link[node[0]]:
        Q.append([nxt,val])

print(*ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
arr = []
con = []
for _ in range(n):
    arr.append([])
    con.append(0)

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

#print(arr)

def add(arr,con,j):
    con[j-1] += x
    if len(arr[j-1])!=0:
        for i in arr[j-1]:
            add(arr,con,i)

for _ in range(q):
    p,x = map(int,input().split())
    add(arr,con,p)

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

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
arr = []
con = []
for _ in range(n):
    arr.append([])
    con.append(0)

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

#print(arr)

def add(arr,con,j):
    con[j-1] += x
    if len(arr[j-1])!=0:
        for i in arr[j-1]:
            add(arr,con,i)

for _ in range(q):
    p,x = map(int,input().split())
    add(arr,con,p)

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

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit
setrecursionlimit(100000)

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

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(*finalValues)

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit
setrecursionlimit(100000)

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

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(" ".join([str(s) for s in finalValues]))

Submission status: Runtime Error

Solution code:
S = input().split(" ")
N = int(S[0])
Q = int(S[1])

arr = []
prr = []
for i in range(N-1):
    S = input().split(" ")
    ar = [int(S[0]),int(S[1])]
    arr.append(ar)

for i in range(Q):
    S = input().split(" ")
    pr = [int(S[0]), int(S[1])]
    prr.append(pr)



def prepare(n, q, arr, prr):
    links = [[] for _ in range(n)]
    values = [0 for _ in range(n)]
    lineStates = [-1 for _ in range(n)]
    finalValues = [0 for _ in range(n)]
    for ar in arr:
        start = ar[0] - 1
        end = ar[1] - 1
        links[start].append(end)
        links[end].append(start)

    for pr in prr:
        i = pr[0] - 1
        x = pr[1]
        values[i] += x

    return links, values, lineStates, finalValues


links, values, lineStates, finalValues = prepare(N, Q, arr, prr)


def dfs(currentNode, arr, parrentAccumulate):
    childNodes = links[currentNode]
    parrentAccumulate += values[currentNode]
    finalValues[currentNode] = parrentAccumulate
    lineStates[currentNode] = 1
    for ch in childNodes:
        if lineStates[ch] == -1:
            dfs(ch, arr, parrentAccumulate)


dfs(0, links, 0)
print(" ".join([str(s) for s in finalValues]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
kolist=[]
plist=[]
answers=[]
for i in range(n):
    kolist.append([])
    plist.append(0)
    answers.append(0)
for i in range(n-1):
    a, b = map(int, input().split())
    kolist[a-1].append(b-1)

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

def kasan(now, plus):
    plus+=plist[now]
    answers[now]=plus
    if len(kolist[now])!=0:
        for i in range(len(kolist[now])):
            kasan(kolist[now][i], plus)
    return kolist[now], plus

kasan(0, 0)
#print(plist)
#print(answers)
for an in answers:
    print(an, end=" ")

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
to = [[] for _ in range(N)]
ans = [0 for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    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, -1)
print(*ans)

Submission status: Runtime Error

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

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

dfs(0)
print(*ans)

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 _ in range(200005)]
ans = [0 for _ in range(250000)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
for i in range(N):
    print(ans[i])

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 _ in range(20005)]
ans = [0 for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(*ans)

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 _ in range(200005)]
ans = [0 for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(*ans)

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 _ in range(200005)]
ans = [0] * N
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans[p] += x
dfs(0)
print(*ans)

Submission status: Runtime Error

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

ans = [-1] * n

def dfs(v, cnt):
    ans[v] = cnt + S[v]
    for i in G[v]:
        if ans[i] == -1: dfs(i, cnt + S[v])

dfs(0, 0)
print(*ans)

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)]
to=[[] for _ in range(N)]
co=[0 for _ in range(N)]

for a, b in ab:
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)
for p, x in px:
    p-=1
    co[p]+=x

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

dfs(0)
print(*co)

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)]
to=[[] for _ in range(N)]
co=[0 for _ in range(N)]

for a, b in ab:
    a-=1
    b-=1
    to[a].append(b)
    to[b].append(a)
for p, x in px:
    p-=1
    co[p]+=x

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

dfs(0)
print(co)

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, key, parent=None):
        self.key = key
        self.parent = parent
        self.children = []
        self.counter = 0

    def add_child(self, node):
        self.children.append(node)

    def add_counter(self, counter):
        self.counter += counter
        for child in self.children:
            child.add_counter(counter)


N, Q = map(int, input().split())
nodes = [Node(i) for i in range(1, N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    nodes[a-1].add_child(nodes[b-1])
for _ in range(Q):
    p, x = map(int, input().split())
    nodes[p-1].add_counter(x)
print(*[node.counter for node in nodes])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(4100000)

[n, q] = map(int, input().rstrip().split(" "))

tree = {}

def add_points(tree, key, point):
    params = tree[key]
    params["point"] = params["point"] + point
    for i in params["next"]:
        add_points(tree, i, point)

for i in range(n-1):
    [a, b] = map(int, input().rstrip().split(" "))
    if tree.get(a) is None:
        tree[a] = { "next": [b], "point": 0 } 
    else:
        tree[a]["next"].append(b)
    if tree.get(b) is None:
        tree[b] = { "next": [], "point": 0 }
    else:
        tree[b]["next"].append(b)

for i in range(q):
    [p, x] = map(int, input().rstrip().split(" "))
    add_points(tree, p, x)

print(" ".join(map(str, [v["point"] for v in tree.values()])))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n - 1)]
b = [list(map(int, input().split())) for _ in range(q)]
g = [[] for i in range(n + 1)]
counter = [0] * (n + 1)

for i, j in a:
    g[i].append(j)

def dfs(e,c,counter):
    counter[e]+=c
    for k in g[e]:
        dfs(k,c,counter)

for i,j in b:
    dfs(i,j,counter)
counter.pop(0)
print(*counter)

Submission status: Runtime Error

Solution code:
from queue import Queue
from collections import defaultdict

count = defaultdict(int)
tree = defaultdict(lambda: [])

n, q = map(int, input().split())
for _ in range(_q):
    p, x = map(int, input().split())
    count[p] += x
for _ in range(_n - 1):
    a, b = map(int, input().split())
    tree[a].append(b)

ret = [0] * n
q = Queue()
q.put((0, 1))
while not q.empty():
    f, c = q.get()
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        if n != f:
            q.put((c, n))

print(*ret)

Submission status: Runtime Error

Solution code:
from queue import Queue
from collections import defaultdict

def solve(n, q, AB, PX):
    count = defaultdict(int)
    for p, x in PX:
        count[p] += x

    tree = defaultdict(lambda: [])
    for a, b in AB:
        tree[a].append(b)

    ret = [0] * n
    q = Queue()
    q.put((1, 1))
    while not q.empty():
        f, c = q.get()
        ret[c - 1] = count[c] + ret[f - 1]
        for n in tree[c]:
            if f != c:
                q.put((c, n))

    return ret

_n, _q = map(int, input().split())
_AB = [map(int, input().split()) for _ in xrange(_n - 1)]
_PX = [map(int, input().split()) for _ in xrange(_q)]
print(*solve(_n, _q, _AB, _PX))

Submission status: Runtime Error

Solution code:
# coding: utf-8 
# Your code here!
import sys 
sys.setrecursionlimit(10000000000)

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

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

#print(l) 
def dfs(num,point,visited): 
    visited[num]=1 
    point+=plus[num] 
    
    ans[num]=point 
    while l[num][-1]!=num: 
        temp=l[num].pop(-1) 
        if visited[temp]==0: 
            dfs(temp,point,visited) 
            
    return 0 

for _ in range(N-1): 
    a,b=map(int,input().split()) 
    l[a-1].append(b-1) 
    l[b-1].append(a-1) 
    
ans=[0]*N 
plus=[0]*N 

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

dfs(0,0,[0]*N) 

print(*ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8 
# Your code here!
import sys 
sys.setrecursionlimit(40000)

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

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

#print(l) 
def dfs(num,point,visited): 
    visited[num]=1 
    point+=plus[num] 
    
    ans[num]=point 
    while l[num][-1]!=num: 
        temp=l[num].pop(-1) 
        if visited[temp]==0: 
            dfs(temp,point,visited) 
            
    return 0 

for _ in range(N-1): 
    a,b=map(int,input().split()) 
    l[a-1].append(b-1) 
    l[b-1].append(a-1) 
    
ans=[0]*N 
plus=[0]*N 

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

dfs(0,0,[0]*N) 

print(*ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N,Q=map(int,input().split())

l=[[i] for i in range(N)]
#print(l)

def dfs(num,point,visited):
    visited[num]=1
    point+=plus[num]
    ans[num]=point
    
    while l[num][-1]!=num:
        temp=l[num].pop(-1)
        if visited[temp]==0:
            dfs(temp,point,visited)
        
    return 0

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

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

Submission status: Runtime Error

Solution code:
from collections import deque
n, q = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

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

stack = deque(0)
seen = [False]*n
while len(stack) > 0:
    parent = stack.pop()
    seen[parent] = True
    for child in graph[parent]:
        if seen[child]:
            continue
        ans[child] += ans[parent]

print(*ans)

Submission status: Runtime Error

Solution code:

class Node:
    def __init__(self):
        self.next=[]
        self.ten=0
    def plus(self,xs):
        self.ten += xs
        for i in range(len(self.next)):
            self.next[i].plus(xs)
        


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

node=[]

for i in range(n):
    node.append(Node())

for i in range(n-1):
    a,b=map(int,input().split())
    node[a-1].next.append(node[b-1])
  
    
for i in range(q):
    p,x=map(int,input().split())
    node[p-1].plus(x)
    
for i in range(n):
    print(node[i].ten)

Submission status: Runtime Error

Solution code:
import Foundation

class Node{
    var next:[Node]=[]
    var ten:Int=0
    init() {
    }
    func plus(xs:Int){
        self.ten += xs
        if !self.next.isEmpty{
           // print("!self.next.isEmpty :",!self.next.isEmpty)
            for i in 0...self.next.count-1{
              //  print("i:",i)
                self.next[i].plus(xs:xs)
            }
        }
    }
}

func readInts() -> [Int] {
    return readLine()!.split(separator:" ").map { Int($0)! }
}

let n:[Int] = readInts()

var node:[Node]=[]
for i in 1...n[0]{
    node.append(Node())
}
for i in 1...n[0]-1{
    let a:[Int]=readInts()
    //print("a:",a)
    node[a[0]-1].next.append(node[a[1]-1])
   // print(node[a[0]-1].next)
}

for i in 1...n[1]{
    let p:[Int]=readInts()
  //  print("p:",p)
    node[p[0]-1].plus(xs:p[1])
}

node.map{print(($0).ten)}

Submission status: Runtime Error

Solution code:
def dfs(v):
  checked[v] = 1  
  for u in g[v]:    
    if checked[u]:
      continue
    ab.append([v,u])
    dfs(u)   

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

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

ab = []
checked = [0]*(n+1)
dfs(1)

cnt = [0]*(n+1)    
for i in range(q):
  p,x = map(int,input().split())
  cnt[p] += x
  
for a,b in ab:
  cnt[b] += cnt[a]      
  
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
import time
import sys

input = sys.stdin.readline

# start = time.time()
n, q = map(int, input().strip().split())
edges = [list(map(int, input().strip().split())) for _ in range(n - 1)]
values = [list(map(int, input().strip().split())) for _ in range(q)]

# elapsed = time.time() - start
# print("elapsed_time:{0}".format(elapsed) + "[sec]")

edge_dict = defaultdict(list)
value_dict = defaultdict(int)
total_value_dict = defaultdict(int)
visited = [False] * n

for a, b in edges:
    edge_dict[a].append(b)
    edge_dict[b].append(a)

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


qu = deque([1])
total_value_dict[1] = value_dict[1]
visited[0] = True

while len(qu) > 0:
    node = qu.popleft()
    next_nodes = [m for m in edge_dict[node] if not visited[m]]
    for m in next_nodes:
        total_value_dict[m] = total_value_dict[node] + value_dict[m]
        visited[m - 1] = True
        qu.append(m)

print(' '.join([str(total_value_dict[i]) for i in range(1, n + 1)]))

Submission status: Runtime Error

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

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

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

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

dfs(0,-1)
for a in ans: print(a)

Submission status: Runtime Error

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

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

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

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

dfs(0,0)
for a in ans: print(a)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

n, q = map(int, input().split())
v = [set() for _ in range(n+1)]
c = [0]*(n+1)

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

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

def addcount(p, prev):
  v[p].remove(prev)
  for i in v[p]:
    c[i] += c[p]
    addcount(i, p)
  return None
addcount(1, 0)
  
print(' '.join(str(i) for i in c[1:]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
v = [set() for _ in range(n+1)]
c = [0]*(n+1)

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

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

def addcount(p, prev):
  for i in v[p]:
    if i != prev:
      c[i] += c[p]
      addcount(i, p)
  return None
addcount(1, 0)
  
print(' '.join(str(i) for i in c[1:]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
v = [set() for _ in range(n+1)]
c = [0]*(n+1)

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

def clearpre(x):
  for i in v[x]:
    v[i].remove(x)
    clearpre(i)
clearpre(1)

def f(p, x):
  c[p] += x
  for i in v[p]:
    f(i, x)

for _ in range(q):
  p, x = map(int, input().split())
  f(p, x)
print(' '.join(str(i) for i in c[1:]))

Submission status: Runtime Error

Solution code:
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix
inf = float("inf")
N, Q = map(int, input().split())
graph = np.zeros((N, N))
for i in range(N - 1):
    a, b = map(int, input().split())
    graph[a - 1][b - 1] = 1
    graph[b - 1][a - 1] = 1
graph = csr_matrix(graph)
shortest_paths = shortest_path(graph, directed=False, method="D")
kaisou = [paths[0] for paths in shortest_paths]
ans = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    for i in range(N):
        if kaisou[i] > (p - 1) or i == (p - 1):
            ans[i] += x
for i in range(N):
    print(ans[i], end=" ")
print()

Submission status: Runtime Error

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

def dfs(v, p=-1):
    for u in to[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u, v)
    
def main():
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        to[a].append(b)
        to[b].append(a)
        
    for i in range(Q):
        p, x = map(int, input().split())
        ans[p-1] += x
        
    dfs(0)
    
    for a in ans: print(a)

if __name__ == '__main__':
    N, Q = map(int, input().split())
    to = [[] for i in range(200005)]
    ans = [0] * N
    
    main()

Submission status: Runtime Error

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

def dfs(v, p=-1):
    for u in to[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u, v)
    
def main():
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        to[a].append(b)
        to[b].append(a)
        
    for i in range(Q):
        p, x = map(int, input().split())
        ans[p-1] += x
        
    dfs(0)
    
    for a in ans: print(a)

if __name__ == '__main__':
    N, Q = map(int, input().split())
    to = [[] for i in range(N)]
    ans = [0] * N
    
    main()

Submission status: Runtime Error

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

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

if __name__ == '__main__':
    N, Q = map(int, input().split())
    to = [[] for i in range(N)]
    ans = [0] * N
    
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
N,Q = map(int, input().split())
G = [[] for _ in range(N)]
count = [0]*N
for i in range(N-1):
    a,b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(Q):
    p,x = map(int, input().split())
    count[p-1]+=x
q = deque([0])
flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for w in G[v]:
        if flag[w]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)