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())
ab = [list(map(int, input().split())) for _ in range(n-1)]
px = [list(map(int, input().split())) for _ in range(q)]

cnt = [0 for _ in range(n+1)]
for p, x in px:
    cnt[p] += x

edge = [[] for _ in range(n+1)]
for a, b in ab:
    edge[a].append(b)
    edge[b].append(a)


def dfs(now, bef):
    for nxt in edge[now]:
        if nxt != bef:
            cnt[nxt] += cnt[now]
            dfs(nxt, now)


dfs(1, 0)
print(*cnt[1:])

Submission status: Runtime Error

Solution code:
    import sys
    sys.setrecursionlimit(10**8)
        
    def treetrace(i):
      score = [-1] * (n + 1)
      if src[i] == 0:
        return point[i]
      elif score[i] != -1:
        return score[i]
      else:
        score[i] = point[i] + treetrace(src[i])
        return score[i]
      
    tmp = list(map(int, (input().split())))
    n = tmp[0]
    q = tmp[1]
     
    src = [0] * (n + 1)
    for i in range(1, n):
      tmp = list(map(int, (input().split())))
      src[tmp[1]] = tmp[0]
     
    point = [0] * (n + 1)
    for i in range(1, q+1):
      tmp = list(map(int, (input().split())))
      point[tmp[0]] += tmp[1]
      
    for i in range(1, n + 1):
      print(treetrace(i), end=" ")

Submission status: Runtime Error

Solution code:
    import sys
    sys.setrecursionlimit(10**8)
     
    def treetrace(i):
      score = [-1] * (n + 1)
      if src[i] == 0:
        return point[i]
      elif score[i] != -1:
        return score[i]
      else:
        score[i] = point[i] + treetrace(src[i])
        return score[i]
      
    tmp = list(map(int, (input().split())))
    n = tmp[0]
    q = tmp[1]
     
    src = [0] * (n + 1)
    for i in range(1, n):
      tmp = list(map(int, (input().split())))
      src[tmp[1]] = tmp[0]
     
    point = [0] * (n + 1)
    for i in range(1, q+1):
      tmp = list(map(int, (input().split())))
      point[tmp[0]] += tmp[1]
      
    for i in range(1, n + 1):
      print(treetrace(i), end=" ")

Submission status: Runtime Error

Solution code:
    import sys
    sys.setrecursionlimit(10**8)
     
    def treetrace(i):
      score = [-1] * (n + 1)
      if src[i] == 0:
        return point[i]
      elif score[i] != 0:
        return score[i]
      else:
        score[i] = point[i] + treetrace(src[i])
        return score[i]
      
    tmp = list(map(int, (input().split())))
    n = tmp[0]
    q = tmp[1]
     
    src = [0] * (n + 1)
    for i in range(1, n):
      tmp = list(map(int, (input().split())))
      src[tmp[1]] = tmp[0]
     
    point = [0] * (n + 1)
    for i in range(1, q+1):
      tmp = list(map(int, (input().split())))
      point[tmp[0]] += tmp[1]
      
    for i in range(1, n + 1):
      print(treetrace(i), end=" ")

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

def treetrace(i):
  score = [0] * (n + 1)
  if src[i] == 0:
    return point[i]
  elif score[i] != 0:
    return score[i]
  else:
    return score[i] = point[i] + treetrace(src[i])
  
tmp = list(map(int, (input().split())))
n = tmp[0]
q = tmp[1]

src = [0] * (n + 1)
for i in range(1, n):
  tmp = list(map(int, (input().split())))
  src[tmp[1]] = tmp[0]

point = [0] * (n + 1)
for i in range(1, q+1):
  tmp = list(map(int, (input().split())))
  point[tmp[0]] += tmp[1]
  
for i in range(1, n + 1):
  print(treetrace(i), end=" ")

Submission status: Runtime Error

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

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

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

def spread(pi):
  for i in t[pi]:
    c[i] += c[pi]
    spread(i)

spread(0)

print(*c)

Submission status: Runtime Error

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

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

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

def spread(pi):
  for i in range(len(t[pi])):
    c[t[pi][i]] += c[pi]
    spread(t[pi][i])

spread(0)

print(*c)

Submission status: Runtime Error

Solution code:
from collections import defaultdict as dd
N, Q = map(int, input().split())
parents = {}
for _ in range(N-1):
    a, b = map(int, input().split())
    parents[b] = a
