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
input = sys.stdin.readline

#入力
n,q = map(int,input().split())
ab = [ list(map(int,input().split(" "))) for _ in range(n-1)]
ab.sort()
p,x = [0]*q,[0]*q
for t in range(q):
  p[t],x[t] = map(int,input().split())
   
#計算    
z = [0]*n
for k in range(q):
  z[p[k]-1] = z[p[k]-1] + x[k]
w = z[:]
for l in range(n-1):
  if z[ab[l][0]-1] > w[ab[l][0]-1] or ab[l][0] == 1:
    z[ab[l][1]-1] += z[ab[l][0]-1]
  else:
    z[ab[l][0]-1] += z[ab[l][1]-1]
  
#出力  
print(*z)

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline

from collections import defaultdict

def extract_tree(tree, i, memo):
    if memo[i] != []:
        return memo[i]

    if tree[i] == -1:
        return [[], memo]

    children = []
    for child in tree[i]:
        children.append(child)
        [tmp, memo] = extract_tree(tree, child, memo)
        if tmp != []:
            # flatten
            for x in tmp:
                children.append(x)

    memo[i] = children
    return [children, memo]

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

tree = [-1] * (N+1)
point = [0] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    if a > b:
        a, b = b, a

    if tree[a] == -1:
        tree[a] = [b]
    else:
        tree[a] += [b]

memo = defaultdict(list)
[_, memo] = extract_tree(tree, 1, memo)

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

# 面倒なので、1-index にして、最後に pop(0)することで辻褄を合わせる
for p, x in dd.items():
    point[p] += x

    if memo[p] != []:
        for i in memo[p]:
            point[i] += x


point.pop(0)
print(*point)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

prev = [False for i in range(num+1)]
prev[1] = True

for i in range(num-1):
  a, b = map(int, input().split())
  if prev[a]:
    ki[a].append(b)
  else:
    ki[b].append(a)
  prev[a] = True
  prev[b] = True

  
c = [0 for i in range(num+1)]

for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

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

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

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

queue = [0]
while queue:
    a = queue.pop()
    for i in G[a]:
        ans[i] += ans[a]
        if G[i]:
            queue.append(i)

print(*ans)

Submission status: Wrong Answer

Solution code:
#入力
n,q = map(int,input().split())
ab = []
for s in range(n-1):
    a1,b1=[int(i) for i in input().split()]
    ab.append([a1,b1])
ab = sorted(ab)
p,x = [],[]
for t in range(q):
    p1,x1=[int(j) for j in input().split()]
    p.append(p1)
    x.append(x1)

#計算    
z = [0]*n
for k in range(q):
  z[p[k]-1] = z[p[k]-1] + x[k]
w = z[:]
for l in range(n-1):
  if z[ab[l][0]-1] > w[ab[l][0]-1] or (ab[l][0]-1) == 0:
    z[ab[l][1]-1] = z[ab[l][1]-1] + z[ab[l][0]-1]
  else:
    z[ab[l][0]-1] = z[ab[l][0]-1] + z[ab[l][1]-1]
  
#出力  
print(*z)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
AB = [[] for _ in range(n)]
cnt = [[0] for _ in range(n)]
res = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    AB[a-1] += [b]
for i in range(q):
    p,x = map(int,input().split())
    cnt[p-1][0] += x
def ahe(eda,nekko):
    for i in range(eda+1):
        if eda+1 in AB[i]:
            res[nekko] += cnt[i][0]
            ahe(i,nekko)
for nekko in range(n):
    ahe(nekko,nekko)
for i in range(n):
    res[i] += cnt[i][0]
print(*res)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
AB = [[] for _ in range(n)]
cnt = [[0] for _ in range(n)]
res = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    AB[a-1] += [b]
for i in range(q):
    p,x = map(int,input().split())
    cnt[p-1][0] += x
def ahe(eda,nekko):
    for i in range(eda+1):
        if eda+1 in AB[i]:
            res[nekko] += cnt[i][0]
            ahe(i,nekko)
for nekko in range(n):
    ahe(nekko,nekko)
for i in range(n):
    res[i] += cnt[i][0]
print(res)

Submission status: Wrong Answer

