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

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

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]
    calcNode(j)

calcNode(1)

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

Submission status: Runtime Error

Solution code:
N_Q_str = input()
N = int(N_Q_str.split(" ")[0])
Q = int(N_Q_str.split(" ")[1])
a = [0] * (N-1)
b = [0] * (N-1)

for i in range(N-1):
    a_b_str = input()
    a[i] = int(a_b_str.split(" ")[0])
    b[i] = int(a_b_str.split(" ")[1])

p = [0] * Q
x = [0] * Q

for i in range(Q):
    p_x_str = input()
    p[i] = int(p_x_str.split(" ")[0])
    x[i] = int(p_x_str.split(" ")[1])

#correspond_list = [[]] * N

correspond_list = []
for i in range(N):
    correspond_list.append([])

#print(correspond_list)

for i in range(N-1):
    correspond_list[a[i]-1].append(b[i])

#print(correspond_list)

flag = [0] * N
counter = [0] * N
def counter_plus(top, counter, correspond_list, add_value, flag):
    if flag[top-1] == 0:
        counter[top-1] += add_value
        flag[top-1] = 1
        if len(correspond_list[top-1]) > 0:
            for i in range(len(correspond_list[top-1])):
                counter_plus(correspond_list[top-1][i], counter, correspond_list, add_value, flag)

for i in range(Q):
    counter_plus(p[i], counter, correspond_list, x[i], flag)
    flag = [0] * N

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

Submission status: Runtime Error

Solution code:
N_Q_str = input()
N = int(N_Q_str.split(" ")[0])
Q = int(N_Q_str.split(" ")[1])
a = [0] * (N-1)
b = [0] * (N-1)

for i in range(N-1):
    a_b_str = input()
    a[i] = int(a_b_str.split(" ")[0])
    b[i] = int(a_b_str.split(" ")[1])

p = [0] * Q
x = [0] * Q

for i in range(Q):
    p_x_str = input()
    p[i] = int(p_x_str.split(" ")[0])
    x[i] = int(p_x_str.split(" ")[1])

#correspond_list = [[]] * N

correspond_list = []
for i in range(N):
    correspond_list.append([])

#print(correspond_list)

for i in range(N-1):
    correspond_list[a[i]-1].append(b[i])

#print(correspond_list)

flag = [0] * N
counter = [0] * N
def counter_plus(top, counter, correspond_list, add_value, flag):
    if flag[top-1] == 0:
        counter[top-1] += add_value
        flag[top-1] = 1
        if len(correspond_list[top-1]) > 0:
            for i in range(len(correspond_list[top-1])):
                counter_plus(correspond_list[top-1][i], counter, correspond_list, add_value, flag)

for i in range(Q):
    counter_plus(p[i], counter, correspond_list, x[i], flag)
    flag = [0] * N

print(counter)

Submission status: Runtime Error

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


a_t = [{'value':0,'child':[]}]

def addvalue(a_t,index,value):
    if(len(a_t[index-1]['child']) == 0):
        a_t[index-1]['value'] += value
    else:
        a_t[index-1]['value'] += value
        for i in a_t[index-1]['child']:
            addvalue(a_t,i,value)


for a,b in ab:
    a_t[a - 1]['child'].append(b)
    a_t.append({'value': 0,'child':[]})


for p,x in px:
    addvalue(a_t,p,x)

answer = ""
for ans in a_t:
    answer += str(ans['value']) + " "

print(answer.strip())

Submission status: Runtime Error

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


a_t = [{'value':0,'child':[]}]

def addvalue(a_t,index,value):
    if(len(a_t[index-1]['child']) == 0):
        a_t[index-1]['value'] += value
    else:
        a_t[index-1]['value'] += value
        for i in a_t[index-1]['child']:
            addvalue(a_t,i,value)


for a,b in ab:
    a_t[a - 1]['child'].append(b)
    a_t.append({'value': 0,'child':[]})


for p,x in px:
    addvalue(a_t,p,x)

for ans in a_t:
    print(ans['value'],end=" ")

