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:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
#ns = sorted(ns)
 
flags = [True]+[False]*(N-1)
dic = [[] for _ in range(N)]
rlt = [0]*N
 
for i in range(N-1):
  if flags[ns[i][1]-1] and not flags[ns[i][0]-1] :
    dic[ns[i][1]-1] += [ns[i][0]-1]
    flags[ns[i][0]-1] = True
  else:
    dic[ns[i][0]-1] += [ns[i][1]-1]
    flags[ns[i][1]-1] = True
  	
    
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  for l in dic[k]:
    rlt[l] += rlt[k]
    
    
print(" ".join(map(str, rlt)))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
#ns = sorted(ns)
 
flags = [False]*N
dic = [[] for _ in range(N)]
rlt = [0]*N
 
for i in range(N-1):
  if flags[ns[i][1]-1] and not flags[ns[i][0]-1] :
    dic[ns[i][1]-1] += [ns[i][0]-1]
    flags[ns[i][0]-1] = True
  else:
    dic[ns[i][0]-1] += [ns[i][1]-1]
    flags[ns[i][0]-1] = True
    flags[ns[i][1]-1] = True
  	
    
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  for l in dic[k]:
    rlt[l] += rlt[k]
    
    
print(" ".join(map(str, rlt)))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
#ns = sorted(ns)
 
flags = [False]*N
dic = [[] for _ in range(N)]
rlt = [0]*N
 
for i in range(N-1):
  if flags[ns[i][1]-1] and not flags[ns[i][0]-1] :
    dic[ns[i][1]-1] += [ns[i][0]-1]
    flags[ns[i][0]-1] = True
  else:
    dic[ns[i][0]-1] += [ns[i][1]-1]
    flags[ns[i][1]-1] = True
  	
    
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  for l in dic[k]:
    rlt[l] += rlt[k]
    
    
print(" ".join(map(str, rlt)))

Submission status: Wrong Answer

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

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

target = deque([1])
while len(target):
    t = target.popleft()
    ct[t] += qs[t]
    for nt in e[t]:
        target.append(nt)
        ct[nt] += ct[t]

print(*ct[1:])

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
#ns = sorted(ns)

flags = [True]+[False]*(N-1)
dic = [[] for _ in range(N)]
rlt = [0]*N
 
for i in range(N-1):
  if flags[ns[i][1]-1]:
    dic[ns[i][1]-1] += [ns[i][0]-1]
  else:
    dic[ns[i][0]-1] += [ns[i][1]-1]
    flags[ns[i][1]-1] = True
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  for l in dic[k]:
    rlt[l] += rlt[k]
    
    
print(" ".join(map(str, rlt)))

Submission status: Wrong Answer

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

ct = [0] * (n + 1)
target = {i: [i] for i in range(n + 1)}

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

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

for i in range(1, n + 1):
    for node in target[i]:
        ct[node] += qs[i]

print(*ct[1:])

Submission status: Wrong Answer

Solution code:
I=lambda:map(int,input().split())
n,q=I()
s=[0]
d=s*n
e=[[]for _ in d]
while~-n:n-=1;a,b=I();e[a-1]+=b-1,;e[b-1]+=a-1,
while q:q-=1;p,x=I();d[p-1]-=x
d[0]*=-1
while s:
 v=s.pop()
 for w in e[v]:
  if d[w]<1:d[w]=d[v]-d[w];s+=w,
print(*d)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(" "))
 
ns= [list(map(int, input().split(" ")))  for _ in range(N-1)]
vs = [list(map(int, input().split(" ")))  for _ in range(Q)]
 
dic = [[] for _ in range(N)]
rlt = [0]*N
 
 
for i in range(N-1):
  dic[ns[i][0]-1] += [ns[i][1]-1]
  
for j in range(Q):
  rlt[vs[j][0]-1] += vs[j][1]
  
for k in range(N):
  for l in dic[k]:
    rlt[l] += rlt[k]
    
print(" ".join(map(str, rlt)))

Submission status: Wrong Answer

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

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