Solution code:
def main():
  n,q = map(int, input().split())
  counter = [0] * n
  edge = [[] for _ in range(n)]
  for _ in range(n - 1):
      a,b = map(int, input().split())
      edge[a - 1].append(b - 1)
  for _ in range(q):
      p,x = map(int, input().split())
      counter[p - 1] += x
  for i,c in enumerate(counter):
      if c == 0:
          continue
      for e in edge[i]:
          counter[e] +=c
  print(*counter)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

cur_node = set()
cur_node.add(1)
pre_node = set()
while len(cur_node) != 0:
  next_node = set()
  for ni in cur_node:
    for nj in tree[ni]:
      if nj in pre_node:
        continue
      counter[nj] += counter[ni]
      next_node.add(nj)
  pre_node = cur_node
  cur_node = next_node
print(" ".join((str(counter[i]) for i in range(1, N+1))))

Submission status: Wrong Answer

Solution code:
#入力
n,q = map(int,input().split())
ab = []
for s in range(n-1):
    a1,b1=[int(i) for i in input().split()]
    ab.append([a1,b1])
ab = sorted(ab)
p,x = [],[]
for t in range(q):
    p1,x1=[int(j) for j in input().split()]
    p.append(p1)
    x.append(x1)

#計算    
z = [0]*n
for k in range(q):
  z[p[k]-1] = z[p[k]-1] + x[k]
for l in range(n-1):
  z[ab[l][1]-1] = z[ab[l][1]-1] + z[ab[l][0]-1]  
  
#出力  
print(*z)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline

P, n = map(int, input().split())
A = deque([list([0, [], False]) for _ in range(P)])

for _ in range(P-1):
    p_, c_ = map(int, input().split())
    A[p_-1][1].append(c_-1)

for _ in range(n):
    p_, v = map(int, input().split())
    A[p_-1][0] += v


q = deque()
q.append(0)

while q:
    p = q.popleft()
    A[p][2] = True
    if A[p][1]:
        q.extend(A[p][1])
        for x in A[p][1]:
            A[x][0] += A[p][0]

print(' '.join([str(x[0]) for x in A]))

Submission status: Wrong Answer

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

#print(N, Q, ab, px)
class Tree:
    def __init__(self):
        # n番目のノードの値
        self.value = [0 for _ in range(200010)]
        # n番目の, 親から含めたノードの値
        self.total_value = [0 for _ in range(200010)]
        # n番目のノードの一つ下の辺リスト
        self.edge = [[] for _ in range(200010)]

    def build(self, a, b):
        #print("Tree build ", a, " ", b)
        self.edge[a].append(b)

    def add(self, p, x):
        # 木の値を更新していく
        self.value[p] += x

    # 値を親から子に向かって更新する
    def reflesh(self):
        for i in range(1,N+1):
            self.total_value[i] += self.value[i]
            x = self.total_value[i]
            for e in self.edge[i]:
                self.total_value[e] += x


tree = Tree()
for ab_ in ab:
    tree.build(*ab_)

for px_ in px:
    tree.add(*px_)

tree.reflesh()
#print(" ".join([str(tree.value[x]) for x in range(1,N+1)]))
print(" ".join([str(tree.total_value[x]) for x in range(1,N+1)]))
#for i in range(1,N+1):
    #print(tree.edge[i], end=",")
#    print(tree.value[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)

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

s=[0,]*n

def calc(dd,i,num):
  global s
  if dd[i]==[]:
    s[i-1]+=num
    return(True)
  else:
    s[i-1]+=num
    for item in dd[i]:
      calc(dd,item,num)
    return(True)


dd=defaultdict(list)
for key in range(1,n):
  dd[ab[key-1][0]].append(ab[key-1][1])

for item in px:
  calc(dd,item[0],item[1])

print(*s)

Submission status: Wrong Answer

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

#print(N, Q, ab, px)
class Tree:
    def __init__(self):
        # n番目のノードの値
        self.value = [0 for _ in range(200010)]
        # n番目のノードの一つ下の辺リスト
        self.edge = [[] for _ in range(200010)]

    def build(self, a, b):
        #print("Tree build ", a, " ", b)
        self.edge[a].append(b)

    def add(self, p, x):
        node_list = [p]
        while len(node_list) > 0:
            node = node_list.pop()
            # 木の値を更新していく
            self.value[node] += x
            # 木の下の値を追加していく
            node_list.extend(self.edge[node])

tree = Tree()
for ab_ in ab:
    tree.build(*ab_)

