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())
T=[set() for i in range(N)]
S=[0 for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    T[a-1].add(b-1)
    T[b-1].add(a-1)
for i in range(0,Q):
    p,x=map(int,input().split())
    S[p-1]+=x
seen=[False for i in range(N)]
def dps(i):
    seen[i]=True
    for v in T[i]:
        if seen[v]:
            continue
        else:
            S[v]+=S[i]
            dps(v)
dps(0)
print(*S)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys

tree = defaultdict(list)
cnt = defaultdict(int)
lines = sys.stdin.readlines()
lines = [line.strip() for line in lines]
N, Q = int(lines[0].split(' ')[0]), int(lines[0].split(' ')[1])

for i in xrange(1, N):
    a, b = int(lines[i].split(' ')[0]), int(lines[i].split(' ')[1])
    tree[a].append(b)
    tree[b].append(a)
    
for i in xrange(N, N+Q):
    p, x = int(lines[i].split(' ')[0]), int(lines[i].split(' ')[1])
    cnt[p] += x

ans = [0 for _ in range(N)]
visited = set()
def dfs(node, val):
    visited.add(node)
    val += cnt[node]
    ans[node-1] += val
    if not node in tree:
        return
    nexts = tree[node]
    for nx_node in nexts:
        if nx_node not in visited:
            dfs(nx_node, val)
dfs(1, 0)
ans = [str(val) for val in ans]
print " ".join(ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
connect = [set() for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    connect[a-1].add(b-1)
    connect[b-1].add(a-1)

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

ans = [0] * n

def dfs(count, v, parent):
    count += counter[v]
    ans[v] = count
    for child in connect[v]:
        if child != parent:
            dfs(count, child, v)

dfs(0, 0, -1)

print(*ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
tree = defaultdict(list)
cnt = defaultdict(int)
N, Q = list(map(int, input().split()))
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    tree[a].append(b)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    cnt[p] += x

ans = [0 for _ in range(N)]
visited = set()
def dfs(node, val):
    visited.add(node)
    val += cnt[node]
    ans[node-1] += val
    if not node in tree:
        return
    nexts = tree[node]
    for nx_node in nexts:
        if nx_node not in visited:
            dfs(nx_node, val)
dfs(1, 0)
ans = [str(val) for val in ans]
print(" ".join(ans))

Submission status: Runtime Error

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

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

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

ans = [0] * N
visited = set()
def dfs(node, val):
    visited.add(node)
    val += cnt[node]
    ans[node-1] += val
    nexts = tree[node]
    for nx_node in nexts:
        if nx_node not in visited:
            dfs(nx_node, val)
dfs(1, 0)
ans = [str(val) for val in ans]
print(" ".join(ans))

Submission status: Runtime Error

Solution code:
import sys

n, q = map(int, sys.stdin.readline().split())
counter = [0 for _ in range(n)]
relation = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    relation[a-1].append(b-1)
operate = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

def dfs(start, point):
    visited[start] = True
    for node in relation[start]:
        if visited[node] == False:
            counter[node] += point
            dfs(node, point)

for ope in operate:
    start, point = ope
    start -= 1
    counter[start] += point
    visited = [False for _ in range(n)]
    dfs(start, point)

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

Submission status: Runtime Error

Solution code:
import sys

n, q = map(int, sys.stdin.readline().split())
counter = [0 for _ in range(n)]
relation = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    relation[a-1].append(b-1)
operate = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

def dfs(start, prev, point):
    visited[start] = True
    for node in relation[start]:
        if node == prev: continue
        if visited[node] == False:
            counter[node] += point
            dfs(node, start, point)

for ope in operate:
    start, point = ope
    start -= 1
    counter[start] += point
    visited = [False for _ in range(n)]
    dfs(start, -1, point)

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

Submission status: Runtime Error

Solution code:
def dfs(cur, par):
    for child in edge[cur]:
        if child == par:
            continue
        counter[child] += counter[cur]
        dfs(child, cur)


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)]
counter = [0] * N

edge = [[] for _ in range(N)]
for i in range(N-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    edge[a].append(b)
    edge[b].append(a)

for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    counter[p] += x


dfs(0, -1)
print(*counter)

Submission status: Runtime Error

Solution code:
def dfs(cur, par):
    for child in edge[cur]:
        if child == par:
            continue
        counter[child] += counter[cur]
        dfs(child, cur)


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)]
counter = [0] * N

edge = [[] for _ in range(N)]
for i in range(N-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    edge[a].append(b)

for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    counter[p] += x


dfs(0, -1)
print(*counter)

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def dfs(p, x):
    counter[p] += x
    if children[p]:
        for c in children[p]:
            dfs(c, x)


children = defaultdict(list)

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)]
counter = [0] * N

