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: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
 
N,Q = (int(i) for i in input().split())
hen = {}
value = [0] * N

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

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

def add(p,x):
        for node in hen[p]:
            value[node-1] = value[node-1] + x
            if node in hen:
                add(node,value[node-1])

#add(1,value[0])
for i in range(0,N):
    if i+1 in hen:
        for node in hen[i+1]:
            value[node-1] += value[i]

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
l = [[] for _ in range(n)]
vals = [0] * (n)
for _ in range(n-1):
    e, f = map(int, input().split())
    l[e-1].append(f-1)
for _ in range(q):
    e, f = map(int, input().split())
    vals[e-1] += f
for i, e in enumerate(l):
    for x in e:
        vals[x] += vals[i]
print(*vals)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ki = [[] for i in range(N+1)]
action = []
ans = [0] * (N+1)
for _ in range(N-1):
    array = list(map(int, input().split()))
    ki[array[0]].append(array[1])
for i in range(Q):
    array = list(map(int, input().split()))
    action.append(array)
    ans[array[0]] += array[1]

for i in range(1,N+1):
	for k in range(len(ki[i])):
		ans[ki[i][k]] += ans[i]

for i in range(1,N+1):
	print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
from pprint import pprint
from collections import deque, defaultdict


def solve(n, q, edges, p):
    counter = defaultdict(int)

    parents = deque()
    parents.append(0)
    while parents:
        parent = parents.popleft()
        counter[parent] += p[parent]
        # print("parent")
        # print(parent, counter[parent])
        if parent in edges:
            for child in edges[parent]:
                if counter[child] != 0:
                    continue
                counter[child] = counter[parent]
                # print("child")
                # print(child, counter[child])
                parents.append(child)

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

if __name__ == "__main__":
    n, q = list(map(int, input().strip().split(" ")))
    edges = defaultdict(list)
    for _ in range(n-1):
        a_i, b_i = list(map(int, input().strip().split(" ")))
        edges[a_i-1].append(b_i-1)
        edges[b_i-1].append(a_i-1)

    p = defaultdict(int)
    x = []
    for _ in range(q):
        p_i, x_i = list(map(int, input().strip().split(" ")))
        p[p_i-1] += x_i

    solve(n, q, edges, p)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
def main():
  _map = map
  _input = input
  _int = int
  def calc_results(results, pvalue, i):
    results[i] = pvalue + values[i]
    for j in childrens[i]:
      calc_results(results, results[i], j)
  n, q = _map(_int, _input().split())
  values = [0] * (n + 1)
  results = [0] * (n + 1)
  childrens = [[] for _ in range(n + 1)]
  for i in range(n - 1):
    a, b = _map(_int, _input().split())
    childrens[a].append(b)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p] += x
  calc_results(results, 0, 1)
  print(*[results[i] for i in range(1, n + 1)])
main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
l = []
vals = [0] * (n)
for _ in range(n-1):
    e, f = map(int, input().split())
    l.append((e, f))
for _ in range(q):
    e, f = map(int, input().split())
    vals[e-1] += f
