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

to = [[] for _ in range(N)]

counter = [0]*N

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

def dfs(p,x):
  counter[p] += x
  if flag[p]: return
  flag[p] = True
  for cmd in to[p]:
    if p < cmd: dfs(cmd,x)

for i in range(Q):
  flag = [False]*N
  p,x = map(int,input().split())
  dfs(p-1,x)

#print(to)

print(counter)

Submission status: Runtime Error

Solution code:
from copy import deepcopy

n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
tree1=[i for i in tree1 if i[0]!=1]

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

while tree1!=[]:
    for j in tree2:
        tree2=[(k if j[1]==k[0] else ([k[1],k[0]] if j[1]==k[1] else None)) for k in tree1]
        for i in tree2:
            if i is not None:
                c[i[1]-1]+=c[i[0]-1]
        tree1=[k for k in tree1 if j[1] not in k]

for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
tree1=[i for i in tree1 if i[0]!=1]

c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
l=0
while tree1!=[]:
    for j in tree2[l:][:]:
        l+=1
        tree3=[k if j[1]==k[0] else [k[1],k[0]] if j[1]==k[1] for k in tree1]
        tree1=[k if j[1] in k for k in tree1]
    tree2.extend(tree3)

for i in tree2:
    c[i[1]-1]+=c[i[0]-1]

for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

Solution code:
N, Q = (int(x) for x in input().split())
A = []
B = []
P = []
X = []

for i in (N-1):
    a, b =  (int(x) for x in input().split())
    A.append(a)
    B.append(b)
for i in Q:
    p, x =  (int(x) for x in input().split())
    P.append(p)
    X.append(x)

dic = {}
for i in P:
    dic[B[i]] = A[i]

C = [0] * N
for i in P:
    C[i] += X[i]

for i in range(1,N):
    C[i] += C[dic[i]-1]

for i in N:
    print(C[0])

Submission status: Runtime Error

Solution code:
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)

    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)    
    return pointTree

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]

        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    firstKey = list(tree.keys())[0]
    pointTree = sumTree(firstKey, tree[firstKey], 0)
    print(' '.join(map(str, pointTree)))

Submission status: Runtime Error

Solution code:
def sumTree(index, children, prev, pointTree=[]):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)
    
    for key in children:
        sumTree(key, tree[key], sumPoint, pointTree)
    return pointTree

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]
        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    pointTree = sumTree(1, tree[1], 0)
    print(' '.join(map(str, pointTree)))

Submission status: Runtime Error

Solution code:
pointTree = []

def sumTree(index, children, prev):
    sumPoint = (point[index] if index in point else 0) + prev
    pointTree.append(sumPoint)
    
    for key in children:
        sumTree(key, tree[key], sumPoint)    

if __name__ == "__main__":
    tree = {}
    point = {}
    N, Q = map(int, input().split(' '))
    for i in [input() for i in range(N-1)]:
        A, B = map(int, i.split(' '))
        if A in tree:
            tree[A].append(B)
        else:
            tree[A] = [B]
        if B not in tree:
            tree[B] = []

    for i in [input() for i in range(Q)]:
        A, B = map(int, i.split(' '))
        if A in point:
            point[A] += B
        else:
            point[A] = B

    sumTree(1, tree[1], 0)
    print(' '.join(map(str, pointTree)))

Submission status: Runtime Error

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


def flatten(data):
    return [element
            for item in data
            for element in (flatten(item) if hasattr(item, '__iter__') else [item])]

def search_append(tree,an,bn):
    if an in tree:
        tree.append([bn])
    else:
        for j in tree:
            if isinstance(j,list):
                search_append(j,an,bn)

def search_count(tree,counter,pn,xn):
    if pn in tree:
        for i in flatten(tree):
            counter[i-1]+=xn
    else:
        for j in tree:
            if isinstance(j,list):
                search_count(j,counter,pn,xn)


t=[1]

