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:
l=list(map(int, input().split()))
N, Q =l[0], l[1]

parent = [0]*(N+1)
for ii in range(N-1):
    l=list(map(int, input().split()))
    parent[ l[1] ] = l[0]

counter = [0]*(N+1)
for ii in range(Q):
    l=list(map(int, input().split()))
    counter[ l[0] ] += l[1]

c = [0]*(N+1)
for index in range(1, N+1):
    c[index] += counter[index]

for index in range(2, N+1):
    c[index] += c[ parent[index] ]

for index in range(1, N+1):
    if index < N:
        print( '%d ' % c[index], end='' )
    else:
        print(c[index])

Submission status: Wrong Answer

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

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

def tree(p, x):
  L[p] += x
  for i in C[p]:
    tree(i, x)

for _ in range(n-1):
  a, b = map(int, input().split())
  C[a].append(b)
  
for _ in range(q):
  p, x = map(int, input().split())
  tree(p, x)
  
print(*L[1:])

Submission status: Wrong Answer

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

n, q = map(int, input().split())
C = [[i] for i in range(n+1)]
P = [[i] for i in range(n+1)]
L = [0]*(n+1)

def Tree(a, b):
  C[a].append(b)
  P[b].append(a)
  if len(P[a])>=2:
    for i in P[a]:
      if i!=a:
        Tree(i, b)

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

for _ in range(q):
  p, x = map(int, input().split())
  for i in C[p]:
    L[i] += x
print(*L[1:])

Submission status: Wrong Answer

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

for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

queue = [0]
while len(queue) != 0:
  for j in pointer[queue[0]]:
    counter[j] += counter[queue[0]]
    queue.append(j)
  queue.pop(0)
  
for c in counter:
  print(c, end = " ")

Submission status: Wrong Answer

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

for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

for i in range(0,n):
  for j in pointer[i]:
    counter[j] += counter[i]

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

Submission status: Wrong Answer

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

for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
  pointer[b-1].append(a-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

for i in range(0,n):
  for j in pointer[i]:
    counter[j] += counter[i]

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

Submission status: Wrong Answer

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

for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

for i in range(0,n):
  for j in pointer[i]:
    counter[j] += counter[i]

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

Submission status: Wrong Answer

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

for i in range(0,n-1):
  a, b = map(int, input().split())
  pointer[a-1].append(b-1)
 
for i in range(0,q):
  p, x = map(int, input().split())
  counter[p-1] += x

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

Submission status: Wrong Answer

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

N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

flg = [0 for _ in range(N+1)]
q = [1]
while q:
	cp = q.pop()
	for np in E[cp]:
		cnt[np] += cnt[cp]
		q.append(np)

cnt.pop(0)
for c in cnt:
	print(c, end = " ")
print()

Submission status: Wrong Answer

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

N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

q = [1]
def dfs(q):
	if not q:
		return
	cp = q.pop()
	for np in E[cp]:
		cnt[np] += cnt[cp]
		q.append(np)

	dfs(q)
dfs(q)
cnt.pop(0)
for c in cnt:
	print(c, end = " ")
print()

Submission status: Wrong Answer

Solution code:
N, Q = map(int ,input().split())
E = [[] for _ in range(N+1)]
for _ in range(N-1):
	at,bt = map(int ,input().split())
	E[at].append(bt)
cnt = [0 for _ in range(N+1)]
for _ in range(Q):
	pt,xt = map(int, input().split())
	cnt[pt] += xt

for i in range(1,N+1):
	for np in E[i]:
		cnt[np] += cnt[i]
cnt.pop(0)
for c in cnt:
	print(c, end=" ")
print()

Submission status: Wrong Answer

Solution code:
from collections import deque

N,Q=map(int,input().split())
out=[set() for i in range(N+1)];out[0]=None # 頂点は1-initial
for j in range(N-1):
    a,b=map(int,input().split())
    out[a].add(b)
# print(out) #DB

a=[0 for i in range(N+1)];a[0]=None

for k in range(Q):
    p,x=map(int,input().split())
    a[p]+=x

# print(a) #DB

q=deque()
q.append(1)
while q:
    u=q.popleft()
    for v in out[u]:
        a[v]+=a[u]
        q.append(v)
        # print(q) #DB

print(*a[1:])

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a < b:
            AB[a].append(b)
        else:
            sys.exit('error')

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n):
        for b in AB[a]:
            ans[b] += ans[a]
    
    print(' '.join(map(str, ans[1:])))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a < b:
            AB[a].append(b)
        else:
            print('error')
            sys.exit()

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n):
        for b in AB[a]:
            ans[b] += ans[a]
    
    print(' '.join(map(str, ans[1:])))