target = [1]
while len(target):
    t = target.pop()
    ct[t] += qs[t]
    for nt in e[t]:
        target.append(nt)
        ct[nt] += ct[t]

print(*ct[1:])

Submission status: Wrong Answer

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

def dfs(v, tree, cnt):
    for u in tree[v]:
        cnt[u] += cnt[v]
        dfs(u, tree, cnt)


def main():
    N, Q = tuple(map(int, input().split()))
    A = tuple(tuple(map(int, input().split())) for _ in range(N - 1))
    P = tuple(tuple(map(int, input().split())) for _ in range(Q))
    T = [[] for _ in range(N + 1)]  # T[i] := children of vertex i
    cnt = [0] * (N + 1)
    for a, b in A:  # set tree relations
        T[a] += [b]
    for p, x in P:  # increment on roots
        cnt[p] += x
    dfs(1, T, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

ct = [0] * (n + 1)
e = [[] for _ in range(n + 1)]

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

qs = dict()
for _ in range(q):
    p, q = map(int, input().split())
    if qs.get(p) is None:
      qs[p] = 0
    qs[p] += q

target = [1]
while len(target):
    t = target.pop()
    if qs.get(t) is not None:
        ct[t] += qs[t]
    for nt in e[t]:
        target.append(nt)
        ct[nt] += ct[t]

print(*ct[1:])

Submission status: Wrong Answer

Solution code:
N, Q = list(map(int, input().split()))
d = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    if a in d:
        d[a].add(b)
    else:
        d[a] = {b}
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

parents = {1}
while True:
    children = set()
    for parent in parents:
        v = counter[parent]
        for child in d[parent]:
            if child in d:
                children.add(child)
            counter[child] += v
    if len(children) == 0:
        break
    parents = children
print(*counter[1:])

Submission status: Wrong Answer

Solution code:
n,q,*t=map(int,open(0).read().split())
d=[0]*n
for p,x in zip(*[iter(t[-q*2:])]*2):d[p-1]+=x
for a,b in sorted(zip(*[iter(t[:-q*2])]*2)):d[b-1]+=d[a-1]
print(*d)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)

N, Q = list(map(int, input().split()))
d = {}
for i in range(N - 1):
    a, b = list(map(int, input().split()))
    if a in d:
        d[a].add(b)
    else:
        d[a] = {b}
counter = [0] * (N + 1)
for _ in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

def count(parent):
    if parent not in d:
        return
    v = counter[parent]
    for child in d[parent]:
        counter[child] += v
        count(child)
    
count(1)
print(*counter[1:])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(20000000)

n, q = map(int, input().split())
ab = [[] for _ in range(n+1)]
p_num = [0] * (n+1)
ans = [0] * (n+1)

for _ in range(n-1):
    a, b = map(int, input().split())
    ab[a].append(b)
for _ in range(q):
    p, x = map(int, input().split())
    p_num[p] += x

def dfs(num, cur_count):
    cur_count += p_num[num]
    ans[num] = cur_count
    if len(ab[num]) <= 0:
        return
    for v in ab[num]:
        dfs(v, cur_count)

dfs(1, 0)

out = []
for i in range(1, n+1):
    out.append(str(ans[i]))
print(" ".join(out))

Submission status: Wrong Answer

Solution code:
def main():
  _map, _input, _int = map, input, int
  n, q = _map(_int, _input().split())
  values = [0] * (n + 1)
  parents = [0] * (n + 1)
  for _ 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:])
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n, q = map(int, input().split())
t = [[] for _ in range(n + 1)]
for _ in range(n - 1):
  a, b = map(int, input().split())
  t[a].append(b)
  t[b].append(a)
c = [0] * n
for _ in range(q):
  p, x = map(int, input().split())
  c[p - 1] += x
stk = deque()
stk.appendleft((0, -1))
while len(stk) != 0:
  i, p = stk.popleft()
  for j in t[i]:
    if j == p:
      continue
    c[j] += c[i]
    stk.appendleft((j, i))
print(*c)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
# 　その頂点の親番号を格納する
node = [0] * (n + 1)

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

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