for i in range(n-1):
    a,b=input().split()
    a,b=int(a),int(b)
    search_append(t,a,b)

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

for i in range(q):
    p,x=input().split()
    p,x=int(p),int(x)
    search_count(t,c,p,x)

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

print("")
'''

#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った



n,q=map(int,input().split())
tree={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in tree:
        tree[a].append(b)
    else:
        tree[a]=[b]
#print(tree)
#ab=[list(map(int,input().split())) for i in range(n-1)]
#ab.sort(key=lambda x:(x[0],x[1]))
c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x

def tree_count(num):
    global tree
    global c
    if num in tree:
        for i in tree[num]:
            c[i-1]+=c[num-1]
            tree_count(i)

tree_count(1)
for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

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

edge=[list(map(int, input().split())) for i in range(N-1)]
adds=[list(map(int, input().split())) for i in range(Q)]

point=[0]*N

for i in range(Q):
  point[adds[i][1]-1]+=adds[i][0]
  
for i in range(N-1):
  point[edge[i][1]-1]+=point[edge[i][0]-1]
  
print(point)

Submission status: Runtime Error

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


def flatten(data):
    return [element
            for item in data
            for element in (flatten(item) if hasattr(item, '__iter__') else [item])]

def search_append(tree,an,bn):
    if an in tree:
        tree.append([bn])
    else:
        for j in tree:
            if isinstance(j,list):
                search_append(j,an,bn)

def search_count(tree,counter,pn,xn):
    if pn in tree:
        for i in flatten(tree):
            counter[i-1]+=xn
    else:
        for j in tree:
            if isinstance(j,list):
                search_count(j,counter,pn,xn)


t=[1]

for i in range(n-1):
    a,b=input().split()
    a,b=int(a),int(b)
    search_append(t,a,b)

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

for i in range(q):
    p,x=input().split()
    p,x=int(p),int(x)
    search_count(t,c,p,x)

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

print("")
'''

#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った



n,q=map(int,input().split())
tree={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in tree:
        tree[a].append(b)
    else:
        tree[a]=[b]
#print(tree)
#ab=[list(map(int,input().split())) for i in range(n-1)]
#ab.sort(key=lambda x:(x[0],x[1]))
c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x

def tree_count(num):
    global tree
    global c
    if num in tree:
        for i in tree[num]:
            tree_count(i)
            c[i-1]+=c[num-1]


tree_count(1)
for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

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

def tree_count(num):
    global tree
    global c
    if num in tree:
        for i in tree[num]:
            c[i-1]+=c[num-1]
            tree_count(i)
            
tree_count(1)
for i in range(n-1):
    print(c[i],end=" ")
print(c[n-1])

Submission status: Runtime Error

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return






def main():
    N,Q = list(map(int,input().split()))
    adjl = [[] for _ in range(N)]
    for _ in  range(N-1):
        a, b = list(map(lambda x: int(x)-1,input().split()))
        adjl[a].append(b)
        adjl[b].append(a)
    res = [0]*N
    for _ in  range(Q):
        p, x = list(map(lambda x: int(x),input().split()))
        res[p-1] += x
    def dfs(n, parent = None):
        if parent != None:
            res[n] += res[parent]
        for neigh in adjl[n]:
            if neigh == parent:
                continue
            else:
                dfs(neigh,n)
    dfs(0)
    for _ in range(N):
        print(res[_],end=" ")
    print()





if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def add_num(i, adj, tmp, ans_lst, total):
    for p in adj[i]:
        ans_lst[p] = total + tmp[p]
        if adj[p]:
            add_num(p, adj, tmp, ans_lst, total + tmp[p])
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] * N for _ in range(N)]
tmp = defaultdict(int)
ans_lst = [0] * N


for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x


ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, adj, tmp, ans_lst, total)

print(*ans_lst, sep=' ')

Submission status: Runtime Error

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

Submission status: Runtime Error

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