if __name__ == "__main__":
    main()

# なにがいけないのだろうか

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a < b: # 入力が問題の制約をみたいしているか確認
            AB[a].append(b)
        else:
            AB[b].append(a)

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n):
        for b in AB[a]:
            ans[b] += ans[a]
    
    print(' '.join(map(str, ans[1:])))

if __name__ == "__main__":
    main()

# なにがいけないのだろうか

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        AB[a].append(b)

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n):
        for b in AB[a]:
            ans[b] += ans[a]
    
    print(' '.join(map(str, ans[1:])))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        AB[a].append(b)

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n+1):
        for b in AB[a]:
            ans[b] += ans[a]
    
    for i in range(1, n+1):
        print(ans[i], end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        AB[a].append(b)

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n+1):
        for b in AB[a]:
            ans[b] += ans[a]
    
    for c in ans:
        print(c, end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# 2019-11-17 10:52:21(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
import itertools
# from functools import reduce
# import operator as op
# import re
# import heapq
# import array
# from scipy.misc import comb # (default: exact=False)
# import numpy as np 


def main():
    n, q = [int(x) for x in sys.stdin.readline().split()]
    AB = [[] for _ in range(n+1)]
    for _ in range(n-1):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        AB[a].append(b)

    ans = [0 for _ in range(n+1)]
    for _ in range(q):
        p, x = [int(x) for x in sys.stdin.readline().split()]
        ans[p] += x
    
    for a in range(1, n+1):
        for b in AB[a]:
            ans[b] += ans[a]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

def dfs(v, p):
    for u in tree[v]:
        print(u)
        if u == p:
            continue
        else:
            ans[u] += ans[v]
            dfs(u, v)

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
tree = [0 for _ in range(n)]
tmp = [0 for _ in range(n)]
ans = [0 for _ in range(n)]

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

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

for i in range(n):
    ans[i] = ans[tree[i]] + tmp[i]

print(*ans)

Submission status: Wrong Answer

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

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

stack = [1]
t = [False]*(N)
t[0] = True
while len(stack) > 0:
    a = stack.pop(0)
    for b in G[a-1]:
        if not t[b-1]:
            t[b-1] = True
            stack.append(b)
            C[b-1] += C[a-1]
print(*C)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue,copy
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x,n,k):
    if k == 0:x.sort(key=lambda y:y[1,n])
    else:x.sort(reversed=True, key=lambda y:y[1,n])
def graph():
    n,m=inpm()
    g=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=inpm()
        a-=1
        b-=1
        g[a].append(b)
        #g[b].append(a)
    return n,m,g

def main():
    n,q,g=graph()
    px=[]
    for _ in range(q):
        px.append(inpl())
    px.sort()
    ans=[0 for _ in range(n)]
    for i in range(q):
        ans[px[i][0]-1]+=px[i][1]
    for i in range(n):
        for j in range(len(g[i])):
            ans[g[i][j]]+=ans[i]
    print(*ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

R=[[] for i in range(N)]
ans=[0]*N
amax=0
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    R[a].append(b)
    amax=max(amax,a)

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

q=deque([0])
cnt=0
while q:
    t=q.pop()
    for i in R[t]:
        c[i]+=c[t]
        if R[i]:
            for j in R[i]:
                q.append(j)
    if len(q)!=0:continue
    elif cnt<amax:
        cnt+=1
        q.append(cnt)
    else:
        break

print(*c)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10**7)
    input = sys.stdin.readline

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

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

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

    def re(sta,add):
        if len(s[sta]) == 0:
            return None
        else:
            for y in s[sta]:
                t[y-1] += add
                add1 = t[y-1]
                re(y,add1)
            return None

    re(1,t[0])

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10**7)

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

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

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

    def re(sta,add):
        if len(s[sta]) == 0:
            return None
        else:
            for y in s[sta]:
                t[y-1] += add
                add1 = t[y-1]
                re(y,add1)
            return None

    re(1,t[0])

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys

sys.setrecursionlimit(10**7)

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

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

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

def re(sta,add):
    if len(s[sta]) == 0:
        return None
    else:
        for y in s[sta]:
            t[y-1] += add
            add1 = t[y-1]
            re(y,add1)
        return None

re(1,t[0])

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

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b); t[b].append(a)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x
    dq = deque([0])
    while dq:
        node = dq.popleft()
        children = t[node]
        dq.extend(children)
        while children:
            child = children.pop()
            c[child] += c[node]
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n, q = map(int, input().split())
    t = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(lambda x: int(x)-1, input().split())
        t[a].append(b)
    c = [0]*n
    for i in range(q):
        p, x = map(int, input().split())
        p -= 1
        c[p] += x
    dq = deque([0])
    while dq:
        node = dq.popleft()
        children = t[node]
        dq.extend(children)
        while children:
            child = children.pop()
            c[child] += c[node]
    print(*c)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# def add(i,g,b):