for px_ in px:
    tree.add(*px_)

print(" ".join([str(tree.value[x]) for x in range(1,N+1)]))

#for i in range(1,N+1):
    #print(tree.edge[i], end=",")
#    print(tree.value[i], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int,input().split())
side = []
p = [0] * (Q)
x = [0] * (Q)

for _ in range(N-1):
  s = []
  s = list(map(int, input().split()))
  side.append(s)
for i in range(Q):
  p[i], x[i] = map(int, input().split())

counter = [0] * N
#print(N,Q)
#print(side)
#print(p)
#print(x)

for i in range(Q):
  group = []
  group.append(p[i])
  for s in side:
    if (s[0] in group) == True:
      group.extend(s)
  group = list(set(group))
  for j in group:
    counter[j-1] = counter[j-1] + x[i]
for c in counter:
  print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque
from collections import defaultdict
N, Q = map(int, input().split())
C = [0]* N
D = defaultdict(set)
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].add(b-1)
#  D[b-1].add(a-1)
for i in range(Q):
  p, x = map(int, input().split())
  C[p-1] += x

Q = deque([])
Q.append(0)
vstd = set()

while len(Q)>0:
  a = Q.popleft()
  vstd.add(a)
  for b in D[a]:
    if b in vstd:
      continue
    C[b] += C[a]
    Q.append(b)

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

Submission status: Wrong Answer

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

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

A = [0] * (P + 1)

struct = [list(map(int, input().split())) for x in range(P-1)]

for y in range(n):
    p_, v_ = map(int, input().split())
    A[p_] += v_

for s in struct:
    A[s[1]] += A[s[0]]

print(' '.join([str(A[x]) for x in range(1, len(A))]))

Submission status: Wrong Answer

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

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

A = []
for a in range(P + 1):
    A.append(list([0, []]))

for x in range(P - 1):
    p_, c_ = map(int, input().split())
    A[p_][1].append(c_)

for y in range(n):
    p_, v_ = map(int, input().split())
    A[p_][0] += v_


def add_val_to_child(p):
    val, child = A[p][0], A[p][1]
    if child:
        for c in child:
            A[c][0] += val
            add_val_to_child(c)


add_val_to_child(1)

print(' '.join([str(A[x][0]) for x in range(1, len(A))]))

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a,b = [],[]
for i in range(n-1):
    a1,b1=[int(i) for i in input().split()]
    a.append(a1)
    b.append(b1)
p,x = [],[]
for j in range(q):
    p1,x1=[int(j) for j in input().split()]
    p.append(p1)
    x.append(x1)
    
z = [0]*n
for k in range(q):
  z[p[k]-1] = z[p[k]-1] + x[k]
for l in range(n-1):
  z[b[l]-1] = z[b[l]-1] + z[a[l]-1]    
print(*z)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a = []
b = []
for i in range(n-1):
    a1,b1=[int(i) for i in input().split()]
    a.append(a1)
    b.append(b1)
p = []
x = []
for i in range(q):
    p1,x1=[int(j) for j in input().split()]
    p.append(p1)
    x.append(x1)
z = [0]*n
for k in range(q):
  z[p[k]-1] = z[p[k]-1] + x[k]
for l in range(n-1):
  z[b[l]-1] = z[b[l]-1] + z[a[l]-1]    
print(*z)

Submission status: Wrong Answer

Solution code:
class Leef(object):
	tree = {}

	def __init__(self):
		self.parent = None
		self.counter = 0

	def sumCount(self):
		if self.parent != None:
			self.counter += Leef.tree[self.parent].counter

N, Q = map(int, input().split())
for n in range(1,N):
	a, b = map(int, input().split())
	if a not in Leef.tree:
		leef = Leef()
		Leef.tree[a] = leef

	if b not in Leef.tree:
		leef = Leef()
		leef.parent = a
		Leef.tree[b] = leef

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

sorted_tree = sorted(Leef.tree.items())
for i,l in sorted_tree:
	l.sumCount()

res_counter = list(map(lambda l:str(l[1].counter), sorted_tree))
print(' '.join(res_counter))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

prev = [0 for i in range(num+1)]
prev[1] = 1

for i in range(num-1):
  a, b = map(int, input().split())
  if prev[a] != 0:
    ki[a].append(b)
    prev[b] = 1
  else:
    ki[b].append(a)
    prev[a] = 1
    