for i, e in sorted(l): vals[e-1] += vals[i-1]
print(' '.join(map(str, vals)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, q = map(int, input().split())
counter = [0]*n
edges = {}

def have(p, add_lst):
    if p not in edges.keys():
        return
    for i in edges[p]:
        add_lst += [i]
        have(i, add_lst)


for i in range(n-1):
    a, b = map(int, input().split())
    if a in edges.keys():
        edges[a] += [b]
    else:
        edges[a] = [b]

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

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

Submission status: Wrong Answer

Solution code:
n, q = map(int,input().split())
top_list = [list(map(int,input().split())) for _ in range(n - 1)]
ope_list = [list(map(int,input().split())) for _ in range(q)]

ans = [0] * n

for i in ope_list:
    ans[i[0] - 1] += i[1]

for j in top_list:
	ans[j[1] - 1] += ans[j[0]-1]
    
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,q=map(int,input().split())
Edges=defaultdict(list)
for _ in range(n-1):
    a,b=map(int,input().split())
    Edges[a-1].append(b-1)
Nodes = [0]*n
for _ in range(q):
    p,x=map(int,input().split())
    Nodes[p-1] += x

def dfs(Edges,Nodes):
    stack = []
    stack.append((0, 0))

    while len(stack)>0:
        current, preval = stack.pop()
        Nodes[current] += preval
        preval = Nodes[current]
        for i in Edges[current]:
            stack.append((i, preval))
    return Nodes

Nodes = dfs(Edges,Nodes)

print(" ".join(list(map(str,Nodes))))

Submission status: Wrong Answer

Solution code:
from pprint import pprint
from collections import deque, defaultdict


def solve(n, q, edges, p):
    counter = defaultdict(int)

    parents = deque()
    parents.append(0)
    while parents:
        parent = parents.popleft()
        counter[parent] += p[parent]
        # print("parent")
        # print(parent, counter[parent])
        if parent in edges:
            for child in edges[parent]:
                if counter[child] != 0:
                    continue
                counter[child] = counter[parent]
                # print("child")
                # print(child, counter[child])
                parents.append(child)

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

if __name__ == "__main__":
    n, q = list(map(int, input().strip().split(" ")))
    edges = defaultdict(list)
    for _ in range(n-1):
        a_i, b_i = list(map(int, input().strip().split(" ")))
        edges[a_i-1].append(b_i-1)

    p = defaultdict(int)
    x = []
    for _ in range(q):
        p_i, x_i = list(map(int, input().strip().split(" ")))
        p[p_i-1] += x_i

    solve(n, q, edges, p)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
 
N,Q = (int(i) for i in input().split())
hen = {}
value = [0] * N
for _ in range(0,N-1):
    a,b = (int(i) for i in input().split())
    if a in hen:
        hen[a].append(b)
    else:
        hen[a] = [b]

def add(p,x):
        for i in hen[p]:
            value[i-1] = value[i-1] + x
            if i in hen:
                add(i,value[i-1])

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


add(1,value[0])
print(' '.join(map(str,value)))

Submission status: Wrong Answer

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

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

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


for i in range(1,n):

    for y in r[i]:
        v[y] += v[i]

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
#%%
def dfs(gr,li):
    st = []
    seen = set()
    st.append([0,-1])
    while(st):
        x,fr = st.pop()
        if(x in seen):continue
        seen.add(x)
        if(fr != -1):
            li[x]+=li[fr]
        if(len(gr[x])==1):
            continue
        for i in gr[x]:
            if(i==fr):continue
            st[0:0] = [[i,x]]

#%%
def main():
    N,Q = [int(x) for x in input().split()]
    li = [0]*N
    gp = [[] for _ in range(N)]
    for _ in range(N-1):
        a,b = input().split()
        a = int(a)
        b = int(b)
        gp[a-1].append(b-1)
        gp[b-1].append(a-1)

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

    dfs(gp,li)
    print(*li)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
parentNodes = [0] * (N+1) #i番目のノードの親番号
counter = [0] * N

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ki = []
action = []
ans = [0] * (N+1)
for _ in range(N-1):
    array = list(map(int, input().split()))
    ki.append(array)
for i in range(Q):
    array = list(map(int, input().split()))
    action.append(array)
    ans[array[0]] += array[1]

for k in range(N-1):
	ans[ki[k][1]] += ans[ki[k][0]]

for i in range(1,N+1):
	print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ki = []
action = []
ans = [0] * (N+1)
for _ in range(N-1):
    array = list(map(int, input().split()))
    ki.append(array)
for i in range(Q):
    array = list(map(int, input().split()))
    action.append(array)
    ans[array[0]] += array[1]

for k in range(N-1):
	ans[ki[k][1]] += ans[ki[k][0]]

print(ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int,input().split())
top_list = [list(map(int,input().split())) for i in range(n - 1)]
ope_list = [list(map(int,input().split())) for i in range(q)]
ans = [0] * n
 
"""
for i in ope_list:
    ans[i[0] - 1] += i[1]
 
for j in range(n):
    for k in top_list:
        if j + 1 == k[1]:
            ans[j] += ans[k[0] - 1]
"""
 
print(*ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
n,q = map(int,input().split())
node_list = list(map(str, [input() for i in range(n-1)]))
q_list = list(map(str, [input() for i in range(q)]))
# materials = list(map(int,input().split()))
node_list.sort()
q_list.sort()
values = [0] * n

for j in range(len(q_list)):
	a,b = map(int, q_list[j].split())
	values[a-1] = values[a-1] + b

for i in range(len(node_list)):
	c,d = map(int, node_list[i].split())
	values[i+1] = values[i+1] + values[c-1]
			
print(" ".join(map(str,values)))

Submission status: Wrong Answer

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

dictionary = [[] for i in range(N + 1)]

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

num = [0] * (N  + 1)

for i in range(Q):
    p, x = map(int, input().split())
    num[p] = x

for i in range(1, N + 1):
    print(num[i])
    for j in range(0, len(dictionary[i])):
        num[dictionary[i][j]] += num[i]

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
edge = [[] for i in range(N)]
q = [0 for i in range(N)]

for i in range(N-1):
  a, b = map(int, input().split())
  edge[a-1].append(b)
for i in range(Q):
  a, v = map(int, input().split())
  q[a-1] += v

for i, v in enumerate(q):
  print(i, v)
  for j in edge[i]:
    print(j)
    q[j-1] += v
print(*q)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n-1)]
l2=[list(map(int,input().split())) for i in range(q)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
L4=[0 for i in range(n)]
for i in range(q):
  L4[l2[i][0]-1]+=l2[i][1]
distance=[0 for i in range(n)]
distance[0]=L4[0]

L=[0]
L2=[]
L3=[]
while len(L2)!=n and len(L)!=0:
  for j in range(len(L)):
    for k in range(len(connection[L[j]])):
      distance[connection[L[j]][k]]=distance[L[j]]+L4[connection[L[j]][k]]
      L3.append(connection[L[j]][k])
    L2.append(L[j])
  L=L3
  L3=[]
for i in range(len(distance)):
  print(distance)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, Q = input().split(' ')
N = int(N)
Q = int(Q)
ab = [input().split(' ') for i in range(N - 1)]
px = [input().split(' ') for i in range(Q)]

edge_list = sorted(ab)
start_edge_list = [e[0] for e in edge_list]

effect_weight = defaultdict(lambda: 0)
for impact in px:
    top_n = impact[0]
    val = int(impact[1])
    effect_weight[top_n] += val

result = [0 for i in range(N)]
for top_n, weight in effect_weight.items():
    result[int(top_n) - 1] = weight

weight_me = []

for effect in ab:
    e_from = effect[0]
    e_to = effect[1]
    weight = result[int(e_from) - 1]
    result[int(e_to) - 1] += weight

result_str = [str(i) for i in result]
print(' '.join(result_str))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
n,q = map(int,input().split())
node_list = list(map(str, [input() for i in range(n-1)]))
q_list = list(map(str, [input() for i in range(q)]))
# materials = list(map(int,input().split()))
node_list.sort()
q_list.sort()
values = [0] * n

for j in range(len(q_list)):
	a,b = map(int, q_list[j].split())
	values[a-1] = values[a-1] + b

for i in range(len(node_list)):
	c,d = map(int, node_list[i].split())
	values[d-1] = values[d-1] + values[c-1]
			
print(" ".join(map(str,values)))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(N-1)]
px = [list(map(int, input().split())) for i in range(Q)]
cnt = [0] * N

for p, x in px:
    cnt[p-1] += x
for a, b in ab:
    cnt[b-1] += cnt[a-1]

print(*cnt)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


class Node:
    def __init__(self, parent_id=None):
        self.val = 0
        self.parent_id = parent_id


n, q = map(int, input().split())
tree = [Node() for _ in range(n)]

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

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

for i in range(1, n):
    tree[i].val += tree[tree[i].parent_id].val

print(*[tree[i].val for i in range(n)])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys

n,q = [ int(x) for x in sys.stdin.readline().strip().split() ]
edge= [ [] for _ in range(n-1) ]
for i in range(n-1):
    edge[i] = [ int(x)-1 for x in sys.stdin.readline().strip().split() ]
#
c= [ 0 for _ in range(n) ]
for i in range(q):
    p,x= [ int(x) for x in sys.stdin.readline().strip().split() ]
    c[p-1]+= x
#
for a,b in edge: c[b]+= c[a]

for ci in c: print( ci,end=" " )

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
sys.setrecursionlimit(10**8)
input = lambda: sys.stdin.readline()[:-1]
cnt=Counter()
data={}
n,q=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(n-1)]
px=[tuple(map(int,input().split())) for i in range(q)]
ab.sort(key=lambda tup:(tup[0],tup[1]))
a=[ab[i][0] for i in range(n-1)]
b=[ab[i][1] for i in range(n-1)]
data[1]=[b[0],0]