for i in range(N-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    children[a] += [b]
for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    dfs(p, x)
ans = []
for i in range(N):
    ans.append(counter[i])
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def dfs(p, x):
    counter[p] += x
    for c in children[p]:
        dfs(c, x)


children = defaultdict(list)

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)]
counter = [0] * N

for i in range(N-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    children[a] += [b]
for i in range(Q):
    p = px[i][0] - 1
    x = px[i][1]
    dfs(p, x)
ans = []
for i in range(N):
    ans.append(counter[i])
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def dfs(p, x):
    counter[p] += x
    for c in children[p]:
        dfs(c, x)


children = defaultdict(list)
counter = defaultdict(int)

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

for i in range(N-1):
    a = ab[i][0]
    b = ab[i][1]
    children[a] += [b]
for i in range(Q):
    p = px[i][0]
    x = px[i][1]
    dfs(p, x)
ans = []
for i in range(1, N+1):
    ans.append(counter[i])
print(' '.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, Q = MAP()
A = [LIST() for i in range(N-1)]
P = [LIST() for i in range(Q)]

tree = defaultdict(list)

# ノードの重み

cnt = [0] * N

# tree 隣接リスト
for i in range(N-1):
    tree[A[i][0]-1].append(A[i][1]-1)
    tree[A[i][1]-1].append(A[i][0]-1)

for i in range(Q):
    cnt[P[i][0]-1] += P[i][1]

ans = [0] * N
q = deque([])
# 始点
q.append(0)
# 各頂点を見たかどうかのフラグ
checked = [0] * N
checked[0] = 1

while q:
    v = q.popleft()
    checked[v] = 1
    for u in tree[v]:
        if checked[u] == 1:
            continue
        cnt[u] += cnt[v]
        q.append(u)
print(*cnt)

Submission status: Runtime Error

Solution code:
# グラフ木累積和
n, q = map(int, input().split())
tree = [[] for _ in range(n)]
ans = [0] * n

for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    tree[a].append(b)
    tree[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 tree[v]:
        # 親には戻らないようにする
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

dfs(0)
print(*ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, prev, pre):
  val[s] = Ope[s] + prev
  for i in G[s]:
    if(i != pre):
      dfs(i, val[s], s)
  return

#X = str(input()).split()
#a = [int(x) for x in input().split()]


N, Q = map(int, input().split())
G = [[] for i in range(N)]
Ope = [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)

val = [0]*N

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

dfs(0, 0, 0)

val = map(str, val)
vall = ' '.join(val)

print(vall)

Submission status: Runtime Error

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, prev, pre):
  val[s] = Ope[s] + prev
  for i in G[s]:
    if(i != pre):
      dfs(i, val[s], s)
  return

#X = str(input()).split()
#a = [int(x) for x in input().split()]


N, Q = map(int, input().split())
G = [[] for i in range(N)]
Ope = [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)

val = [0]*N

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

dfs(0, 0, 0)

print(val)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    
    n,q=map(int,input().split())
    tree=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=map(int,input().split())
        a-=1
        b-=1
        tree[a].append(b)
        tree[b].append(a)
    
    cnt=[0]*n
    for _ in range(q):
        p,x=map(int,input().split())
        p-=1
        cnt[p]+=x
    
    def dfs(i,before=-1):
        for e in tree[i]:
            if e!=before:
                cnt[e]+=cnt[i]
                dfs(e,i)
    dfs(0)   
    print(*cnt)

if __name__=='__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
jun_lis = [[] for i in range(N)]
gyaku_lis = [[] for i in range(N)]
plus_lis = [0] * N
for i in range(N-1):
  a,b = map(int, input().split())
  jun_lis[a-1].append(b-1)
  gyaku_lis[b-1].append(a-1)
for i in range(Q):
  p,x = map(int, inpu().split())
  plus_lis[p-1] += x
ans_lis = [0]*N
ans_lis[0] = plus_lis[0]
for i in range(1,N):
  ans_lis[i] = ans_lis[gyaku_lis[i]] + plus_lis[i]
print(*ans_lis)

Submission status: Runtime Error

Solution code:
# coding: utf-8
from queue import Queue


class TreeNode:
    def __init__(self, tree_id: int):
        self.parent = None
        self.id = tree_id
        self.val = 0

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

tree_list = [TreeNode(i) for i in range(n)]

connect = {}
for i in range(n-1):
    (a,b) = [int(i) - 1 for i in input().rstrip().split(" ")]
    if a not in connect:
        connect[a] = {}
    if b not in connect:
        connect[b] = {}

    connect[a][b] = True
    connect[b][a] = True

for i in range(q):
    (t,x) = [int(i) for i in input().rstrip().split(" ")]
    tree_list[t-1].val += x

q = Queue()
q.put(tree_list[0])

while not len(q) == 0:
    current = q.get()
    for i in connect[current.id]:
        if current.parent is not None:
            if i == current.parent.id:
                continue
        tree_list[i].parent = current
        tree_list[i].val += current.val
        q.put(tree_list[i])

print(" ".join([str(i.val) for i in tree_list]))

Submission status: Runtime Error

Solution code:
# coding: utf-8
from queue import Queue


class TreeNode:
    def __init__(self, tree_id: int):
        self.parent = None
        self.items = []
        self.id = tree_id
        self.val = 0

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

tree_list = [TreeNode(i) for i in range(n)]

connect = {}
for i in range(n-1):
    (a,b) = [int(i) - 1 for i in input().rstrip().split(" ")]
    if a not in connect:
        connect[a] = {}
    if b not in connect:
        connect[b] = {}

    connect[a][b] = True
    connect[b][a] = True

for i in range(q):
    (t,x) = [int(i) for i in input().rstrip().split(" ")]
    tree_list[t-1].val += x

q = [tree_list[0]]

while not q.empty():
    current = q.pop()
    for i in connect[current.id]:
        if current.parent is not None:
            if i == current.parent.id:
                continue
        tree_list[i].parent = current
        current.items.append(tree_list[i])
        tree_list[i].val += current.val
        q.append(tree_list[i])

print(" ".join([str(i.val) for i in tree_list]))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(200005)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a-1].append(b-1)
  l[b-1].append(a-1)
ans=[0 for i in range(n)]
for i in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x
def dsf(index,u):
  for i in l[index]:
    if i==u:continue
    ans[i]+=ans[index]
    dsf(i,index)
dsf(0,0)
print(' '.join(str(j) for j in ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a-1].append(b-1)
ans=[0 for i in range(n)]
for i in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x
def dsf(index,num):
  ans[index]+=num
  if len(l)-1<index:return
  for i in l[index]:
    dsf(i,ans[index])
dsf(0,0)
print(' '.join(str(j) for j in ans))

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
l=[[] for i in range(n-1)]
for i in range(n-1):
  a,b=map(int,input().split())
  l[a-1].append(b-1)
ans=[0 for i in range(n)]
for i in range(q):
  p,x=map(int,input().split())
  ans[p-1]+=x
def dsf(index,num):
  ans[index]+=num
  if len(l)-1<index:return
  for i in l[index]:
    dsf(i,ans[index])
dsf(0,0)
print(' '.join(str(j) for j in ans))

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
c=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  c[a].append(b)
  c[b].append(a)
px=[0]*n
for I in range(q):
    p,x=map(int,input().split())
    px[p-1]+=x 
ans=[0]*n
def dfs(g,h=-1):
  ans[g]=ans[h]+px[g]
  for i in c[g]:
    if i==h:
      continue
    dfs(i,g)
dfs(0)
print(*ans)

Submission status: Runtime Error

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

from collections import defaultdict
d = defaultdict(list)
for _ in range(n-1):
    a, b = map(int, input().split())
    d[a].append(b)
    
px = [list(map(int, input().split())) for _ in range(q)]


def dfs(u, x):
    C[u] += x
    for nu in d[u]:
        if V[nu] == 0:
            dfs(nu, x)
            

C = [0]*(n+1)

for p, x in px:
    V = [0]*(n+1)
    dfs(p, x)
    
print(*C[1:])

Submission status: Runtime Error

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


def solve(now,prev):
    for node in tree[now]:
        if node == prev:
            continue
        pointTree[node] += pointTree[now]
        solve(node,now)


for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for i in range(q):
    tp,x = map(int,input().split())
    pointTree[tp-1] += x
    
solve(0,-1)
s = ''
for i in range(n):
    s += str(pointTree[i])+' '
print(s[:-1])

Submission status: Runtime Error

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


def solve(now,point,prev):
    pointTree[now] += point
    for node in tree[now]:
        if node == prev:
            continue
        solve(node,point+p[node],now)


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

    
solve(0,p[0],-1)
s = ''
for i in range(n):
    s += str(pointTree[i])+' '
print(s[:-1])

Submission status: Runtime Error

Solution code:

def solve(pointTree,tree,p,now,point,prev):
    pointTree[now] += point
    for node in tree[now]:
        if node == prev:
            continue
        solve(pointTree,tree,p,node,point+p[node],now)

n,q = map(int,input().split())
tree = [[] for i in range(n)]
pointTree = [0 for i in range(n)]
p = [0 for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for i in range(q):
    tp,x = map(int,input().split())
    p[tp-1] += x
    
solve(pointTree,tree,p,0,p[0],-1)
s = ''
for i in range(n):
    s += str(pointTree[i])+' '
print(s[:-1])

Submission status: Runtime Error

Solution code:

def solve(pointTree,tree,p,now,point):
    pointTree[now] += point
    for node in tree[now]:
        solve(pointTree,tree,p,node,point+p[node])

n,q = map(int,input().split())
tree = [[] for i in range(n)]
pointTree = [0 for i in range(n)]
p = [0 for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
for i in range(q):
    tp,x = map(int,input().split())
    p[tp-1] += x
    
solve(pointTree,tree,p,0,p[0])
s = ''
for i in range(n):
    s += str(pointTree[i])+' '
print(s[:-1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(9999)
input = sys.stdin.readline
tree = []
cnt = []

def add_cnt(p,x):
    cnt[p] += x
    for b in tree[p]:
        add_cnt(b,x)

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

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

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

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

Submission status: Runtime Error

Solution code:
from collections import deque
n,q=map(int,input().split())
L=[[]for _ in range(n)]
V=[False]*n
P=[0]*n
for i in range(n-1):
   a,b=map(int,input().split())
   L[a-1].append(b-1)
   L[b-1].append(a-1)
for i in range(q):
   p,x=map(int,input().split())
   P[p-1]+=x
que=deque([0])
while que:
   s=que.popleft()
   V[s]=True
      for ni in L[s]:
         if V[ni]==False:
            P[ni]+=P[s]
            que.append(ni)
print(*P)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
N, Q = tuple(map(int, input().split(" ")))
ab = []
for _ in range(N-1):
    ab.append(tuple(map(int, input().split(" "))))
px = []
for _ in range(Q):
    px.append(tuple(map(int, input().split(" "))))

count = [0 for _ in range(N)]
for p, x in px:
    count[p-1] += x

ans = [-1 for _ in range(N)]

adj_list = [[] for _ in range(N)]
for a, b in ab:
    adj_list[a-1].append(b-1)
    adj_list[b-1].append(a-1)

def dfs(node, c):
    if ans[node] != -1:
        return
    ans[node] = c + count[node]
    for next_node in adj_list[node]:
        dfs(next_node, c + count[node])

dfs(0, 0)
print(" ".join([str(a) for a in ans]))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

N, Q = map(int, sys.stdin.readline().split(" "))

edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, sys.stdin.readline().split(" "))
    # print(a, b)
    edges[a-1].append(b-1)
# print(edges)
# sys.exit()

points = [0] * N
for _ in range(Q):
    p, x = map(int, sys.stdin.readline().split(" "))
    points[p-1] += x


counted = [0] * N
q = deque([(0, points[0])])
while q:
    node, point = q.popleft()
    # print(node, point)
    counted[node] = str(point)
    for edge in edges[node]:
        q.appendleft((edge, point + points[edge]))

print(" ".join(counted))

Submission status: Runtime Error

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

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

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

visited=set()
visited.add(0)
def counter(L,p):
    for i in L[p]:
        if i not in visited:
            visited.add(i)
            cnt[i] += cnt[p]
            counter(L,i)

counter(tree,0)
print(" ".join(map(str,cnt)))

Submission status: Runtime Error

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

#tree
tree = []
for i in range(N+1):
    tree.append([])
o = [0]*(N+1)
for i in range(N-1):
    a,b = map(int,input().split())
    tree[a].append(b)
    tree[b].append(a)

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

# DFS
def DFS(n):
    if n == 0:
        return
    oya = o[n]
    ans[n] = point[n] + ans[oya]
    while len(tree[n]) > 0:
        k = tree[n].pop()
        if k != oya and k != 0:
            o[k] = n
            return DFS(k)
    return DFS(oya)

# main
DFS(1)

# output
print(" ".join(map(str,ans[1:])))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 20)
def input(): return sys.stdin.readline().rstrip()

def main():
    N, Q = map(int, input().split())
    ki = [[] for _ in range(N + 1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        ki[a].append(b)
    PX = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = [0 for _ in range(N + 1)]
    def dfs(i, n):
        ans[i] += n
        for j in ki[i]:
            dfs(j, n)

    for p, x in PX:
        dfs(p, x)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, Q = map(int, input().split())
    ki = [[] for _ in range(N + 1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        ki[a].append(b)
    PX = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = [0 for _ in range(N + 1)]
    def dfs(i, n):
        ans[i] += n
        for j in ki[i]:
            dfs(j, n)

    for p, x in PX:
        dfs(p, x)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

    # dfs
    visited = {0}
    def dfs(i, s):
        ans[i] = s
        for j in tree[i]:
            if j in visited:
                continue
            visited.add(j)
            s += f[j]
            dfs(j, s)
            s -= f[j]

    dfs(0, f[0])

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque


def main():
    N, Q = map(int, input().split())

    tree = defaultdict(list)
    f = defaultdict(int)
    ans = defaultdict(int)
    for _ in range(N-1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

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

    # dfs
    visited = {1}
    def dfs(i, s):
        ans[i] = s
        for j in tree[i]:
            if j in visited:
                continue
            visited.add(j)
            s += f[j]
            dfs(j, s)
            s -= f[j]

    dfs(1, f[1])

    counts = [ans[i] for i in range(1, N+1)]
    ans = " ".join(map(str, counts))
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

def counter(L,p,x):
    for i in L[p]:
        cnt[i] += x
        counter(L,i,x)

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

for c in cnt:
    print(c)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
tree=[[] for _ in range(n-1)]
cnt=[0]*n

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

def counter(L,p,x):
    cnt[p-1]+=x
    for i in L[p-1]:
        cnt[i-1]+=x
        counter(L,i-1,x)

for i in range(q):
    p,x=map(int,input().split())
    counter(tree,p,x)

for c in range(cnt):
    print(c)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)

    N, Q = map(int, input().split())
    G = [set() for _ in range(N)]
    cnt = [0]*N
    visited = [0]*N

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

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

    visited[0]=1
    st = [0]
    while st:
        v = st.popleft()
        for u in G[v]:
            if visited[u]:
                continue
            visited[u] = 1
            cnt[u] += cnt[v]
            st.append(u)    

    print(*cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)

    N, Q = map(int, input().split())
    G = [[] for _ in range(N)]
    cnt = [0]*N
    visited = [0]*N

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

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

    visited[0]=1
    st = [0]
    while st:
        v = st.pop()
        for u in G[v]:
            if visited[u]:
                continue
            visited[u] = 1
            cnt[u] += cnt[v]
            st.append(u)    

    print(*cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(500000)

N, Q = map(int, input().split())
G = [set() for _ in range(N)]
cnt = [0]*N
visited = [0]*N


def dfs(start_node):
    for i in G[start_node]:
        if visited[i]==0:
            visited[i]=1
            cnt[i] += cnt[start_node]
            dfs(i)

for _ 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):
    node, x = map(int, input().split())
    node -= 1
    cnt[node] += x

visited[0]=1
dfs(0)

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

Submission status: Runtime Error

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


def dfs(start_node, x):
    for i in G[start_node]:
        cnt[i] += x
        dfs(i, x)



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

for i in range(Q):
    start_node, x = map(int, input().split())
    start_node -= 1
    cnt[start_node] += x
    dfs(start_node, x)



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