c = [0 for i in range(num+1)]

for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

prev = [0 for i in range(num+1)]
prev[1] = 1

for i in range(num-1):
  a, b = map(int, input().split())
  if True:
    ki[a].append(b)
    prev[b] = 1
  else:
    ki[b].append(a)
    prev[a] = 1
    
c = [0 for i in range(num+1)]

for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

prev = [0 for i in range(num+1)]
prev[1] = 1

for i in range(num-1):
  a, b = map(int, input().split())
  if prev[a] == 1:
    ki[a].append(b)
  else:
    ki[b].append(a)
    prev[a] = 1
    
c = [0 for i in range(num+1)]



for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

prev = [0 for i in range(num+1)]
prev[1] = 1

for i in range(num-1):
  a, b = map(int, input().split())
  if prev[a] == 1:
    ki[a].append(b)
    prev[b] = 1
  else:
    ki[b].append(a)
    prev[a] = 1
    
c = [0 for i in range(num+1)]



for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import copy

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

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

c = [0 for i in range(num+1)]



for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b


q = [1]

while True:
  a = []
  for x in q:
    a += ki[x]
    for y in ki[x]:
      c[y] += c[x]
  if len(a) == 0:
    break
  else:
    q = copy.copy(a)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

num, sousa_num = map(int, input().split())

ki = defaultdict(list)

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

c = [0 for i in range(num+1)]

for i in range(sousa_num):
  a, b = map(int, input().split())
  c[a] += b
  
for i, l in ki.items():
  for x in l:
    c[x] += c[i]

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys


[n, q] = [int(x) for x in sys.stdin.readline().rstrip().split()]

counts = [0] * (n+1)

adjs = [[int(x) for x in sys.stdin.readline().rstrip().split()] for i in range(n-1) ]
costs = [[int(x) for x in sys.stdin.readline().rstrip().split()] for i in range(q)]


sorted(adjs, key=lambda x: x[0])

for cost in costs:
    counts[cost[0]] += cost[1]

for adj in adjs:
    counts[adj[1]] += counts[adj[0]]

print(' '.join(list(map(lambda x: str(x), counts[1:]))))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

n, q = [int(i) for i in raw_input().split()]

parent = {}
children = defaultdict(list)

for _ in range(n-1):
    u, v = [int(i) for i in raw_input().split()]
    parent[v] = u
    children[u].append(v)

ops = defaultdict(lambda: 0)
for _ in range(q):
    p, x = [int(i) for i in raw_input().split()]
    ops[p] += x

if n == 1:
    root = 1
else:
    root = [x for x in parent.values() if x not in parent][0]

output = range(n+1)

def bfs(root, output):
    queue = deque([(root, ops[root])])

    while len(queue) > 0:
        current, value = queue.pop()
        output[current] = value

        for x in children[current]:
            queue.append((x, value + ops[x]))

bfs(root, output)

print ' '.join(map(str, output[1:]))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

n, q = [int(i) for i in raw_input().split()]

parent = {}
children = defaultdict(list)

for _ in range(n-1):
    u, v = [int(i) for i in raw_input().split()]
    parent[v] = u
    children[u].append(v)

ops = defaultdict(lambda: 0)
for _ in range(q):
    p, x = [int(i) for i in raw_input().split()]
    ops[p] += x

root = [x for x in parent.values() if x not in parent][0]

output = range(n+1)

def bfs(root, output):
    queue = deque([(root, ops[root])])

    while len(queue) > 0:
        current, value = queue.popleft()
        output[current] = value

        for x in children[current]:
            queue.append((x, value + ops[x]))

bfs(root, output)

print ' '.join(map(str, output[1:]))

Submission status: Wrong Answer

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

P, n = map(int, input().split())
struct = [list(map(int, input().split())) for x in range(P-1)]
act = [list(map(int, input().split())) for y in range(n)]

A = []


def add_val(action):
    idx = action[0]
    val = action[1]

    A[idx][0] += val

    if A[idx][1]:
        for r in A[idx][1]:
            add_val([r, val])


for a in range(P + 1):
    A.append(list([0, []]))

for z in struct:
    p = z[0]
    p_ = z[1]
    A[p][1].append(p_)
print(A)
for _ in act:
    add_val(_)