for i in range(Q):
  box=[]
  box.append(p[i])
  for k in range(N-1):
    if a[k] in box :
      box.append(b[k])
  for l in range(len(box)):
    num=box[l]-1
    v[num]+=x[i]

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

Submission status: Runtime Error

Solution code:
N, Q = [int(x) for x in input().split()]
counter_increments = {}
parents = [None for i in range(N+1)] # parents[i] is parent of vertex numberd i
for _ in range(N-1): # each node except 1 has exactly 1 parent
    a, b = [int(x) for x in input().split()]
    parents[b] = a
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    counter_increments[p] = counter_increments.get(p, 0) + x
result = [counter_increments.get(1, 0)] # results[i] corresponds to vertex i+1
for i in range(2, N+1):
    result.append(result[parents[i]-1] + counter_increments.get(i, 0))
for r in result:
    print(r, end=' ')

Submission status: Runtime Error

Solution code:
N, Q = [int(x) for x in input().split()]
counter_increments = {}
parents = [None for i in range(N+1)] # parents[i] is parent of vertex numberd i
for _ in range(N-1): # each node except 1 has exactly 1 parent
    a, b = [int(x) for x in input().split()]
    parents[b] = a
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    counter_increments[p] = counter_increments.get(p, 0) + x
result = [counter_increments.get(1, 0)] # results[i] corresponds to vertex i+1
for i in range(2, N+1):
    try:
        assert counter_increments.get(i, 0) >= 0
    except:
        print('asdfasdf')
        break
    result.append(result[parents[i]-1] + counter_increments.get(i, 0))
print(' '.join(str(r) for r in result))

Submission status: Runtime Error

Solution code:
N, Q = [int(x) for x in input().split()]
counter_increments = {}
parents = [None for i in range(N+1)] # parents[i] is parent of vertex numberd i
for _ in range(N-1): # each node except 1 has exactly 1 parent
    a, b = [int(x) for x in input().split()]
    parents[b] = a
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    counter_increments[p] = counter_increments.get(p, 0) + x
result = [counter_increments.get(1, 0)] # results[i] corresponds to vertex i+1
for i in range(2, N+1):
    try:
        parents[i]
    except:
        print('asdfasdf')
        break
    result.append(result[parents[i]-1] + counter_increments.get(i, 0))
print(' '.join(str(r) for r in result))

Submission status: Runtime Error

Solution code:
# Solution: Conveniently ai < bi so it's topologically sorted somewhat. value
# of counter at node i is value of counter at its parent (already calculated)
# plus combined value of operation j performed directly on i.

N, Q = [int(x) for x in input().split()]
counter_increments = {}
parents = [None for i in range(N+1)] # parents[i] is parent of vertex numberd i
for _ in range(N-1): # each node except 1 has exactly 1 parent
    a, b = [int(x) for x in input().split()]
    parents[b] = a
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    counter_increments[p] = counter_increments.get(p, 0) + x
result = [counter_increments.get(1, 0)] # results[i] corresponds to vertex i+1
for i in range(2, N+1):
    result.append(result[parents[i]-1] + counter_increments.get(i, 0))
print(' '.join(str(r) for r in result))

Submission status: Runtime Error

Solution code:
def search(lst, x, al, A, tmp, key):

    for p in lst:
        if p in tmp:
            for v in tmp[p]:
                A[v] += x
        else:
            A[p] += x
            tmp[key].add(p)
            search(al[p], x, al, A, tmp, key)

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

al = [[] * N for _ in range(N)]
tmp = {}
A = [0] * N


for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    al[a].append(b)
    
for i in range(Q):
    p, x = [int(i) - 1 for i in input().split()]
    if p in tmp:
        for v in tmp[p]:
            A[v] += x + 1
            continue
    else:
        A[p] += (x + 1)
        tmp[p] = {p}
        search(al[p], x + 1, al, A, tmp, p)

print(*A, sep=' ')