Submission status: Runtime Error

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


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

pars = [0]*(n+1)
edges = [[] for _ in range(n+1)]
counter = [0]*(n+1)

for _ in range(n-1):
    tmp_e, tmp_ch = map(int, input().split())
    pars[tmp_ch] = tmp_e
    edges[tmp_e].append(tmp_ch)

q_e = []
q_x = []
for _ in range(q):
    tmp_e, x = map(int, input().split())
    counter[tmp_e] += x


def incre(pars, edges, e, counter):
    counter[e] += counter[pars[e]]

    if len(edges[e]) != 0:
        for n_e in edges[e]:
            incre(pars, edges, n_e, counter)


incre(pars, edges, 1, counter)

counter = counter[1:]
counter = [str(c) for c in counter]
print(' '.join(counter))

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())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int, input().split())
    ans[a-1] += b
for i in range(2, n+1):
    ans[i-1] += ans[tree[i]-1]
for i in ans:
    print(i, end=" ")

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())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int, input().split())
    ans[a-1] += b
for i in range(2, n+1):
    ans[i-1] += ans[tree[i]-1]
print(ans)

Submission status: Runtime Error

Solution code:
n, q = map(int, input())
tree = {}
for i in range(n):
    a, b = map(int, input())
    tree[b] = a
ans = [0]*n
for i in range(q):
    a, b = map(int)
    ans[a-1] += b
for i in tree:
    ans[i] += ans[tree[i]]
print(ans)

Submission status: Runtime Error

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


def incre(edges, counter, e, x):
    counter[e] += x

    if edges[e] != 0:
        for n_e in edges[e]:
            incre(edges, counter, n_e, x)


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

edges = [[] for _ in range(n+1)]
counter = [0]*(n+1)

for _ in range(n-1):
    tmp_e, tmp_ch = map(int, input().split())
    if not(tmp_ch in edges[tmp_e]):
        edges[tmp_e].append(tmp_ch)

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

    incre(edges, counter, tmp_e, x)

counter = counter[1:]
counter = [str(c) for c in counter]
print(' '.join(counter))

Submission status: Runtime Error

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


def incre(edges, counter, e, x):
    counter[e] += x

    if edges[e] != 0:
        for n_e in edges[e]:
            incre(edges, counter, n_e, x)


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

edges = [[] for _ in range(n+1)]
counter = [0]*(n+1)

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

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

    incre(edges, counter, tmp_e, x)

counter = counter[1:]
counter = [str(c) for c in counter]
print(' '.join(counter))

Submission status: Runtime Error

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

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

def dfs(v, parent):
    for i in graph[v]:
        if i==parent:
            continue
        counter[i] += counter[v]
        dfs(i, v)
        
dfs(0, -1)
print(*counter)

Submission status: Runtime Error

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

graph = [[] for _ in range(N)]
counter = [0] * 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())
  counter[p-1] += x

def shori(pa,c,val):
  val += counter[c]
  counter[c] = val
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i,val)
  
shori(-1,0,0)
  

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

Submission status: Runtime Error

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

# 添字を1からにしたいのでN+1
graph = [[] for _ in range(N+1)]
counter = [0] * (N+1)

# 各頂点に隣接する頂点
for _ in range(N-1):
  a,b = map(int, input().split())
  graph[a].append(b)
  graph[b].append(a)


# 各頂点に点数を足す
for _ in range(Q):
  p,x = map(int, input().split())
  counter[p] += x

def shori(pa,c,val):
  val += counter[c]
  counter[c] = val
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i,val)
  
shori(0,1,0)
  

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

Submission status: Runtime Error

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

AB = [[int(x) for x in input().split()] for _ in range(N-1)]

# print(AB)

# 添字を1からにしたいのでN+1
graph = [[] for _ in range(N+1)]

# 各頂点に隣接する頂点
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

# print(graph)
counter = [0] * (N+1)

# print(counter)

PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 各頂点に点数を足す
for p,x in PX:
  counter[p] += x


def shori(pa,c,val):
  val += counter[c]
  counter[c] = val
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i,val)
  