for i in range(1, n + 1):
    cnt[i] += cnt[node[i]]
print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=a*10

letterDic={}
j=0
for i in a:
  if i in letterDic:
    letterDic[i].append(j)
  else:
    letterDic[i]=[j]
  j=j+1

#print(letterDic)
    
result=0

flag=1
temp=0

for word in b:
  #print(word,temp,"#############################")
  if word in letterDic:
    endFlag=0
    #print(letterDic[word])
    for item in letterDic[word]:
      if item>=temp:
        result+=item-temp
        temp=item
        endFlag=1

        break
    if endFlag==0:
      result+=min(letterDic[word])-temp+len(a) 
      temp=min(letterDic[word])
    #print(c[0:result+1])

      
      
  
  else:
    print("-1")
    flag=0
    break

if flag==1:
  print(result+1)

Submission status: Wrong Answer

Solution code:
inputNumN = list(map(int, input().split()))
N = inputNumN[0]
Q = inputNumN[1]

AB = {}
for i in range(N - 1):
    inputNumAB = list(map(int, input().split()))
    if not AB.get(inputNumAB[0]):
        AB[inputNumAB[0]] = []
    AB[inputNumAB[0]].append(inputNumAB[1])

result = [0] * N
for i in range(Q):
    inputNumPQ = list(map(int, input().split()))
    result[inputNumPQ[0] - 1] += inputNumPQ[1]

for A, B in AB.items():
    for b in B:
        result[b - 1] += result[A - 1]

for r in result:
    print(r, end=' ')

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n,q = m()

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

root = [[] for i in range(n)]

for i in range(n-1):
    a,b = m()
    a -= 1
    b -= 1
    root[a].append(b)

for i in range(q):
    a,b = m()
    a -= 1
    count[a] += b
de = cl.deque()

for i in range(len(root[0])):
    count[root[0][i]] += count[0]
    de.append(root[0][i])
while de:
    k = de.popleft()
    for i in range(len(root[k])):
        count[root[k][i]] += count[k]
        de.append(root[k][i])
print(jo(count))

Submission status: Wrong Answer

Solution code:
N, Q = list(map(int, input().split()))
trees = {i: [] for i in range(1, N + 1)}
counter = {i: 0 for i in range(1, N + 1)}

for i in range(N-1):
    a, b = list(map(int, input().split()))
    trees[a].append(b)
    
for i in range(Q):
    p, x = list(map(int, input().split()))
    counter[p] += x

for key in trees:
    for branch in trees[key]:
        counter[branch] += counter[key]

print(' '.join([str(i) for i in counter.values()]))

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 links[i]:
      if results[j] == 0:
        calc_results(results, results[i], j)
  n, q = _map(_int, _input().split())
  values = [0] * (n + 1)
  results = [0] * (n + 1)
  links = [[] for _ in range(n + 1)]
  for i in range(n - 1):
    a, b = _map(_int, _input().split())
    links[a].append(b)
    links[b].append(a)
  for _ in range(q):
    p, x = _map(_int, _input().split())
    values[p] += x
  calc_results(results, 0, 1)
  print(*results[1:])
main()

Submission status: Wrong Answer

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

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

operations = Counter()
for i in range(Q):
    p,q =  map(int, input().split())
    operations[p] += q

ans = defaultdict(int)
for i in range(1,N+1):
    ans[i] += operations[i]
    d = deque(connection[i])
    while len(d) > 0:
        x = d.popleft()
        ans[x] += operations[i]
        d += connection[x]

print(' '.join(list(map(str, ans.values()))))

Submission status: Wrong Answer

Solution code:
N,Q=list(map(int,input().strip().split(' ')))
gr=[[] for _ in range(N)]
val=[0]*N
for _ in range(N-1):
    a,b=list(map(int,input().strip().split(' ')))
    a-=1
    b-=1
    gr[a]+=[b]
    gr[b]+=[a]
for _ in range(Q):
    p,x=list(map(int,input().strip().split(' ')))
    p-=1
    val[p]=x