Submission status: Runtime Error

Solution code:

def add(tree,i, num):
    tree[i-1][0] += num
    for j in range(1,len(tree[i-1])):  
        add(tree, tree[i-1][j], num)

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

for i in range(q):
    a, b = map(int, input().split())
    add(tree, a, b)

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

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split(' '))
 
listTree = [[] for i in range(N)]
listNode = [0] * N
 
for i in range(N-1):
  a, b  = map(int, input().split(' '))
  listTree[a-1] += [b]

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

listResult = [0] * N
 
def calcNode(target):
  listResult[target - 1] = listNode[target - 1]
  targets = listTree[target - 1]
  for j in targets:
    listNode[j - 1] += listNode[target - 1]
  return targets

next_targets = [1]
while True:
  _next_targets = []
  for i in next_targets:
    result = calcNode(i)
    _next_targets += result
  next_targets = _next_targets
  if len(next_targets) == 0:
    break

str_result = ' '.join(map(str, listResult))
print(str_result)

Submission status: Runtime Error

Solution code:
import numpy as np

    
def searchParent(tmp_i, V):
  return_parent = []
  child_idx = np.where(V[tmp_i, :]==1)[0]
  parent_idx = np.where(V[tmp_i, :]==-1)[0]
  p_parent_idx = []
  for i in range(len(parent_idx)):
    V[parent_idx[i], child_idx] = 1
    p_parent_idx = np.where(V[parent_idx[i], :]==-1)[0]
    if len(p_parent_idx) > 0:
      return_parent.append(p_parent_idx)
  return p_parent_idx, V


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

V = np.zeros((N, N))

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

for i in range(N):
  parent_i, V = searchParent(i, V)
  while(len(parent_i) > 0):
    parent_i, V = searchParent(parent_i, V)

X = np.zeros(N).astype(int)
for i in range(Q):
  p, x = input().split()
  idx = np.where(V[int(p)-1,:] == 1)[0]
  X[idx] += int(x)

print(*X)

Submission status: Runtime Error

Solution code:
import numpy as np

    
def searchParent(tmp_i, V):
  return_parent = []
  child_idx = np.where(V[tmp_i, :]==1)[0]
  parent_idx = np.where(V[tmp_i, :]==-1)[0]
  p_parent_idx = []
  for i in range(len(parent_idx)):
    V[parent_idx[i], child_idx] = 1
    p_parent_idx = np.where(V[parent_idx[i], :]==-1)[0]
    if len(p_parent_idx) > 0:
      return_parent.append(p_parent_idx)
  return p_parent_idx, V


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

V = np.zeros((N, N))

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

for i in range(N):
  parent_i, V = searchParent(i, V)
  while(len(parent_i) > 0):
    parent_i, V = searchParent(parent_i, V)

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

print(X)

Submission status: Runtime Error

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

list_ab = []
for i in range(n - 1):
  pair_ab = [int(num) for num in input().split(" ")]
  list_ab.append(pair_ab)

list_px = []
for i in range(q):
  pair_px = [int(num) for num in input().split(" ")]
  list_px.append(pair_px)

def make_dict(list_ab):
  dict = {}
  for num in range(1, n+1):
    dict[num] = []
  for pair_ab in list_ab:
    dict[int(pair_ab[0])].append(pair_ab[1])
  return dict

ab_dict = make_dict(list_ab)

counter = [0]*(n+1)

def calc(position, num_plus):
  counter[position] += num_plus
  list_plus = ab_dict[position]
  for num in list_plus:
    calc(num, num_plus)

for p, x in list_px:
  calc(p, x)

for i in range(1, len(counter)):
  counter[i] = str(counter[i])
counter.pop(0)
str = " ".join(counter)
print(str)

Submission status: Runtime Error

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

list_ab = []
for i in range(n - 1):
  pair_ab = [int(num) for num in input().split(" ")]
  list_ab.append(pair_ab)