shori(0,1,0)
  

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

Submission status: Runtime Error

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

AB = [[int(x) for x in input().split()] for _ in range(N-1)]

# print(AB)

# 添字を1からにしたいのでN+1
graph = [[] for _ in range(N+1)]

# 各頂点に隣接する頂点
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

# print(graph)
counter = [0] * (N+1)

# print(counter)

PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 各頂点に点数を足す
for p,x in PX:
  counter[p] += x


def shori(pa,c,val):
  val += counter[c]
  counter[c] = val
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i,val)
  
shori(0,1)
  

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

Submission status: Runtime Error

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

AB = [[int(x) for x in input().split()] for _ in range(N-1)]

# print(AB)

# 添字を1からにしたいのでN+1
graph = [[] for _ in range(N+1)]

# 各頂点に隣接する頂点
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

# print(graph)
counter = [0] * (N+1)

# print(counter)

PX = [[int(x) for x in input().split()] for _ in range(Q)]

# 各頂点に点数を足す
for p,x in PX:
  counter[p] += x


def shori(pa,c):
  counter[c] += counter[pa]
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i)
  
shori(0,1)
  

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

#     v に訪問済みの印を付ける
#     v を処理する
#     for each v に接続している頂点 i do
#         if i が未訪問 then
#             深さ優先探索(i)

Submission status: Runtime Error

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

inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
def add(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        point[k] += point[num]
        add(k)

add(0)
print(*point)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, Q = read_int()

p = []
x = []

papd = p.append
xapd = x.append

adj = defaultdict(list)
for i in range(N - 1):
    a, b = read_int()
    if a > b: a, b = b, a
    adj[a].append(b)
# adj = dict(adj)


for i in range(Q):
    pi, xi = read_int()
    xapd(xi)
    papd(pi)

counter = [0 for _ in range(N + 5)]
ans = [0 for _ in range(N + 5)]

def rec(i, n):
    """ i : index
        n : partial sum
    """
    ans[i] = n
    if len(adj[i]) == 0: return
    for next in adj[i]:
        rec(next, n + counter[next])



def solve():
    for i in range(Q):
        counter[p[i]] += x[i]
    # print(adj, p, x)
    rec(1, counter[1])

if __name__ == "__main__":
    solve()
    for i in range(1, N + 1):
        if i == N:
            print(ans[i])
            continue
        print(ans[i], end=" ")

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, Q = read_int()

p = []
x = []

papd = p.append
xapd = x.append

adj = defaultdict(list)
for i in range(N - 1):
    a, b = read_int()
    if a > b: a, b = b, a
    adj[a].append(b)
# adj = dict(adj)


for i in range(Q):
    pi, xi = read_int()
    xapd(xi)
    papd(pi)

counter = [0 for _ in range(N + 2)]
ans = [0 for _ in range(N + 2)]

def rec(i, n):
    # i: index, n : partial sum
    ans[i] = n
    if len(adj[i]) == 0: return
    for next in adj[i]:
        rec(next, n + counter[next])



def solve():
    for i in range(Q):
        counter[p[i]] += x[i]
    # print(adj, p, x)
    rec(1, counter[1])

if __name__ == "__main__":
    solve()
    for i in range(1, N + 1):
        if i == N:
            print(ans[i])
            continue
        print(ans[i], end=" ")

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, Q = read_int()

p = []
x = []

papd = p.append
xapd = x.append

adj = defaultdict(list)
for i in range(N - 1):
    a, b = read_int()
    if a > b: a, b = b, a
    adj[a].append(b)


for i in range(Q):
    pi, xi = read_int()
    xapd(xi)
    papd(pi)

counter = [0 for _ in range(N + 1)]
ans = [0 for _ in range(N + 1)]

def rec(i, n):
    # i: index, n : partial sum
    ans[i] = n
    for next in adj[i]:
        # print(next)
        rec(next, n + counter[next])



def solve():
    for i in range(Q):
        counter[p[i]] += x[i]
    # print(adj, p, x)
    rec(1, counter[1])

if __name__ == "__main__":
    solve()
    for i in range(1, N + 1):
        if i == N:
            print(ans[i])
            continue
        print(ans[i], end=" ")

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

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

def add_p(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k] = [x for x in inf[k] if x != num]
        p_list[k] += p_list[num]
        add_p(k)
    return 0

add_p(0)
print(*p_list)

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

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

def add_p(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        p_list[k] += p_list[num]
        add_p(k)
    return 0

add_p(0)
print(*p_list)

Submission status: Runtime Error

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

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

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

def bfs(cur_vrtx, score):
	ret[cur_vrtx] = score + query_ctr[cur_vrtx]
	for adj in adj_vrtx[cur_vrtx]:
		if not visited_vrtx[adj]:
			visited_vrtx[adj] = True
			bfs(adj, ret[cur_vrtx])
			
		
bfs(0, 0)

print(*ret)

Submission status: Runtime Error

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

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

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

def bfs(cur_vrtx, score):
	ret[cur_vrtx] = score + query_ctr[cur_vrtx]
	for adj in adj_vrtx[cur_vrtx]:
		if not visited_vrtx[adj]:
			visited_vrtx[adj] = True
			bfs(adj, ret[cur_vrtx])
			
		
bfs(0, 0)

print(*ret)

Submission status: Runtime Error

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

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

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

def bfs(cur_vrtx, score):
	ret[cur_vrtx] = score + query_ctr[cur_vrtx]
	for adj in adj_vrtx[cur_vrtx]:
		if not visited_vrtx[adj]:
			visited_vrtx[adj] = True
			bfs(adj, ret[cur_vrtx])
			
visited_vrtx[0] = True			
bfs(0, 0)

print(*ret)

Submission status: Runtime Error

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

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

def dfs(child, parent):
    X[child] += X[parent]
    for e in E[child]:
        if e==parent: continue
        dfs(e, child)

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

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n+1)]
p_list = [0 for _ in range(n+1)]

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