for i in range(2,n+1):
	try :data[i]=[b[a.index(i)]]
	except ValueError:data[i]=[0]
	try:
		if a[b.index(i)]==a[b.index(i)+1]:
			data[i].append(b[b.index(i)+1])
		else:
			data[i].append(0)
	except IndexError:	data[i].append(0)
	
def plus(number,value):
	cnt[number]+=value
	if data[number][0]!=0:
		plus(data[number][0],value)
	if data[number][1]!=0:
		plus(data[number][1],value)
for i in px:
	cnt[i[0]]+=i[1]
	if data[i[0]][0]!=0:
		plus(data[i[0]][0],i[1])
ans=list(cnt.items())
ans.sort(key=lambda tup:tup[0])
ans = [str(i[1]) for i in ans]
print(' '.join(ans))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


def main():
    n, q = [int(i) for i in input().split()]
    scores = [0] * (n + 1)
    lines = sorted([[int(i) for i in input().split()] for _ in range(n - 1)])
    counter = [[int(i) for i in input().split()] for _ in range(q)]
    for index, score in counter:
        scores[index] += score

    print(lines)
    for a, b in lines:
        scores[b] += scores[a]
    print(*scores[1:])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split(" "))
A = []

B = [0 for j in range(N)]
for i in range(N-1):
    tmp = list(map(int,input().split(" ")))
    A.append(tmp)