ans=[0]*N
stack=[0]
checked=[0]*N
checked[0]=1
ans[0]=val[0]
while stack:
    x=stack.pop()
    for v in gr[x]:
        if checked[v]==0:
            stack+=[v]
            checked[v]=1
            ans[v]=ans[x]+val[v]
for x in ans:
    print(x,end=" ")

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,q=map(int,input().split())
ls=[0]*(n+1)

ki_list=[list(map(int,input().split())) for i in range(n-1)]
ki_list.sort(key=itemgetter(0))

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

for i in ki_list:
    oya,ko=i[0],i[1]
    ls[ko]+=ls[oya]

print(*ls[1:])

Submission status: Wrong Answer

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

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

operations = Counter()
for i in range(Q):
    p,q =  map(int, input().split())
    operations[p] += q

children = defaultdict(set)
for i in range(1,N+1):
    children[i].add(i)
    d = deque(connection[i])
    while len(d) > 0:
        x = d.popleft()
        children[i].add(x)
        d += connection[x]

ans = defaultdict(int)
for i in range(1,N+1):
    for v in children[i]:
        ans[v] += operations[i]

print(' '.join(list(map(str, ans.values()))))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n,q=map(int,input().split())
ls=[0]*(n+1)

ki_list=[list(map(int,input().split())) for i in range(n-1)]
ki_list.sort(key=itemgetter(0))

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

for i in ki_list:
    oya,ko=i[0],i[1]
    ls[ko]+=ls[oya]

del ls[0]
print(*ls)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ls=[0]*(n+1)

ki_list=[list(map(int,input().split())) for i in range(n-1)]
ki_list.sort()

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

for i in ki_list:
    oya,ko=i[0],i[1]
    ls[ko]+=ls[oya]

del ls[0]
print(*ls)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
# 深さ優先探索で各直線を考える


def dfs(parent):
    for child in tree[parent]:
        # 累積和の計算
        ans[child] += ans[parent]
        dfs(child)


n, q = map(int, input().split())
# tree[i]: 頂点i+1の子どもたち
tree = [[] for i in range(n)]
ans = [0] * n

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

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

# 根ノードの頂点 1 から開始
dfs(0)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
# 深さ優先探索で各直線を考える


def dfs(parent):
    for child in tree[parent]:
        # 累積和の計算
        ans[child] = operate[child] + ans[parent]
        dfs(child)


n, q = map(int, input().split())
# tree[i]: 頂点i+1の子どもたち
tree = [[] for i in range(n)]
# 操作を行う際に根として指定する頂点と加算する値を保持
# ex. 出力例1において、2 を根とする部分木に 100 加算する場合は
# operate = [0, 100, 0, 0] とする
operate = [0] * n
ans = [0] * n

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

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

# 累積和の初期値を設定
ans[0] = operate[0]
# 根ノードの頂点 1 から開始
dfs(0)

print(*ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
n,q=map(int,input().split())
a=[]
s=[0]


for i in range(n-1):
    a.append([int(j) for j in input().split()])
    s.append(0)

a.sort(key=itemgetter(0))

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


for j in range(0,n-1):
    s[a[j][1]-1]=s[a[j][1]-1]+s[a[j][0]-1]


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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ls=[0]*(n+1)
ki_list=sorted([list(map(int,input().split())) for i in range(n-1)])
p_list=[list(map(int,input().split())) for i in range(q)]

for i in p_list:
    ls[i[0]]+=i[1]

for i in ki_list:
    ls[i[1]]+=ls[i[0]]

del ls[0]
print(" ".join(list(map(str,ls))))

Submission status: Wrong Answer

Solution code:
class Node():
    def __init__(self):
        self.counter = 0
        self.child_num = 1


def main():

    n, q = map(int, input().split(' '))
    nodes = [Node() for _ in range(0, n)]
    ab = []
    for _ in range(0, n-1):
        a, b = map(int, input().split(' '))
        ab.append((a, b))

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

    ab.sort(key=lambda x: x[0])
    # 構築
    for a, b in ab:
        nodes[b-1].counter += nodes[a-1].counter

    # for node in nodes[::-1]:
    #     if node.parent is not None:
    #         node.parent.child_num += node.child_num
    #     node.counter *= node.child_num
    # 

    count = 0
    for node in nodes:
        count += 1
        print(node.counter, end='')
        if count != n:
            print(' ', end='')

    print()

if __name__ == '__main__':
    main()

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)
    parents = set()
    parents.add(0)
    while parents:
        # parent = parents.popleft()
        parent = parents.pop()
        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)
                parents.add(child)

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