def add_p(num, point):
    if len(inf[num]) == 0:
        p_list[num] += point
        return 0
    p_list[num] += point
    for next_num in inf[num]:
        print(next_num, point)
        add_p(next_num, point)
    return 0
for i in range(q):
    p, x = list(map(int, input().split(' ')))
    add_p(p, x)
result = p_list[1:]
result = list(map(str, result))
print(' '.join(result))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self):
        self.oya = None
        self.edge = []
        self.counter = 0

def oyafind(tree, n, reached):
    if tree[n].edge == []:
        return
    for ko in tree[n].edge:
        if reached[ko-1] == 0:
            reached[ko-1] = 1
            tree[ko].oya = n
            oyafind(tree, ko, reached)

[N, Q] = [int(i) for i in input().split()]
tree = {}
edges = [[]]*(N-1)

for i in range(1, N+1):
    tree[i] = Node()

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

reached = [0]*N

oyafind(tree, 1, reached)

for i in range(Q):
    [p, x] = [int(i) for i in input().split()]
    tree[p].counter += x

ans = [0]*N
ans[0] = str(tree[1].counter)

for i in range(2, N+1):
    tree[i].counter += tree[tree[i].oya].counter
    ans[i-1] = str(tree[i].counter)

print(' '.join(ans))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self):
        self.oya = None
        self.edge = []
        self.counter = 0

def oyafind(tree, n, reached):
    for ko in tree[n].edge:
        if reached[ko-1] == 0:
            reached[ko-1] = 1
            tree[ko].oya = n
            oyafind(tree, ko, reached)

[N, Q] = [int(i) for i in input().split()]
tree = {}
edges = [[]]*(N-1)

for i in range(1, N+1):
    tree[i] = Node()

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

reached = [0]*N

oyafind(tree, 1, reached)

for i in range(Q):
    [p, x] = [int(i) for i in input().split()]
    tree[p].counter += x

ans = [0]*N
ans[0] = str(tree[1].counter)