for j in range(Q):
    tmp = list(map(int,input().split(" ")))
    B[tmp[0]-1] += tmp[1]

for i in range(N-1):
        B[A[i][1]-1] += B[A[i][0]-1]

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

Submission status: Wrong Answer

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

hen = []
result = [0]*(n+1)

for num in range(n-1):
  hen.append(map(int,input().split()))

for num in range(q):
  a1,b1 = map(int,input().split())
  result[a1] += b1

for a,b in hen:
  result[b] = result[a]
  
print(*result[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
tree = list()
ope = list()
cnt = [0] * N
for i in range(N-1):
    tree.append(list(map(int, input().split())))
for i in range(Q):
    ope.append(list(map(int, input().split())))

cnt = [0] * N
for p, a in ope:
    cnt[p-1] += a
for p, c in tree:
    cnt[c-1] += cnt[p-1]

print(*cnt)

Submission status: Wrong Answer

Solution code:
def main():
  _map, _range, _int = map, range, int
  n, q = _map(_int, input().split())
  values = [0] * (n + 1)
  parents = [0] * (n + 1)
  for i in _range(n - 1):
    a, b = _map(_int, input().split())
    parents[b] = a
  for _ in _range(q):
    p, x = _map(_int, input().split())
    values[p] += x
  for i in _range(1, n + 1):
    values[i] += values[parents[i]]
  print(*values[1:])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s = input()
t = input()
len_s = len(s)
len_t = len(t)
a = [-100]
ans = 0
ng_count = 0
s_raw = s
d = []
for i in range(len_t):
    b = s.find(t[i])
    # print(b)
    if b == -1:
        e = s_raw.find(t[i])
        if e!=-1:
            d.append(e)
        else:
            print(-1)
            exit()
    else:
        if b!=len_t:
            s = s[:b] + "?" + s[b+1:]
        else:
            s = s[:b] + "?"
        # print(t)
        d.append(b)
        # print(d)
if len(d) == 1:
    print(d[0]+1)
    exit()
else:
    for i in range(1,len_t):
        if d[i-1] >= d[i]:
            ng_count += 1
        ans = ng_count*len_s + d[i]
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
n, q = map(int,input().split())
d = [[]]*n
for i in range(n-1):
    ai, bi = map(int,input().split())
    d[ai-1].append(bi-1)
    d[bi-1].append(ai-1)
#point
px = [0]*n
for i in range(q):
    pi,xi = map(int,input().split())
    px[pi-1] += xi
sp = [0]*n
s = [0]
visited = [False]*n
while len(s) > 0:
    x = s.pop(0)
    visited[x] = True
    sp[x] += px[x]
    for xc in d[x]:
        if not visited[xc]:
            s.append(xc)
            sp[xc] += sp[x]
for i in range(n):
    print(sp[i])

Submission status: Wrong Answer

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

g = {}
for i in range(n):
	g[i + 1] = {i + 1}

for i in range(n - 1):
	x, y = map(int, input().split())
	g[x].add(y)

for i in g.keys():
	for j in g[i]:
		if i != j:
			g[i] = g[i].union(g[j])

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

for i in op:
	print(i, end= ' ')

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, parent, num):
        self.parent = parent
        self.num = num
        self.score = 0

    def add_score(self, score):
        self.score += score

class Tree:
    @classmethod
    def calc_sum(cls, node):
        if node.parent:
            return node.score + cls.calc_sum(node.parent)
        else:
            return node.score

N, Q = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(1, N):
    A[i], B[i] = map(int, input().split())

P = [0] * (Q+1)
X = [0] * (Q+1)
for i in range(1, Q+1):
    P[i], X[i] = map(int, input().split())

nodes = [0] * (N+1)
nodes[1] = Node(None, 1)

for i in range(1, N):
    nodes[B[i]] = Node(nodes[A[i]], B[i])

for i in range(1, Q+1):
    nodes[P[i]].add_score(X[i])

for i in range(1, N+1):
    sum_score = Tree.calc_sum(nodes[i])
    print(sum_score, end=' ')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200005)

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, q = na()
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = na()
    a -= 1
    b -= 1
    g[a].append(b)
    g[b].append(a)