if __name__ == "__main__":
    n, q = list(map(int, input().strip().split(" ")))
    edges = defaultdict(set)
    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)
        edges[a_i-1].add(b_i-1)
        edges[b_i-1].add(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:
n,q=map(int,input().split())
ls=[0]*(n+1)
ki_list=sorted([list(map(int,input().split())) for i in range(n-1)])

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

for i in ki_list:
    ls[i[1]]+=ls[i[0]]

del ls[0]
print(*ls)

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

cnt = [0] * n
ab = sorted(ab)

for i in range(q):
    cnt[px[i][0]-1] += px[i][1]

for i in range(n-1):
    cnt[ab[i][1]-1] += cnt[ab[i][0]-1]
        
print(" ".join(map(str, cnt)))

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
parent = [0] * (n+1)

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

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

for i in range(1, n+1):
    cnt[i] += cnt[parent[i]]

print(*cnt[1:])

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0] * n
ki = {1 : [1]}
for _ in range(n - 1):
  a, b = map(int, input().split())
  if not ki.get(a):
    ki[b] = [b]
  else:
    ki[b] = ki[a] + [b]

p = {}
for _ in range(q):
  c, d = map(int, input().split())
  if not p.get(c):
    p[c] = d
  else:
    p[c] = p[c] + d

for e, f in p.items():
  for n, k in ki.items():
    if e in k:
      ans[n - 1] += f

for g in ans:
  print(g, 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)]
parent = [0] * n
for i in top_list:
    parent[i[1] - 1] = i[0]

ans = [0] * n

ope_list = [list(map(int,input().split())) for _ in range(q)]
for j in ope_list:
    ans[j[0] - 1] += j[1]

for k in range(n):
    if parent[k] == 0:
        pass
    else:
        ans[k] += ans[parent[k] - 1]

print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n-1)]
ab.sort()
px = [list(map(int, input().split())) for i in range(q)]
p = [0 for i in range(n)]
for i in px:
  p[i[0] - 1] += i[1]
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(" ".join(map(str, p)))

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a=[[] for i in range(n)]
p=[]*n
l=[0]*n

def plus(tl,ta,t2):
    # print(tl)
    for i in ta:
        tl[i-1]+=t2
        # print(ta[t1-1],t1)
        plus(tl,a[i-1],t2)

for i in range(n-1):
    t1,t2 = map(int,input().split())
    a[t1-1].append(t2)
# print(a)
for i in range(q):
    t1,t2 = map(int,input().split())
    l[t1-1]+=t2

for i in range(n):
    # print(a[i-1])
    for j in a[i-1]:
        # print(i,j,"]")
        l[j-1]+=l[i-1]

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
stdin = sys.stdin
na = lambda: map(int, stdin.readline().split())
N, Q = na()
ans = [0] * N
g = [[] for _ in range(N)]

def dfs(cur, par): 
  for e in g[cur]:
    if e == par:
      continue
    ans[e] += ans[cur]
    dfs(e, cur)

for _ in range(N - 1):
    a, b = na()
    g[a-1] += [b-1]
 
for _ in range(Q):
    p, x = na()
    ans[p-1] += x
 
dfs(0, -1)
print(*ans)

Submission status: Wrong Answer

Solution code:
def cin():
  return map(int, input().split())
n, q = cin()
u = [0] * n
c = [0] * n
for i in range(n-1):
    a, b = cin()
    u[b-1] = a-1
for j in range(q):
    p, x = cin()
    c[p-1] += x
for k in range(1,n):
    c[k] += c[u[k]]