for i in range(2, N+1):
    tree[i].counter += tree[tree[i].oya].counter
    ans[i-1] = str(tree[i].counter)

print(' '.join(ans))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self):
        self.oya = None
        self.counter = 0

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

for i in range(1, N+1):
    tree[i] = Node()

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

for i in range(Q):
    [p, x] = [int(i) for i in input().split()]
    tree[p].counter += x

ans = [0]*N
ans[0] = str(tree[1].counter)

for i in range(2, N+1):
    tree[i].counter += tree[tree[i].oya].counter
    ans[i-1] = str(tree[i].counter)

print(' '.join(ans))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self):
        self.oya = None
        self.ko = []
        self.counter = 0

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

for i in range(1, N+1):
    tree[i] = Node()

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

for i in range(Q):
    [p, x] = [int(i) for i in input().split()]
    tree[p].counter += x

ans = [str(tree[1].counter)]

for i in range(2, N+1):
    tree[i].counter += tree[tree[i].oya].counter
    ans.append(str(tree[i].counter))

print(' '.join(ans))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self):
        self.oya = None
        self.ko = []
        self.counter = 0

def operation(tree, node, op):
    if node.ko == []:
        node.counter += op[1]
        return
    else:
        node.counter += op[1]
        for ko in node.ko:
            operation(tree, tree[ko], op)
        

[N, Q] = [int(i) for i in input().split()]
tree = {}
ops = []

for i in range(N):
    tree[i+1] = Node()

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

for i in range(Q):
    ops.append([int(i) for i in input().split()])

for op in ops:
    operation(tree, tree[op[0]], op)

ans = []
 
for i in range(1, N+1):
    ans.append(str(tree[i].counter))
    
print(' '.join(ans))

Submission status: Runtime Error

Solution code:

def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    nn = len(n_data)
    for n1 in range(0, nn - 1):
        for n2 in range(n1 + 1, nn):
            if n_data[n1][0] > n_data[n2][0]:
                n_data[n1], n_data[n2] = n_data[n2], n_data[n1]

    t_index = {}
    #for i in range(1, n + 1):
    #    t_index[i] = []

    """
    1-2 2-3 2-4 3-5
    1-2-3-5
       -4

    2 1
    3 2

    """
    for n in n_data:
        if n[0] in t_index.keys():
            t_index[n[0]] = [n[1]]
        else:
            t_index[n[0]].append(n[1])
        for k in t_index.keys():
            if n[0] in t_index[k]:
                t_index[k].append(n[1])

    return ""


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Runtime Error

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

for i in range(1, N+1):
  c[i] += c[child_per[i]]
  print(c[i], '', end='')

Submission status: Runtime Error

Solution code:
def dfs(s,co):
    if s not in visited:
        visited[s]=1
        if s in v:
            co+=v[s]
        c[s]=co
        for u in adj[s]:
            dfs(u,co)
        