ps = [0] * n
for i in range(q):
    p, x = na()
    p -= 1
    ps[p] += x

ans = [0] * (n+1)

# def dfs(cur, pre):
#     global ps, ans, g
#     ans[cur] = ans[pre] + ps[cur]
#     for e in g[cur]:
#         if e != pre:
#             dfs(e, cur)
#
# dfs(0, -1)

print(" ".join(map(str, ans[0:-1])))

Submission status: Wrong Answer

Solution code:
NQ = input().split(' ')
N = int(NQ[0])
Q = int(NQ[1])
candidate_list = []
tree_list = [[]] * N
#Make tree dict
for i in range(N-1):
    ab = input().split(' ')
    a = int(ab[0])
    b = int(ab[1])
    candidate_list.append((a,b))
candidate_list.reverse()
for candidate in candidate_list:
    a = candidate[0]
    b = candidate[1]
    tree_list[a-1] = tree_list[a-1] + tree_list[b-1]
    tree_list[a-1].append(b)
out_put_list = [0] * N
for _ in range(Q):
    px = input().split(' ')
    p = int(px[0])
    x = int(px[1])
    out_put_list[p-1] += x
    for idx in tree_list[p-1]:
        out_put_list[idx-1] += x

print(*out_put_list)

Submission status: Wrong Answer

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            if current_node >= len(info_lst) - 1: ##########
                pass ############
            else:   ###########
                info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
for _ in range(N-1): # 10**5 # ここじゃない
    ai, bi = map(int, input().split())
    info_lst[ai].append(bi)
    info_lst[bi].append(ai)

passed_place = [False] * (N+1)
passed_place[1] = True

output_str = "" #####################
try:
    glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない
#########

    pi_xi_sum_lst = [0]*(N+1)
    for _ in range(Q): # 10**5
        pi, xi = map(int, input().split())
        pi_xi_sum_lst[pi] += xi

    output_lst = [0]*(N+1)
    count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
    #output_str = ""
    for i in range(1, N+1): # ここじゃない
        output_str += str(output_lst[i]) + " "
except:
    pass
print(output_str)

Submission status: Wrong Answer

Solution code:
input_line = list(map(int,input().split()))
tree_info = []
for i in range(input_line[0]-1):
    info = list(input().split())
    tree_info.append(info)
counter = [0] * input_line[0]
for i in range(input_line[1]):
    count_info = list(map(int, input().split()))
    counter[count_info[0]-1] += count_info[1]
for i in range(1,len(counter)):
    counter[i] += counter[int(tree_info[i-1][0])-1]
print(" ".join(list(map(str,counter))))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n,q = map(int, input().split())
ans = dict.fromkeys(list(range(1,n+1)),0)

li = {i:i for i in range(1,n+1)}
li = defaultdict(set)

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

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

    if p == 1:
        for m in range(1,n+1):
            ans[m] += x
        continue

    if p in li:
        ans[p] += x
        for i in li[p]:
            ans[i] += x

    else:
        ans[p] += x