list_px = []
for i in range(q):
  pair_px = [int(num) for num in input().split(" ")]
  list_px.append(pair_px)

def make_dict(list_ab):
  dict = {}
  for num in range(1, n+1):
    dict[num] = []
  for pair_ab in list_ab:
    dict[int(pair_ab[0])].append(pair_ab[1])
  return dict

ab_dict = make_dict(list_ab)

counter = [0]*(n+1)

def calc(position, num_plus):
  counter[position] += num_plus
  list_plus = ab_dict[position]
  for num in list_plus:
    calc(num, num_plus)

for p, x in list_px:
  calc(p, x)

for i in range(len(counter)):
  counter[i] = str(counter[i])
str = " ".join(counter)
print(str)

Submission status: Runtime Error

Solution code:
tree = [[] for i in range(200001)]
res = [0]*200001

def dfs(p, pre):
    for child in tree[p]:
        res[child]+= res[p]
        dfs(child, p)

def main():
    N, Q = map(int, input().split())
    for i in range(N-1):
        a, b =map(int, input().split())
        tree[a].append(b)
    for i in range(Q):
        p, x = map(int, input().split())
        res[p] += x
    dfs(1, 0)
    print(*res[1:N+1])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def search(lst, x, al, A):
    if not lst:
        return
    for p in lst:
        A[p] += x
        search(al[p], x, al, A)

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

al = [[] * N for _ in range(N)]
A = [0] * N

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

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

print(*A, sep=' ')

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n, q = map(int, input().split())

# Tree DS
tree = defaultdict(list)

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

# Start processing queries
ans = [0] * n;

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

# push values
def dfs(curnode, parentCost):
    ans[curnode] += parentCost
    if(tree.get(curnode) == None):
        return

    for _ in range(len(tree[curnode])):
        dfs(tree[curnode][_], ans[curnode])

dfs(0, 0)

print(*ans)

Submission status: Runtime Error

Solution code:
def main(edge, counter):

    def add_count(node, before=-1):
        for target in edge[node]:
            if target != before:
                counter[target] += counter[node]
                add_count(target, node)

    add_count(0)

    return " ".join(str(i) for i in counter)


if __name__ == "__main__":
    n, q = map(int, input().split())
    edge = [[] for _ in range(n)]

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

    add_list = [0]*n

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

    print(main(edge, add_list))

Submission status: Runtime Error

Solution code:
def main(edge, add_list):
    node_len = len(edge)
    counter = add_list

    def add_count(node, before=-1):
        for target in edge[node]:
            if target != before:
                counter[target] += counter[node]
                add_count(target, node)

    add_count(0)

    return " ".join(str(i) for i in counter)


if __name__ == "__main__":
    n, q = map(int, input().split())
    edge = [[] for _ in range(n)]

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

    add_list = [0]*n

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

    print(main(edge, add_list))

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
N, Q = map(int, input().split(" "))
 
ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)
 
def add_nodes(seledted, nodes):
  for n in nodes:
    if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected
 
results = [0] * N
for p, x in px:
  selected = [p]
  selected = add_nodes(selected, root2node[p])
  for s in selected:
    results[s - 1] += x
 
for r in results[:-1]:
  print(r, end =" ") 