process = dd(int)
for _ in range(Q):
    p, x = map(int, input().split())
    process[p] += x
points = [0]*(N+1)
for i in range(1, N+1):
    points[i] += process[i]
    if i != 1:
        points[i] += points[parents[i]]
print(' '.join(map(str, points[1:])))

Submission status: Runtime Error

Solution code:
def add(r):
    if r.next==[]:
        pass
    else:
        for v in r.next:
            v.val+=r.val
            add(v)

class Vertex:
    def __init__(self):
        self.val=0
        self.next=[]

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

V=[Vertex() for i in range(N)]

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

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

add(V[0])

for v in V:
    print(v.val,end=' ')

Submission status: Runtime Error

Solution code:
def root(V):
    for v in V:
        if v.isroot:
            return v
    return None

def add(r):
    if r.next==[]:
        pass
    else:
        for v in r.next:
            v.val+=r.val
            add(v)

class Vertex:
    def __init__(self):
        self.val=0
        self.next=[]
        self.isroot=True

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

V=[Vertex() for i in range(N)]

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

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

r=root(V)
add(r)

for v in V:
    print(v.val,end=' ')

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop
import deque
import re
import math
import functools

def s_raw():
    return input().rstrip("\r")

def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return list(map(int, ss_raw()))


INF = 1 << 29


def make1d_arr(n, val=INF):
    return [val for i in range(n)]


def make2d_arr(h, w, val=INF):
    return [[val for i in range(w)]for i in range(h)]


def gcd(a, b):
    if(b == 0):
        return a
    return gcd(b, a % b)


def BFord(n,es):
    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n*2):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
        if v == n-1:
            prev = v_cost[:]
    v_cost = [-INF if prev[idx]!=v_cost[idx] else v_cost[idx] for idx in range(n)]
    return v_cost
    
def ZAlgo(s):
    l = len(s)
    A = [0]*l
    i =1
    j =0
    A[0]=l
    while i<l:
        while i+j<l and s[j] == s[i+j]:
            j+=1
        if j==0:
            i+=1
            continue
        A[i]=j
        k=1
        while (i+k<l) and (k+A[k]<j):
            A[i+k] =A[k]
            k+=1
        i+=k
        j-=k
    return A


def main():
    
    n,q = ints_raw()
    graph = [[] for _ in range(n)] 
    counters = [0 for _ in range(n)]
    for _ in range(n-1):
        a,b = ints_raw()
        a = a-1
        b = b-1
        graph[a].append(b)
        graph[b].append(a)
    for _ in range(q):
        p,x = ints_raw()
        p = p-1
        counters[p]+=x
    ans = [-1 for _ in range(n)]
    qu = deque()
    qu.append((0,0))

    while len(qu) != 0:
        node,parC = qu.popleft()
        curC = counters[node]+ parC
        ans[node]=curC
        for toN in graph[node]:
            if ans[toN] == -1:
                qu.append((toN,curC))
    return " ".join([str(a) for a in ans])


if __name__ == "__main__":
    print(main())

Submission status: Runtime Error

Solution code:
def root(V): # i(Root)
    for i in range(len(V)):
        if V[i][1]==[]:
            return i
    return -1

def add(V,r): # add recursively
    for l in V[r][2]:
        V[l][0]+=V[r][0]
        add(V,l)

N,Q=map(int,input().split())
V=[[0,[],[]] for i in range(N)] # value,prev[],next[]

for i in range(N-1):
    a,b=map(int,input().split())
    V[a-1][2].append(b-1) # append i(Next) to Prev.next
    V[b-1][1].append(a-1) # append i(Prev) to Next.prev

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

add(V,root(V))

for v in V:
    print(v[0],end=' ')

Submission status: Runtime Error

Solution code:
"""
testing code here
 
https://codeforces.com/blog/entry/70304
"""
 
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
  
# 'global' variables
d = defaultdict(list)
l = [0]*(n+1)
l[a] += b
visted = [False]*(n+1)
  
def dfs(u,v):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i)
    return 
            
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    d[a].append(b);d[b].append(a)
 
for i in range(m):
    a,b = map(int,input().split())
 
dfs(0,1)
print(*l[1:])

Submission status: Runtime Error