ans = list(ans.values())
print(" ".join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
N, Q = (int(i) for i in input().split())
ab = [[int(i) for i in input().split()] for i in range(N-1)] 
px = [[int(i) for i in input().split()] for i in range(Q)] 
ab.sort()

weight = [0]*N
for i in range(len(px)):
	weight[px[i][0]-1]+=px[i][1]

for g in range(len(ab)):
	weight[ab[g][1]-1] += weight[ab[g][0]-1]

print(weight)

Submission status: Wrong Answer

Solution code:
s = [int(i) for i in input().split()]
N = s[0]
Q = s[1]
ab = [[int(i) for i in input().split()] for j in range(N-1)]
px = [[int(i) for i in input().split()] for j in range(Q)]
c = [0 for i in range(N)]

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

for i in range(N-1):
    c[ab[i][1]-1] += c[ab[i][0]-1]

print(*c)

Submission status: Wrong Answer

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            if next_node in info_lst[current_node]: #########
                info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
for _ in range(N-1): # 10**5 # ここじゃない
    ai, bi = map(int, input().split())
    info_lst[ai].append(bi)
    info_lst[bi].append(ai)

output_str = "" #####################

try:
    passed_place = [False] * (N+1)
    passed_place[1] = True
    glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない
#########

    pi_xi_sum_lst = [0]*(N+1)
    for _ in range(Q): # 10**5
        pi, xi = map(int, input().split())
        pi_xi_sum_lst[pi] += xi

    output_lst = [0]*(N+1)
    count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
    #output_str = ""
    for i in range(1, N+1): # ここじゃない
        output_str += str(output_lst[i]) + " "
except:
    pass
print(output_str)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())

def main():
  N, Q = na()

  ab = []
  for _ in range(N-1):
    a, b = na()
    ab.append([a, b])
  ab.sort()
  
  counter = [0] * (N+1)
  for _ in range(Q):
    p, x = na()
    counter[p] += x
  for i in range(N - 1):
    counter[ab[i][1]] += counter[ab[i][0]]
  print(*counter[1:])
  print(ab)

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
T = [[0, [], []] for _ in range(N+1)] #counter, parents, children
E = sorted([list(map(int, input().split())) for _ in range(N-1)], key=lambda x:x[0])
for e in E:
  p = e[0] if e[0] in T[1][2] else e[1]
  if 1 in e:
    p = 1
  c = e[0]+e[1]-p
  T[c][1] += T[p][1]
  T[c][1].append(p)
  for q in T[c][1]:
    T[q][2].append(c)
for _ in range(Q):
  p, x = map(int, input().split())
  T[p][0] += x
  for c in T[p][2]:
    T[c][0] += x
ans_s = ""
for t in T[1:]:
  ans_s += "%d "%(t[0]-1)
print(ans_s)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [tuple(map(int, input().split())) for _ in range(N-1)]
W = [tuple(map(int, input().split())) for _ in range(Q)]

# 親に付いている重み
r = [0,] * (N+1)
for p, x in W:
    r[p] += x

# 木の隣接関係 (a<bは保証されている)
for a, b in G:
    r[b] += r[a]

print(*r[1:])

Submission status: Wrong Answer

Solution code:

def count_add(info_lst, pi_xi_sum_lst, output_lst, current_node, adding):
    output_lst[current_node] += adding
    for i in info_lst[current_node]:   #先の枝についても再帰
        adding += pi_xi_sum_lst[i]
        count_add(info_lst, pi_xi_sum_lst, output_lst, i, adding)
        adding -= pi_xi_sum_lst[i]

def glaph_2_tree(info_lst, passed_place, current_node):
    next_nodes_lst = info_lst[current_node][:]

    for next_node in next_nodes_lst:
        if passed_place[next_node]: # 通ったことがあったら、
            info_lst[current_node].remove(next_node)
        else:
            passed_place[next_node] = True
            glaph_2_tree(info_lst, passed_place, next_node)
            passed_place[next_node] = False
    passed_place[next_node] = True

N, Q = map(int, input().split())
info_lst = [[] for _ in range(N+1)]
for _ in range(N-1): # 10**5 # ここじゃない
    ai, bi = map(int, input().split())
    info_lst[ai].append(bi)
    info_lst[bi].append(ai)

output_str = "" #####################

try:
    passed_place = [False] * (N+1)
    passed_place[1] = True
    glaph_2_tree(info_lst, passed_place, 1) # 10**5 # ここじゃない


    pi_xi_sum_lst = [0]*(N+1)
    for _ in range(Q): # 10**5
        pi, xi = map(int, input().split())
        pi_xi_sum_lst[pi] += xi

    output_lst = [0]*(N+1)
    count_add(info_lst, pi_xi_sum_lst, output_lst, current_node=1, adding=pi_xi_sum_lst[1]) # ここじゃない
    #output_str = ""
    for i in range(1, N+1): # ここじゃない
        output_str += str(output_lst[i]) + " "
except:
    pass
print(output_str)