print(*c)

Submission status: Wrong Answer

Solution code:
def main():
    n,q=map(int,input().split())
    ls=[0]*(n+1)
    ki_list=sorted([list(map(int,input().split())) for i in range(n-1)])
    p_list=[list(map(int,input().split())) for i in range(q)]
    
    for i in p_list:
        ls[i[0]]+=i[1]
    
    for i in ki_list:
        ls[i[1]]+=ls[i[0]]

    del ls[0]
    print(*ls)

main()

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0] * n
ki = {1 : [1]}
for _ in range(n - 1):
  a, b = map(int, input().split())
  if not ki.get(a):
    ki[b] = [b]
  else:
    ki[b] = ki[a] + [b]
  
ki = sorted(ki.values())

p = {}
for _ in range(q):
  c, d = map(int, input().split())
  if not p.get(c):
    p[c] = d
  else:
    p[c] = p[c] + d

for e, f in p.items():
  for n, k in enumerate(ki):
    if e in k:
      ans[n] += f

for e in ans:
  print(e, end=' ')

Submission status: Wrong Answer

Solution code:
import sys
from decimal import Decimal
from collections import defaultdict

sys.setrecursionlimit(10**6)

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

tree = defaultdict(set)
values = defaultdict(int)

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

def add_px(p, x):
    for node in tree[p]:
        values[node] += x
        if tree[node]:
            add_px(node, x)

for n in range(Q):
    p, x = map(int, input().split())
    values[p] += x
    add_px(p, x)

print(" ".join([str(int(v)) for v in sorted(values.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
ab = sorted(ab)

for i in range(q):
    cnt[px[i][0]-1] += px[i][1]

for i in range(n-1):
    cnt[ab[i][1]-1] += cnt[ab[i][0]-1]
        
print(" ".join(map(str, cnt)))

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

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

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

Submission status: Wrong Answer

Solution code:
def cin():
  return map(int, input().split())
n, q = cin()
parent = [0]*(n+1)
for i in range(n-1):
  a, b = cin()
  parent[b-1] = a-1
score = [0]*n
for i in range(q):
  p, x = cin()
  score[p-1] += x
for i in range(1, n):
  score[i] += score[parent[i]]
print(*score)

Submission status: Wrong Answer

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

tree = {}

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

op = {i: 0 for i in range(1, n+1)}

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

ans = []

for node in op.keys():
  cnt = op[node]
  ans.append(cnt)
  if node in tree:
    for i in tree[node]:
      op[i] += cnt

print(*ans)

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 i in range(q):
    cnt[px[i][0]-1] += px[i][1]

for i in range(n-1):
    cnt[ab[i][1]-1] += cnt[ab[i][0]-1]
        
print(" ".join(map(str, cnt)))

Submission status: Wrong Answer

Solution code:
def cin():
  return map(int, input().split())
n, q = cin()
parent = [0]*(n+1)
for i in range(n-1):
  a, b = cin()
  parent[b] = a
score = [0]*n
for i in range(q):
  p, x = cin()
  score[p-1] += x
for i in range(1, n):
  score[i] += score[parent[i]]
print(*score)

Submission status: Wrong Answer

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

tree = {}

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

op = {i: 0 for i in range(1, n+1)}

for _ in range(q):
  p, x = map(int, input().split())
  op[p] = x

ans = []

for node in op.keys():
  cnt = op[node]
  ans.append(cnt)
  if node in tree:
    for i in tree[node]:
      op[i] += cnt

print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ans = [0] * n
ki = {1 : [1]}
for _ in range(n - 1):
  a, b = map(int, input().split())
  if not ki.get(a):
    ki[b] = [b]
  else:
    ki[b] = ki[a] + [b]
  
ki = ki.values()

p = {}
for _ in range(q):
  c, d = map(int, input().split())
  if not p.get(c):
    p[c] = d
  else:
    p[c] = p[c] + d

for e, f in p.items():
  for n, k in enumerate(ki):
    if e in k:
      ans[n] += f

for e in ans:
  print(e, end=' ')