Solution code:
"""
testing code here

https://codeforces.com/blog/entry/70304
"""
 
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
  
# 'global' variables
d = defaultdict(list)
l = [0]*(n+1)
l[a] += b
visted = [False]*(n+1)
  
def dfs(u,v):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i,visted,l,d)
    return 
            
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    d[a].append(b);d[b].append(a)

for i in range(m):
    a,b = map(int,input().split())
 
dfs(0,1)
print(*l[1:])

Submission status: Runtime Error

Solution code:
# 頂点n個 辺iは頂点aiとbiを結ぶ 各頂点には初期値0のカウンターがある
# 操作j: 頂点pjを根とする部分木に含まれるすべてのカウンターにxj足す
# Q回の操作のあと各カウンターの数値を求める

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

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


def dfs(now=0, prev=-1):
    for i in range(len(connect[now])):
        kid = connect[now][i]
        if kid == prev:
            continue
        count[kid] += count[now]
        dfs(kid, now)


dfs()

for i in range(n):
    print(count[i])

Submission status: Runtime Error

Solution code:
N,Q=map(int, input().split())
#AB=[list(map(int, input().split())) for i in range(N)]
tree_dist={}
for i in range(N):
    tree_dist[i+1] = set()
for i in range(N-1):
    a,b = map(int, input().split())
    tree_dist[a].add(b)
    tree_dist[b].add(a)
    
PX=[list(map(int, input().split())) for i in range(Q)]
z=[0]*N
for p,x in PX:
    z[p-1] += x

def saiki(i,p):
    L = list(tree_dist[i])
    for l in L:
        if l == p:
            continue
        z[l-1] += z[i-1]
        saiki(l,i)
saiki(1,0)
print(" ".join(map(str,z)))

Submission status: Runtime Error

Solution code:
N,Q=map(int, input().split())
tree_dist={}
for i in range(N):
    tree_dist[i+1] = set()
for i in range(N-1):
    a,b = map(int, input().split())
    tree_dist[a].add(b)
    tree_dist[b].add(a)
    
PX=[list(map(int, input().split())) for i in range(Q)]
z=[0]*N
for p,x in PX:
    z[p-1] += x

def saiki(i,p):
    print(i)
    L = list(tree_dist[i])
    for l in L:
        if l == p:
            continue
        z[l-1] += z[i-1]
        saiki(l,i)
saiki(1,0)
print(*z)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:26:06 2019

@author: avina
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(1500)
def dfs(u,v, visted,l,d):
    visted[v] = True
    l[v] += l[u]
    for i in d[v]:
        if visted[i] == False:
            dfs(v,i,visted,l,d)
            
g = defaultdict(list)
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    g[a].append(b);g[b].append(a)
l = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
visted = [False]*(n+1)

dfs(0,1,visted,l,g)
print(*l[1:])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:26:06 2019