As = []
for _ in range(1, len(A)):
    As.append(A[_][0])

print(*As, sep=' ')

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, q = map(int, input().split())
cnt = [0]*n
root = [[] for i in range(n)]
 
for i in range(n-1):
    a, b = map(int, stdin.readline().split())
    root[a-1].append(b-1)
 
for i in range(q):
    p, x = map(int, stdin.readline().split())
    cnt[p-1] += x
 
tmparr = [0]
while tmparr:
    tmpi = tmparr.pop()
    for i in root[tmpi]:
        cnt[i] += cnt[tmpi]
    tmparr += root[tmpi]
print(*cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, q = map(int, input().split())
cnt = [0]*n
root = [[] for i in range(n)]
 
for i in range(n-1):
    a, b = map(int, stdin.readline().split())
    root[a-1].append(b-1)
 
for i in range(q):
    p, x = map(int, stdin.readline().split())
    cnt[p-1] += x
 
tmparr = [0]
while tmparr:
    tmpi = tmparr.pop()
    for i in root[tmpi]:
        cnt[i] += cnt[tmpi]
    tmparr += root[tmpi]

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for n in range(n)]
score = [0]*n
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
for _ in range(q):
    p, x = map(int, input().split())
    score[p-1] += x
arr = [0]
while len(arr) != 0:
    i = arr.pop()
    for t in tree[i]:
        score[t] += score[i]
    arr += tree[i]
        

score_str=[str(s) for s in score]
print(" ".join(score_str))

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [[] for n in range(n)]
score = [0]*n
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
for _ in range(q):
    p, x = map(int, input().split())
    score[p-1] += x
arr = tree[0]
while len(arr) != 0:
    i = arr.pop()
    for t in tree[i]:
        score[t] += score[i]
    arr += tree[i]
        

score_str=[str(s) for s in score]
print(" ".join(score_str))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

links = [[] for x in range(n)]
sums = [0 for x in range(n + 1)]
adds = [0 for x in range(n + 1)]

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

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

#print(links)
que = [0,[1]]
for i in range(len(links)):
  childs = links[i]
  for child in childs:
    sums[child] = sums[i] + adds[child]

  
print(*sums[1:])

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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

link = [None] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    if link[a - 1] is None:
        link[a - 1] = [b]
    else:
        link[a - 1].append(b)
    if link[b - 1] is None:
        link[b - 1] = [a]
    else:
        link[b - 1].append(a)

states = [0] * N
check = [0] * N

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

check[0] = 1
for i, state in enumerate(states):
    if state == 0:
        continue
    next_nodes = link[i]
    if next_nodes is None:
        continue
    for node in next_nodes:
        if check[node - 1]:
            continue
        check[node - 1] = 1
        states[node - 1] += state

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

links = [[] for x in range(n)]

sums = [0 for x in range(n + 1)]
adds = [0 for x in range(n + 1)]

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

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

for par, childs in enumerate(links):
  for child in childs:
    sums[child] = sums[par] + adds[child]
  
print(*sums[1:])

Submission status: Wrong Answer

Solution code:
# AC (test cases assigned before contest)
# WA (after_contest_x)
import sys
from collections import deque

stack = deque()
input = sys.stdin.readline

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

link = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    link[a - 1].append(b)
    # link[b - 1].append(a)
    
states = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    states[p - 1] += x

check = [0] * N
stack.append(1)
check[0] = 1
while stack:
    node = stack.pop()
    for next_node in link[node - 1]:
        if check[next_node - 1]:
            continue
        states[next_node - 1] += states[node - 1]
        check[next_node - 1] = 1
        stack.append(next_node)

# Output
print(*states)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

links = defaultdict(list)

sums = [0 for x in range(n + 1)]
adds = [0 for x in range(n + 1)]

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

links = sorted(links.items(), key=lambda x:x[0])

for _ in range(m):
  p, x = map(int, input().split())
  adds[p] += x
#print(links)
for par, childs in links:
  #print(par, childs)
  for child in childs:
    sums[child] = sums[par] + adds[child]
  
print(*sums[1:])

Submission status: Wrong Answer

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

for k in range(len(score)):
    for i in tree[k]:
        if k == i:
            continue
        score[i] += score[k]

score_str=[str(s) for s in score]
print(" ".join(score_str))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import heapq

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

links = []