#     g[i].append(b)
#     for j in range(n):
#         if j!=i and i in g[j]:
#             g[j].append(b)
#     return g

def add(i,g,b):
    g[i].append(b)
    return g

n,q=map(int,input().split())
g=[[]for i in range(n)]
res=[0 for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    g=add(a-1,g,b-1)

# def my_add(i,score,g,res):
#     res[i]+=score
#     if g[i]!=[]:
#         for j in range(len(g[i])):
#             res[g[i][j]]+=score
#     return res

def my_add(res,g):
    for i in range(n):
        if g[i]!=[]:
            for j in range(len(g[i])):
                res[g[i][j]]+=res[i]
    return res
#print(g)
for k in range(q):
    p,x=map(int,input().split())
    res[p-1]+=x
print(*my_add(res,g))

Submission status: Wrong Answer

Solution code:
def add(i,g,b):
    g[i].append(b)
    for j in range(n):
        if j!=i and i in g[j]:
            g[j].append(b)
    return g

n,q=map(int,input().split())
g=[[]for i in range(n)]
res=[0 for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    g=add(a-1,g,b-1)

def my_add(i,score,g,res):
    res[i]+=score
    if g[i]!=[]:
        for j in range(len(g[i])):
            res[g[i][j]]+=score
    return res

for k in range(q):
    p,x=map(int,input().split())
    res=my_add(p-1,x,g,res)
print(*res)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000)

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

def my_add(i,score,g,res):
    res[i]+=score
    if g[i]!=[]:
        for j in range(len(g[i])):
            res=my_add(g[i][j],score,g,res)
    return res

for k in range(q):
    p,x=map(int,input().split())
    res=my_add(p-1,x,g,res)
print(*res)

Submission status: Wrong Answer

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

def my_add(i,score,g,res):
    res[i]+=score
    for j in range(n):
        if g[i][j]!=0:
            res=my_add(j,score,g,res)
    return res

for k in range(q):
    p,x=map(int,input().split())
    res=my_add(p-1,x,g,res)
print(*res)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
edge = {}
for _ in range(N-1):
  s,e = map(int,input().split())
  if not(s in edge):
    edge[s] = [e]
  else:
    edge[s] += [e]

operate = [list(map(int,input().split())) for _ in range(Q)]
count = [0]*(N+1)

for i in range(Q):
  count[operate[i][0]] += operate[i][1]

stack = [1]
cstack = []
while len(stack) > 0:
  node = stack.pop()
  if not (node in edge):
    continue
  cstack = edge[node]
  stack += edge[node]
  for i in cstack:
    count[i] += count[node]

for i in count[1:]:
  print(i,end=" ")

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
edge = {}
for _ in range(N-1):
  s,e = map(int,input().split())
  if not(s in edge):
    edge[s] = [e]
  else:
    edge[s] += [e]

operate = [list(map(int,input().split())) for _ in range(Q)]
count = [0]*(N+1)

for i in range(Q):
  count[operate[i][0]] += operate[i][1]

stack = [1]
while len(stack) > 0:
  node = stack.pop()
  if not (node in edge):
    continue
  stack += edge[node]
  for i in stack:
    count[i] += count[node]

for i in count[1:]:
  print(i,end=" ")

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
edge = {}
for _ in range(N-1):
  s,e = map(int,input().split())
  if not(s in edge):
    edge[s] = [e]
  else:
    edge[s] += [e]

operate = [list(map(int,input().split())) for _ in range(Q)]
count = [0]*(N+1)

for i in range(Q):
  count[operate[i][0]] += operate[i][1]

for node in edge:
  stack = edge[node]
  for i in stack:
    count[i] += count[node]

for i in count[1:N-1]:
  print(i,end=" ")
print(count[N-1])

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
edge = {}
for _ in range(N-1):
  s,e = map(int,input().split())
  if not(s-1 in edge):
    edge[s-1] = [e-1]
  else:
    edge[s-1] += [e-1]

operate = [list(map(int,input().split())) for _ in range(Q)]
operate = list(map(lambda x:[x[0]-1,x[1]],operate))
count = [0]*N

for i in range(Q):
  count[operate[i][0]] += operate[i][1]

for node in range(N):
  if not (node in edge):
    continue
  stack = edge[node]
  for i in stack:
    count[i] += count[node]

for i in count:
  print(i,end=" ")

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
edge = {}
for _ in range(N-1):
  s,e = map(int,input().split())
  if not(s-1 in edge):
    edge[s-1] = [e-1]
  else:
    edge[s-1] += [e-1]

operate = [list(map(int,input().split())) for _ in range(Q)]
operate = list(map(lambda x:[x[0]-1,x[1]],operate))
operate.sort()
count = [0]*N

for i in range(Q):
  count[operate[i][0]] += operate[i][1]

stack = [0]
for node in range(N):
  if not (node in edge):
    continue
  stack = edge[node]
  for i in stack:
    count[i] += count[node]

for i in count:
  print(i,end=" ")

Submission status: Wrong Answer

Solution code:

def main():
  N,Q = map(int,input().split())
  edge = {}
  for _ in range(N-1):
    s,e = map(int,input().split())
    if not(s-1 in edge):
      edge[s-1] = [e-1]
    else:
      edge[s-1] += [e-1]

  operate = [list(map(int,input().split())) for _ in range(Q)]
  operate = list(map(lambda x:[x[0]-1,x[1]],operate))
  count = [0]*N
  for ope in operate:
    adder(ope,edge,count)
  for i in count:
    print(i,end=" ")

def adder(operatepoint,edge,count):
  stack = [operatepoint[0]]
  while len(stack)>0:
    node = stack.pop()
    count[node] += operatepoint[1]
    if node in edge:
      for j in edge[node]:
        stack += [j]

main()

Submission status: Wrong Answer

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

tree = {}
for i in range(N - 1):
    p, c = map(int, input().split())
    if p in tree:
        tree[p].append(c)
    else:
        tree[p] = [c]

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

for root, reaf in tree.items():
    for i in reaf:
        value[i] += value[root]

print(*value[1:])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
v_parent=[0 for _ in range(n+1)]

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

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

for i in range(1,n+1):
    v_counter[i]+=v_counter[v_parent[i]]

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

#print(v_parent)
#print(v_counter)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
v_counter=[0 for _ in range(n+1)]
v_parent=[0 for _ in range(n+1)]

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

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

for i in range(1,n+1):
    v_counter[i]+=v_counter[v_parent[i]]
    print(v_counter[i],end=" ")
print()

#print(v_parent)
#print(v_counter)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
nli = [[] for i in range(n)]
ans = [0 for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    nli[a-1].append(b-1)
for j in range(q):
    p, x = map(int, input().split())
    p -= 1
    
    st = [p]
    while len(st) > 0:
        e = st.pop()
        for c in nli[e]:
            st.append(c)
        ans[e] += x
print(' '.join([str(s) for s in ans]))

Submission status: Wrong Answer

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

tree = [0] * (N+1)
for a, b in [list(map(int, input().split())) for i in range(N-1)]:
    tree[b] = a

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

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

print(*points[1:])

Submission status: Wrong Answer

Solution code:
N,Q = map(int, input().split())
v = [list(map(int, input().split())) for i in range(N-1)]
w=[0]*N #0origin
for j in range(Q):
    p,x = map(int, input().split())
    w[p-1]+=x

for i in range(N-1):
    w[v[i][1]-1]+=w[v[i][0]-1]

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

Submission status: Wrong Answer

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

def main():
    N, Q = map(int, input().split())
    con = [[] for _ in range(N)]
    ans = [0] * N

    # 木構造を表す
    for _ in range(N - 1):
        a, b = map(int, input().split())
        # 各節から繋がっている節を、親とか子とか気にせず記録
        con[a - 1].append(b - 1)
        con[b - 1].append(a - 1)

    # 各カウンターの素の値を計算    
    for _ in range(Q):
        p, v = map(int, input().split())
        ans[p-1] += v

    # queueを使用し、頂点に近い節から繋がっているところを辿っていく
    stacks = deque()

    # 頂点は0、親はないので-1を初期値セット
    stacks.append((0, -1))

    while stacks:
        # 節の値を取得
        stack = stacks.pop()

        # nodeは節番号、parentは親
        node, parent = stack[0], stack[1]

        # その節から繋がっている全ての節に対して処理を実施
        children = con[node]
        for child in children:

            # もし繋がっている節が親であれば、無視する（逆流防止）
            if child == parent:
                continue

            # 子の節には、今の自身のカウンターをプラスする
            ans[child] += ans[node]

            # 子をスタックする
            stacks.append((child, node))
    
    # FAにするため誤った出力を出す（記録したいけどまだFAにはしたくない）
    print("aaa")
    print(*ans)

if __name__ == '__main__':
    main()