n,q=map(int,input().split())
adj={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a not in adj:
        adj[a]=[b]
    else:
        adj[a].append(b)
    if b not in adj:
        adj[b]=[a]
    else:
        adj[b].append(a)
c={}
visited={}
v={}
for i in range(1,1+n):
    c[i]=0
for w in range(q):
    a,co=map(int,input().split())
    if a not in v:
        v[a]=co
    else:
        v[a]+=co
dfs(1,0)
for i in range(1,1+n):
    print(c[i],end=" ")

Submission status: Runtime Error

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

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

def dfs(v, parent):
    for i in graph[v]:
        if i==parent:
            continue
        counter[i] += counter[v]
        dfs(i, v)
        
dfs(0, -1)
print(*counter)

Submission status: Runtime Error

Solution code:
NQ=list(map(int,input().split()))
edge=[[] for i in range(NQ[0]+1)]#N+1こ宣言し、０番目を無視することで、人間の●番目に等しい感覚でプログラムが書ける。
counter=[0]*(NQ[0]+1)
for i in range(NQ[0]-1):
    s=list(map(int,input().split()))
    edge[s[0]]=edge[s[0]]+[ edge[s[1]]]
    edge[s[1]]=edge[s[1]]+[ edge[s[0]]]#以上で、点がどことつながっているのかをedgeとして保存した。
for k in range(NQ[0]-1):
    m=list(map(int,input().split()))
    counter[m[0]]=counter[m[0]]+counter[m[1]]#以上で、最上位カウンターに数値をたす作業が終わった。

tmp = [1]
while len(tmp) != 0:
    tmp2 = tmp
    tmp = []
    for oya in tmp2:
        counter[int(oya)] = 1
        for k in edge[oya]:
            if check[k-1] == 0:
                counter[k-1] += counter[oya-1]
                tmp.append(k)
 
 
print(" ".join(map(str, counter)))

Submission status: Runtime Error

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

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

def dfs(v, prev):
    for i in to[v]:
        if prev == i:
            continue
        w[i] += w[v]
        dfs(i, v)

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

Submission status: Runtime Error

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

PQ = [list(map(int, input().split())) for _ in range(Q)]
w = [0] * N
for p, x in PQ:
    w[p-1] += x
def dfs(v, prev, to):
    for i in to[v]:
        if prev == i:
            continue
        w[i] += w[v]
        dfs(i, v, to)   

dfs(0, -1, to)
ret = ""
for i in w:
    ret += str(i)
    ret += " "
print(ret)

Submission status: Runtime Error

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

PQ = [list(map(int, input().split())) for _ in range(Q)]
w = [0] * N
for p, x in PQ:
    w[p-1] += x
def dfs(v, prev, to):
    for i in to[v]:
        if prev == i:
            continue
        w[i] += w[v]
        dfs(i, v, to)   

dfs(0, -1, to)
ret = ""
for i in w:
    ret += str(i)
    ret += " "
print(ret[:-1])

Submission status: Runtime Error

Solution code:
def dfs(s,co):
    if s not in visited:
        visited[s]=1
        if s in v:
            co+=v[s]
        c[s]=co
        for u in adj[s]:
            dfs(u,co)
            
n,q=map(int,input().split())
adj={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a not in adj:
        adj[a]=[b]
    else:
        adj[a].append(b)
    if b not in adj:
        adj[b]=[a]
    else:
        adj[b].append(a)
c={}
visited={}
v={}
for i in range(1,1+n):
    c[i]=0
for w in range(q):
    a,co=map(int,input().split())
    v[a]+=co
dfs(1,0)
for i in range(1,1+n):
    print(c[i],end=" ")

Submission status: Runtime Error

Solution code:
def dfs(s,co):
    if s not in visited:
        visited[s]=1
        if s in v:
            co+=v[s]
        c[s]=co
        for u in adj[s]:
            dfs(u,co)
            
n,q=map(int,input().split())
adj={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a not in adj:
        adj[a]=[b]
    else:
        adj[a].append(b)
    if b not in adj:
        adj[b]=[a]
    else:
        adj[b].append(a)
c={}
visited={}
v={}
for i in range(1,1+n):
    c[i]=0
for w in range(q):
    a,co=map(int,input().split())
    v[a]=co
dfs(1,0)
for i in range(1,1+n):
    print(c[i],end=" ")

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
bran_a = []
bran_b = []
list_p = []
list_x = []

for i in range(n-1):
  a,b = map(int, input().split())
  bran_a.append(a)
  bran_b.append(b)
  
for i in range(q):
  p,x = map(int, input().split())
  list_p.append(p)
  list_x.append(x)

root = []
ans = []
for i in range(1,n+1): #それぞれの頂点について調べる
  if i==1:
    root.append([1])
  else: #どの部分木に含まれるかのリストを作る
  	temp = [i]
  	idx = bran_b.index(i)
  	before = bran_a[idx]
  	temp = temp + root[bran_a[idx]-1]
  	root.append(temp)
  pre_ans = 0
  for j in range(q): #１つずつ加算していく
    if root[i-1].count(list_p[j])==1:
      pre_ans += list_x[j]
  ans.append(pre_ans)
  print(pre_ans, end=" ")