sums = [0 for x in range(n + 1)]
adds = [0 for x in range(n + 1)]

heapq.heappush(links, [0,1])
for _ in range(n - 1):
  a, b= map(int, input().split())
  heapq.heappush(links, [a, b])

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

while links:
  par, child = heapq.heappop(links)
  sums[child] = sums[par] + adds[child]
  
print(*sums[1:])

Submission status: Wrong Answer

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

for k in range(len(score)):
    for i in tree[k]:
        score[i] += score[k]

score_str=[str(s) for s in score]
print(" ".join(score_str))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(N-1)]
cnt = [list(map(int, input().split())) for _ in range(Q)]
lis = [i[0] for i in graph]
#下から頂点を取っていった時のすでに取られたもの
a = []

gpdict = {i: [i] for i in range(1, N+1)}
cntdict = [0 for i in range(N)]
#一番下内包0の頂点をリスト化
for i in range(1, N+1):
    if not i in lis:
        a.append(i)
#内包しているグラフをリストと辞書で表記
for i in a:
    for j in graph:
        if i == j[1]:
            gpdict[j[0]].extend(gpdict[j[1]])
            if j[0] not in a:
                a.append(j[0])
#print(gpdict)
for i in cnt:
    for j in gpdict[i[0]]:
        cntdict[j-1] += i[1]

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

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
l = [0 for i in range(n)]
l2 = [0 for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    l2[b-1] = a-1
for i in range(q):
    c,d = map(int,input().split())
    l[c-1] += d
ans = [0 for i in range(n)]
ans[0] = l[0]
for i in range(1,n):
    ans[i] = ans[i] + l[i] + ans[l2[i]]
for x in ans:
    print(x,end=" ")

Submission status: Wrong Answer

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

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

for i in range(n):
    c = res[i]
    for a in tree[i]:
        res[a]+=c

print(*res)

Submission status: Wrong Answer

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

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

for i in range(n):
    c = res[i]
    for a in tree[i]:
        res[a]+=c

print(res)

Submission status: Wrong Answer

Solution code:
N, Q = map(int,input().split())
side = []
p = [0] * (Q)
x = [0] * (Q)

for _ in range(N-1):
  s = []
  s = list(map(int, input().split()))
  side.append(s)
for i in range(Q):
  p[i], x[i] = map(int, input().split())

counter = [0] * N
#print(N,Q)
#print(side)
#print(p)
#print(x)

for i in range(Q):
  group = []
  group.append(p[i])
  for s in side:
    if (s[0] in group) == True:
      group.extend(s)
  group = list(set(group))
  for j in group:
    counter[j-1] = counter[j-1] + x[i]

print(counter)

Submission status: Wrong Answer

Solution code:
import numpy as np

import sys
input = sys.stdin.readline

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

T = [ [int(i) for i in input().split() ] for j in range(N - 1) ]

S = [ [int(i) for i in input().split() ] for j in range(Q) ]



w = []


# T = [[1,2],[2,3],[3,4],[3,5]]

T = np.array(T)


# nの右隣の数をリストとして返す関数
def return_number(n):
	
	x = []

	# rはタプル
	r = np.where(T[:,0] == n )

	for i in range(len(r[0])):
		x.append(T[r[0][i],1])
		#xにはnの右横の数字が保存される

	return x	
	# リストxを返す、0列目にnがない時は[]を返す

# リストyの下のノードの数を数える関数
def counter_children(y):
	counter = 0
	for i in range(len(y)):
		counter += len(return_number(y[i]))

	return counter	



def search_subtree(x,y):
	# 再起のたびにzは空になる	
	z = []

	# x,yはリスト xは全体のsubtreeリスト、yは維持的はi番目の深さのnode番号
	# 終了条件(下に子がない時)
	if counter_children(y) == 0:
		return  x
	
	else:
		for i in range(len(y)):
			x += return_number(y[i]) 
			z += return_number(y[i]) 
		
		return search_subtree(x,z)

counter_node = [0 for i in range(N)]

for i in range(Q):
	# S[i][1]がポイント
	# S[i][0]が親node
	 p = search_subtree([S[i][0]],[S[i][0]])

	 for j in range(len(p)):
	 	counter_node[p[j] - 1] += S[i][1]

for i in range(N):
	sys.stdout.write(str(counter_node[i]) + " ")