@author: avina
"""

from collections import defaultdict

def dfs(u,v, visted,l,d):
    visted[v] = True
    l[v] += l[u]
    for i in d:
        if visted[i] == False:
            dfs(v,i,visted,l,d)
            
g = defaultdict(list)
n,m = map(int, input().split())
for i in range(n-1):
    a,b = map(int, input().split())
    g[a].append(b);g[b].append(a)
l = [0]*(n+1)
for i in range(m):
    a,b = map(int,input().split())
    l[a] += b
visted = [False]*(n+1)

dfs(0,1,visted,l,g)
print(*l[1:])

Submission status: Runtime Error

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

def dfs(v, par):
    for nv in tree[v]:
        if nv == par:
            continue
        add[nv] += add[v]
        dfs(nv, v)


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

Submission status: Runtime Error

Solution code:
def dfs(v, par):
    for nv in tree[v]:
        if nv == par:
            continue
        add[nv] += add[v]
        dfs(nv, v)


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

Submission status: Runtime Error

Solution code:
import sys

def main():
    def input():
        return sys.stdin.readline()[:-1]

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

    def dfs(ima):
        for tsugi in G[ima]:
            ans[tsugi] += ans[ima]
            dfs(tsugi)
    dfs(0)
    print(*ans,sep=" ")
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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


count=[-1]*N
c=0
def dfs(p):
  global c
  c+=P[p-1]
  count[p-1]=c
  for i in ab[p-1]:
    if count[i-1]==-1:
      dfs(i)
  c-=P[p-1]

dfs(1)
print(" ".join(map(str,count)))

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

v = [[] for _ in range(n)]
for i in range(n-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    v[a].append(b)
    v[b].append(a)
    
f = [0] * (n + 1)
for i in range(q):
    p = px[i][0] - 1
    f[p] += px[i][1]
    
def dfs(x, d, s):
    for y in v[x]:
        if d[y]:
            s += f[y]
            ans[y] = s
            d[y] = 0
            dfs(y, d, s)
            s -= f[y]
        
ans = [0] * n
d = [1] * n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
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)]

v = [[] for _ in range(n)]
for i in range(n-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    v[a] += [b]
    v[b] += [a]
    
f = [0] * n
for i in range(q):
    p = px[i][0] - 1
    f[p] += px[i][1]
    
def dfs(x, d, s):
    for y in v[x]:
        if d[y]:
            s += f[y]
            ans[y] = s
            d[y] = 0
            dfs(y, d, s)
            s -= f[y]
        
ans = [0] * n
d = [1] * n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
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)]

v = [[] for _ in range(n + 20005)]
for i in range(n-1):
    a = ab[i][0] - 1
    b = ab[i][1] - 1
    v[a] += [b]
    v[b] += [a]
    
f = [0] * n
for i in range(q):
    p = px[i][0] - 1
    f[p] += px[i][1]
    
def dfs(x, d, s):
    for y in v[x]:
        if d[y]:
            s += f[y]
            ans[y] = s
            d[y] = 0
            dfs(y, d, s)
            s -= f[y]
        
ans = [0] * n
d = [1] * n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
print(*ans)

Submission status: Runtime Error

Solution code:
class ki:
  def __init__(self,val):
    self.value = val
    self.count = 0
    self.oya = None
    self.child = []
  def addall(self,a):
    self.count +=a
    for ch in self.child:ch.addall(a)
  def seek(self,v):
    if v==self.value:return self
    for ch in self.child:
      r1 = ch.seek(v)
      if r1 is not None:return r1
    return None
  def append(self,v):
    self.child.append(ki(v))
    
n,q = map(int,input().split())

obj = ki(1)
for i in range(n-1):
  a,b = map(int,input().split())
  obj.seek(a).append(b)

for i in range(q):
  v,c = map(int,input().split())
  obj.seek(v).addall(c)

print(" ".join(map(str,(obj.seek(i).count for i in range(1,n+1)))))

Submission status: Runtime Error

Solution code:
class ki:
  def __init__(self,val):
    self.value = val
    self.count = 0
    self.oya = None
    self.child = []
  def addall(self,a):
    self.count +=a
    for ch in self.child:ch.addall(a)
  def seek(self,v):
    if v==self.value:return self
    for ch in self.child:
      r1 = ch.seek(v)
      if r1 is not None:return r1
    return None
  def append(self,v):
    self.child.append(ki(v))
    
n,q = map(int,input().split())

obj = ki(1)
for i in range(n-1):
  a,b = map(int,input().split())
  obj.seek(a).append(b)

for i in range(q):
  v,c = map(int,input().split())
  obj.seek(v).addall(c)

Submission status: Runtime Error

Solution code:
class ki:
  def __init__(self,val):
    self.value = val
    self.count:int = 0
    self.oya:ki = None
    self.child:List[ki] = []
  def addall(self,a):
    self.count +=a
    for ch in self.child:ch.addall(a)
  def seek(self,v):
    if v==self.value:return self
    for ch in self.child:
      r1 = ch.seek(v)
      if r1 is not None:return r1
    return None
  def append(self,v):
    self.child.append(ki(v))
    
n,q = map(int,input().split())

obj = ki(1)
for i in range(n-1):
  a,b = map(int,input().split())
  obj.seek(a).append(b)

for i in range(q):
  v,c = map(int,input().split())
  obj.seek(v).addall(c)

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

for i in range(n - 1):
    a, b = map(int, input().split())
    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)

for i in ans:
    print(i)

Submission status: Runtime Error

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

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

for i in range(n - 1):
    a, b = map(int, input().split())
    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)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import collections




def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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

def solve2(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * N

    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    def add_counter(cur,x):
        counter[cur-1] += x
        for i in graph[cur]:
            add_counter(i,x)

    for pi, xi in zip(p, x):
        add_counter(pi,xi)

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


def solve3(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    graph = collections.defaultdict(list)
    counter = [0] * (N+1)
    for ai, bi in zip(a, b):
        graph[ai].append(bi)

    for pi, xi in zip(p, x):
        counter[pi] += xi

    def add_counter(cur,x):
        counter[cur] += x
        for i in graph[cur]:
            add_counter(i,counter[cur])

    add_counter(1,0)
    print(" ".join(map(str,counter[1:])))






# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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))
    solve3(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from sys import stdin

input = stdin.readline
lmi = lambda: list(map(int, input().split()))
mi = lambda: map(int, input().split())
si = lambda: input().strip('\n')
ssi = lambda: input().strip('\n').split()


def dfs(node, prev):
    for i in graph[node]:
        if i != prev:
            inc[i] += inc[node]
            dfs(i, node)


n, q = mi()
inc = [0 for i in range(n+1)]
graph = [[] for i in range(n+1)]
for i in range(n-1):
    a, b = mi()
    graph[a].append(b)
    graph[b].append(a)
for i in range(q):
    a, b = mi()
    inc[a] += b
dfs(1, -1)
print(*inc[1:])

Submission status: Runtime Error

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

for _ in range(N - 1):
	a, b = map(int, input().split())
	node[a - 1].append(b)
	node[b - 1].append(a)
	
for _ in range(Q):
	p, x = map(int, input().split())
	count_lst[p - 1] += x
	
def dfs(v, parent):
	for adj in node[v - 1]:
		if adj == parent: continue
		count_lst[adj - 1] += count_lst[v - 1]
		dfs(adj, v)
dfs(1, -1)
print(*count_lst)

Submission status: Runtime Error

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

# 遅延評価で加えてあげるだけ

N,Q = map(int,readline().split())
lines = readlines()
AB = [tuple(int(x) for x in readline().split()) for _ in range(N-1)]
PX = [tuple(int(x) for x in readline().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

par = [None] * (N+1)
q = [1]
while q:
    x = q.pop()
    for y in graph[x]:
        if y == par[x]:
            continue
        value[y] += value[x]
        q.append(y)
        par[y] = x

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def solve(f):
    n, q = f.read_int_list()
    g = collections.defaultdict(list)
    for _ in xrange(n-1):
        a, b = f.read_int_list()
        g[a].append(b)
        g[b].append(a)

    c = [0] * (n+1)

    for _ in xrange(q):
        p, x = f.read_int_list()
        c[p] += x

    vis = [False] * (n+1)
    def dfs(v, ci):
        vis[v] = True
        c[v] += ci
        for vi in g[v]:
            if not vis[vi]:
                dfs(vi, c[v])

    dfs(1, 0)

    return " ".join(map(str, c[1:]))


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Runtime Error

Solution code:
import queue
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())

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

points = [0]*N
PX = [list(map(int, input().split())) for _ in range(Q)]
for px in PX:
    points[px[0]-1] += px[1]

def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Runtime Error

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

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

points = [0]*N
PX = [list(map(int, input().split())) for _ in range(Q)]
for px in PX:
    points[px[0]-1] += px[1]


def dfs(v, par):
    for nv in edges[v]:
        if nv == par:
            continue
        points[nv] += points[v]
        dfs(nv, v)

dfs(0, -1)

print(*points)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
 
graph = [[] for i in range(N)]
ans = [ 0 for i in range(N)]
 
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
 
for _ in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
def dfs(v, p):
    for u in graph[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())
 
graph = [[] for i in range(N)]
ans = [ 0 for i in range(N)]
 
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
 
for _ in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
def dfs(v, p=-1):
    for u in graph[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)
 
dfs(0)
print(*ans)

Submission status: Runtime Error

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

graph = [[] for i in range(N)]
ans = [ 0 for i in range(N)]

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

for _ in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
def dfs(v, p=-1):
    for u in graph[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u,v)

dfs(0)
print(*ans)

Submission status: Runtime Error

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

graph = [[] for i in range(N)]
ans = [ 0 for i in range(N)]

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

for _ in range(Q):
    p, x = map(int, input().split())
    ans[p-1] += x
    
def dfs(v, p=-1):
    for u in graph[v]:
        if u != p:
            ans[u] += ans[v]
            dfs(u,v)

dfs(0)
print(*ans)