print(results[-1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
N, Q = map(int, input().split(" "))
 
ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)
 
def add_nodes(seledted, nodes):
  for n in nodes:
    if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected
 
results = [0] * N
for p, x in px:
  selected = [p]
  add_nodes(selected, root2node[p])
  for s in selected:
    results[s - 1] += x
 
for r in results[:-1]:
  print(r, end =" ") 
print(results[-1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
N, Q = map(int, input().split(" "))
 
ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)
 
def add_nodes(seledted, nodes):
  for n in nodes:
    if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected
 
results = [0] * N
for p, x in px:
  selected = [p]
  add_nodes(selected, root2node[p])
  for s in selected:
    results[s - 1] += x
 
for r in results[:-1]:
  print(r, end =" ") 
rint(results[-1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
N, Q = map(int, input().split(" "))
 
ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)
 
def add_nodes(seledted, nodes):
  for n in nodes:
    if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected
 
results = [0] * N
for p, x in px:
  selected = [p]
  add_nodes(selected, root2node[p])
  for s in selected:
    results[s - 1] += x
 
for r in results:
  print(r, end =" ")

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
N, Q = map(int, input().split(" "))
 
ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)
 
def add_nodes(seledted, nodes):
  for n in nodes:
    if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected
 
results = [0] * N
for p, x in px:
  selected = [p]
  add_nodes(selected, root2node[p])
  print(selected)
  for s in selected:
    results[s - 1] += x
 
for r in results:
  print(r, end =" ")

Submission status: Runtime Error

Solution code:
from collections import defaultdict

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

ab = []
for _ in range(N - 1):
  a, b = map(int, input().split(" "))
  ab.append((a, b))
  
px = []
for _ in range(Q):
  p, x = map(int, input().split(" "))
  px.append((p, x))
  
root2node = defaultdict(list, [])
for a, b in ab:
  root2node[a].append(b)

def add_nodes(seledted, nodes):
  for n in nodes:
  	if n not in selected:
      selected.append(n)
      add_nodes(seledted, root2node[n])
  return selected

results = [0] * len(root2node)
for p, x in px:
  selected = []
  add_nodes(selected, root2node[p])
  for s in selected:
    results[s] += x

for r in results:
  print(r, end =" ")

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200001)
N, Q = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
p_x = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]
 
rin = [[] for _ in range(N)]
for v in a_b:
    rin[v[0]-1].append(v[1]-1)
    rin[v[1]-1].append(v[0]-1)
    
score = [0 for _ in range(N)]
for ele in p_x:
    score[ele[0]-1] +=ele[1]
#print(score)
 
def dfs(p, bp=-1):
    for c in rin[p]:
        if c != bp:
            #print(c)
            score[c]+= score[p]
            dfs(c, p)
 
dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(50000)
N, Q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(N-1)]
p_x = [list(map(int, input().split())) for _ in range(Q)]
 
rin = [[] for _ in range(N)]
for v in a_b:
    rin[v[0]-1].append(v[1]-1)
    rin[v[1]-1].append(v[0]-1)
    
score = [0 for _ in range(N)]
for ele in p_x:
    score[ele[0]-1] +=ele[1]
#print(score)
 
def dfs(p, bp=-1):
    for c in rin[p]:
        if c != bp:
            #print(c)
            score[c]+= score[p]
            dfs(c, p)
 
 
 
 
dfs(0)
print(*score)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(20001)
N, Q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(N-1)]
p_x = [list(map(int, input().split())) for _ in range(Q)]
 
rin = [[] for _ in range(N)]
for v in a_b:
    rin[v[0]-1].append(v[1]-1)
    rin[v[1]-1].append(v[0]-1)
    
score = [0 for _ in range(N)]
for ele in p_x:
    score[ele[0]-1] +=ele[1]
#print(score)
 
def dfs(p, bp=-1):
    for c in rin[p]:
        if c != bp:
            #print(c)
            score[c]+= score[p]
            dfs(c, p)
 
 
 
 
dfs(0)
print(*score)

Submission status: Runtime Error

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

rin = [[] for _ in range(N)]
for v in a_b:
    rin[v[0]-1].append(v[1]-1)
    rin[v[1]-1].append(v[0]-1)
    
score = [0 for _ in range(N)]
for ele in p_x:
    score[ele[0]-1] +=ele[1]
#print(score)

ans = [0 for _ in range(N)]
def dfs(p, bp=-1):
    for c in rin[p]:
        if c != bp:
            #print(c)
            score[c]+= score[p]
            dfs(c, p)




dfs(0)
print(*score)