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

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

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

    for i in range(1, N):
        if parents[i] != -1:
            ans[i] += ans[parents[i]]

    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Q = list(map(int, input().split()))
    parents = [-1] * N
    ans = [0] * N

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

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

    for i in range(1, N):
        if parents[i] != -1:
            ans[i] += ans[parents[i]]

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

l=[list() for i in range(n)]
s=[0]*n

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

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

for i,e in enumerate(s):
  if e==0:
    continue
  for j in l[i]:
    s[j]+=e

print(*s)
print()

Submission status: Wrong Answer

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

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

l=[list() for i in range(n)]
s=[0]*n

def dfs(t,p):
  for c in l[t]:
    if c==p:
      continue
    s[c]+=s[t]
    dfs(c,t)

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

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

dfs(0,n)

print(*s)
print()

Submission status: Wrong Answer

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

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

l=[list() for i in range(n)]
s=[0]*n
c=[0]*n

def ladd(p,q):
  s[p]=c[p]+q
  for i in l[p]:
    ladd(i,s[p])

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

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

ladd(0,0)

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(N - 1)]
px = [tuple(map(int, input().split())) for _ in range(Q)]
 
counter = [0] * N
 
for p, x in px:
    counter[p - 1] += x
 
ab.sort()
for a, b in ab:
    counter[b] += counter[a]
 
for c in counter:
    print(c, end=' ')

Submission status: Wrong Answer

Solution code:
# import numpy as np

N, Q = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(N - 1)]
px = [tuple(map(int, input().split())) for _ in range(Q)]

# counter = np.array([[0] * N for _ in range(N)])
counter = [[0] * N for _ in range(N)]
for p, x in px:
    counter[p - 1][p - 1] += x

# print(counter)
ab.sort()
for a, b in ab:
    for i, (cb, ca) in enumerate(zip(counter[b], counter[a])):
        if cb == 0:
            counter[b][i] += ca

for c in counter:
    print(sum(c))

Submission status: Wrong Answer

Solution code:
# import numpy as np

N, Q = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(N - 1)]
px = [tuple(map(int, input().split())) for _ in range(Q)]

counter = [[0] * N for _ in range(N)]
for p, x in px:
    counter[p - 1][p - 1] += x

ab.sort()
for a, b in ab:
    for i, (cb, ca) in enumerate(zip(counter[b], counter[a])):
        if cb == 0:
            counter[b][i] += ca

print(counter)
for c in counter:
    print(sum(c))

Submission status: Wrong Answer

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

counter = [0] * N

for p, x in px:
    counter[p - 1] += x

ab.sort()
for a, b in ab:
    counter[b] += counter[a]

for c in counter:
    print(c)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def main():
    n, q = map(int, input().split())
    child_dict = defaultdict(list)
    value_dict = defaultdict(lambda: 0)
    rank_dict = defaultdict(lambda: 0)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        child_dict[a].append(b)
        rank_dict[b] += rank_dict[a] + 1

    for _ in range(q):
        c, d = map(int, input().split())
        value_dict[c] += d
    from collections import deque
    stack = deque()
    top = [k for k, v in rank_dict.items() if v == 0][0]
    stack.append(top)
    while stack:
        top_ = stack.pop()
        for child in child_dict[top_]:
            value_dict[child] += value_dict[top_]
        stack.extend(child_dict[top_])
    ans = [value_dict[i] for i in range(1 ,n+1)]
    print(*ans)
main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 21:29:27 2019

@author: lamducanhndgv

"""
import queue


class Node:
    def __init__(self):
        self.value = 0
        self.nNext = []
        
if __name__ == "__main__":
    
    N, Q = list(map(int, input().split()))
    tree = [Node() for _ in range(N)]
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        tree[a - 1].nNext.append(b - 1)
    
    for _ in range(Q):
        p, x = list(map(int, input().split()))
        tree[p - 1].value += x
    
    q = queue.Queue(maxsize = N)
    
    q.put(0)
    
    while q.qsize():
        nc = q.qsize()
        while nc > 0:
            pa = q.get()
            nc -= 1
            tmpNode = tree[pa]
            if tmpNode.nNext:
                for j in tmpNode.nNext:
                    tree[j].value += tmpNode.value
                    q.put(j)
    for i in tree:
        print(i.value, end = " ")

Submission status: Wrong Answer

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

N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
temp=[0]*N
ans=[0]*N

def dfs(i,value):
    ans[i]+=value
    for item in l[i]:
        dfs(item,value+temp[item])

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


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

dfs(0,temp[0])

print(*ans)

Submission status: Wrong Answer

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

N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
temp=[0]*N
ans=[0]*N

def dfs(i,value):
    ans[i]+=value
    for item in l[i]:
        dfs(item,value+temp[item])

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


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

dfs(0,temp[0])

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
temp=[0]*N
ans=[0]*N

def dfs(i,value):
    ans[i]+=value
    for item in l[i]:
        dfs(item,value+temp[item])

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


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

dfs(0,temp[0])

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
#がんばれ
N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
temp=[0]*N
ans=[0]*N


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

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

for i in range(N):
    ans[i]+=temp[i]
    for item in l[i]:
        ans[item]+=temp[i]

print(*ans)

Submission status: Wrong Answer

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

N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
ans=[0]*N


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

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

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
#これどんな意味あんの
N,Q=map(int,input().split())
l=[[]*N for i in range(N)]
ans=[0]*N


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

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

print(*ans)

Submission status: Wrong Answer

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


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

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

print(*ans)

Submission status: Wrong Answer

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

N, Q = [int(x) for x in input().split()]

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

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

next = [0]
founded = [0]
for i in next:
    found = False
    for v in edge[i]:
        if v in founded:
            continue
        found = True
        founded.append(v)
        next.append(v)
        cost[v] += cost[i]
    if not found:
        for v in edge2[i]:
            if v in founded:
                continue
            founded.append(v)
            next.append(v)
            cost[v] += cost[i]

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

Submission status: Wrong Answer

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

graph=[[]for i in range(n+1)]
li=[0]*(n+1)
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)

for p,x in px:
    li[p]+=x

q=((1,0))
for i in range(1,n+1):
    for j in graph[i]:
        if j<i:
            continue
        li[j]+=li[i]
li.pop(0)
print(*li)

Submission status: Wrong Answer

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

N, Q = [int(x) for x in input().split()]

edge = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    if a > b:
        edge[b - 1].append(a - 1)
    else:
        edge[a - 1].append(b - 1)

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

next = [0]
for i in next:
    for v in edge[i]:
        next.append(v)
        cost[v] += cost[i]

print(*cost)

Submission status: Wrong Answer

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

N, Q = [int(x) for x in input().split()]

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

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

next = [0]
for i in next:
    for v in edge[i]:
        next.append(v)
        cost[v] += cost[i]

print(*cost)

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input=sys.stdin.readline
    n,q=map(int,input().split())

    edge=[[] for _ in range(n)]
    for _ in range(n-1):
        u,v=map(int,input().split())
        u-=1;v-=1
        edge[u].append(v)
   
    cost=[0]*n
    for _ in range(q):
        p,x=map(int,input().split())
        p-=1
        cost[p]+=x

    for idx,e in enumerate(cost):
        if e==0:continue
        for f in edge[idx]:
            cost[f]+=e

    print(*cost)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# このコードでは子頂点が親頂点よりも大きい番号を持つ木でWAになる
# そのため、コンテスト後に追加されたafter_contest_15-17でWAになる
# WAになる最も単純な入力例としては以下の例が考えられる
 
# 3 1
# 1 3
# 2 3
# 2 1
 
# 出力例は*0 1 0*となるべきだが実際には*0 1 1*となる
 
n, q = map(int, input().split())
parents = [0] * n
points = [0] * n
 
for i in range(n-1):
  a, b = map(int, input().split())
  parents[b-1] = a-1
  
for i in range(q):
  p, x = map(int, input().split())
  points[p-1] += x
 
for i in range(1, n):
  points[i] += points[parents[i]]
  
print(*points)

Submission status: Wrong Answer

Solution code:
# このコードでは子頂点が親頂点よりも大きい番号を持つ木でWAになる
# そのため、コンテスト後に追加されたafter_contest_15-17でWAになる
# WAになる最も単純な入力例としては以下の例が考えられる

# 3 1
# 1 3
# 2 3
# 2 1

# 出力例は*0 0 1*となるべきだが実際には*0 1 1*となる

n, q = map(int, input().split())
parents = [0] * n
points = [0] * n

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
parents = [0] * n
points = [0] * n

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

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

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
parents = [0] * n
points = [0] * n

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

for i in range(1, n):
  points[i] += points[parents[i]]
  
print(points)

Submission status: Wrong Answer

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

a = [0] * (n-1)
b = [0] * (n-1)

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

p = 0
x = 0

points = [0] * n

for i in range(q):
  p, x = map(int, input().split())
  points[p-1] += x
  
temp = sorted(zip(a, b))
a, b = zip(*temp)

for i in range(n-1):
  points[b[i]-1] += points[a[i]-1]
    
print(points)

Submission status: Wrong Answer

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

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

V = [0]*N

A = []
for i in range(N-1):
    a,b = map(int, input().split())
    A.append((min(a,b),max(a,b)))
A.sort()
P = []
for i in range(Q):
    x,y = map(int, input().split())
    P.append((x,y))
P.sort()

#print("V", V)
#print("A", A)
#print("P", P)

for p in P:
    V[p[0]-1] += p[1]
for a in A:
    V[a[1]-1] += V[a[0]-1]

print(*V)

Submission status: Wrong Answer

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

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

V = [0]*N

A = []
for i in range(N-1):
    a,b = map(int, input().split())
    A.append((a,b))
A.sort()
P = []
for i in range(Q):
    x,y = map(int, input().split())
    P.append((min(x,y),max(x,y)))
P.sort()

#print("V", V)
#print("A", A)
#print("P", P)

for p in P:
    V[p[0]-1] += p[1]
for a in A:
    V[a[1]-1] += V[a[0]-1]

print(*V)

Submission status: Wrong Answer

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

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

V = [0]*N

A = []
for i in range(N-1):
    a,b = map(int, input().split())
    A.append((a,b))
A.sort()
P = []
for i in range(Q):
    x,y = map(int, input().split())
    P.append((x,y))
P.sort()

#print("V", V)
#print("A", A)
#print("P", P)

for p in P:
    V[p[0]-1] += p[1]
for a in A:
    V[a[1]-1] += V[a[0]-1]

print(*V)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline

N, Q = [int(x) for x in input().split()]

n_dic = defaultdict(list)

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

for i in range(N, 0, -1):
    for v in n_dic[i]:
        n_dic[i].extend(n_dic[v])

p_dic = defaultdict(int)
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p_dic[p] += x

ans = [0] * N
for k in p_dic.keys():
    if k == 1:
        for i, _ in enumerate(ans):
            ans[i] += p_dic[k]
        continue
    ans[k - 1] += p_dic[k]
    for v in n_dic[k]:
        ans[v - 1] += p_dic[k]
for v in ans:
    print(v, end=" ")

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline

N, Q = [int(x) for x in input().split()]

n_dic = defaultdict(list)

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

for i in range(N, 0, -1):
    for v in n_dic[i]:
        n_dic[i].extend(n_dic[v])

p_dic = defaultdict(int)
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p_dic[p] += x

ans = [0] * N
for k in p_dic.keys():
    ans[k - 1] += p_dic[k]
    for v in n_dic[k]:
        ans[v - 1] += p_dic[k]
for i in range(N):
    print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, Q = [int(x) for x in input().split()]

n_dic = defaultdict(list)

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

for i in range(N, 0, -1):
    for v in n_dic[i]:
        n_dic[i].extend(n_dic[v])

p_dic = defaultdict(int)
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p_dic[p] += x

ans = [0] * N
for k in p_dic.keys():
    ans[k - 1] += p_dic[k]
    for v in n_dic[k]:
        ans[v - 1] += p_dic[k]
for i in range(N):
    print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, Q = [int(x) for x in input().split()]

dic = defaultdict(list)
dic2 = defaultdict(list)

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    for v in dic[a]:
        dic2[v].append(b)
    dic[b].append(a)
    dic2[a].append(b)

ans = [0] * N

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

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

Submission status: Wrong Answer

Solution code:
#番号が若い方が根に近いと仮定
n,q = map(int,input().split())
Edges = [list(map(int,input().split())) for i in range(n-1)]
Points = [list(map(int,input().split())) for i in range(q)]
Counter = [0 for i in range(n)]
for point in Points:
  Counter[point[0]-1] += point[1]
Edges.sort()
for edge in Edges:
  Counter[edge[1]-1] += Counter[edge[0]-1]
print(*Counter)

Submission status: Wrong Answer

Solution code:
# ABC138D - Ki
from collections import deque
 
 
def dfs() -> None:
    # pop, appendまたはQueueライブラリだと間に合わない
    queue = deque([[0,1]])
    while queue:
        parent, node = queue.popleft()
        for leaf in T[node]:
            if leaf == parent:
                continue
            counter[leaf] += counter[node]
            queue.append([node, leaf])
 
 
def main():
    # Ai (A[i][0]) may be not a parent of Bi (A[i][1])
    global T, counter
    N, Q, *A = map(int, open(0).read().split())
    E, P = A[: (N - 1) * 2], A[(N - 1) * 2 :]
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(E)] * 2):
        print(v,u)
        T[v].append(u), T[u].append(v)
    counter = [0] * (N + 1)
    for p, x in zip(*[iter(P)] * 2):
        counter[p] += x  # increase vertex p by x
    dfs()
    print(*counter[1:])
 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:


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

ab = []
for i in range(n-1):
    ab.append([int(i) for i in input().split()])

px = []
for i in range(q):
    px.append([int(i) for i in input().split()])

tree = {}
for a, b in ab:
    if a in tree.keys():
        tree[a].append(b)
    else:
        tree[a] = [b]

counter = [0] * (n+1)

for p, x in px:
    counter[p] += x

def count(node):
    if node in tree.keys():
        for leaf in tree[node]:
            counter[leaf] += counter[node]
            count(leaf)

for counter_i in counter[1:]:
    print(counter_i, end=" ")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 21:29:27 2019

@author: lamducanhndgv

"""

class Node:
    def __init__(self):
        self.value = 0
        self.next = []
    
    def __add__(self, value):
        self.value += value
        return self
    
    def add_next(self, node):
        self.next.append(node)

def solution(t):
    q = []
    q.append((0, 0))
    while q:
        addValue, tNode = q.pop(0)
        t[tNode] += addValue 
        for i in t[tNode].next:
            q.append((t[tNode].value, i))
            
        

if __name__ == "__main__":
    
    N, Q = list(map(int, input().split()))
    tree = [Node() for i in range(N)]
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        tree[a - 1].add_next(b - 1)
    
    for _ in range(Q):
        p, x = list(map(int, input().split()))
        tree[p - 1].value += x
    
    solution(tree)
    
    print(*(i.value for i in tree), end = "")

Submission status: Wrong Answer

Solution code:


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

ab = []
for i in range(n-1):
    ab.append([int(i) for i in input().split()])

px = []
for i in range(q):
    px.append([int(i) for i in input().split()])

tree = {}
for a, b in ab:
    if a in tree.keys():
        tree[a].append(b)
    else:
        tree[a] = [b]

counter = [0] * (n+1)

for p, x in px:
    counter[p] += x

for root, leaves in tree.items():
    root_count = counter[root]
    for leaf in leaves:
        counter[leaf] += root_count

for counter_i in counter[1:]:
    print(counter_i, end=" ")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc138/tasks/abc138_d

# n: 頂点の数 ((n-1)回エッジを作る)
# q: 頂点に整数を足し込む操作の回数
n, q = map(int, input().split())

values = [0 for _ in range(n)]  # n 個の頂点の値はすべて 0 で初期化する
links = [[] for _ in range(n)]  # n 番目の頂点が、エッジを伸ばしている先の頂点のリスト

# 木の構築
for _ in range(n - 1):
    a, b = [int(x) - 1 for x in input().split()]
    links[a].append(b)
    links[b].append(a)

# 初期値の挿入
for _ in range(q):
    node, value = map(int, input().split())
    node -= 1  # 0 オリジン対応
    values[node] = value

# 値の伝播
stack = [(0, -1)]  # (頂点の番号, 親頂点の番号) を stack につめて、空になるまでイテレートする
while 0 < len(stack):
    node, parent = stack.pop()
    for link in links[node]:
        if link == parent:
            continue
        values[link] += values[node]
        stack.append((link, node))

print(*values)

Submission status: Wrong Answer

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


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]

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

print(*ans)
"""

Submission status: Wrong Answer

Solution code:
Connection_table=[]
#最初のNとQの読み込み
Nstr,Qstr=input().split(" ")
N=int(Nstr)
Q=int(Qstr)
#まずデータの受け取り
ablist=[]
for n in range(N-1):
    ablist.append(input())
pxlist=[]
for q in range(Q):
    pxlist.append(input())

#それぞれの頂点がどれだけ節を持っているか調べる
# #まずデータを入れるリスト作り
for n in range(N):
    Connection_table.append([])

#枝の付き方を保存
for n in range(N-1):
    astr,bstr=ablist[n].split(" ")
    a=int(astr)-1
    b=int(bstr)-1
    Connection_table[a].append(b)
# print(Connection_table,"Connection_table")

# まずそれぞれの頂点に加算
intlist=[0]*N

for px in pxlist:
    p,x=px.split(" ")
    p=int(p)
    x=int(x)
    intlist[p-1]+=x
# print(intlist,"intlist")

# 次に下々に加えていく
for i in range(N):
    for Connect in Connection_table[i]:
        intlist[Connect]+=intlist[i]
        # print(intlist[Connect],Connect,i,"tmp")

# print(int_list,"intlist")
print(" ".join(map(str,intlist)))

Submission status: Wrong Answer

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


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

for i in range(N-1)[::-1]:
    for item in l[i]:
        l[i]=l[i]+l[item]


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

print(*ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, q = map(int, input().split())
brn = [[] for i in range(n)]
w = [0] * n
for i in range(n-1):
    a, b = map(int, stdin.readline().split())
    brn[a-1].append(b)
for i in range(q):
    p, x = map(int, stdin.readline().split())
    w[p-1] += x
for i in range(n):
    print(w[i], end=" ")
    for j in brn[i]: w[j-1] += w[i]
print("")

Submission status: Wrong Answer

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

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


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

stack = [0]
while stack:
  idx = stack.pop(0)
  cnt = tree_counter[idx]
  for next_node in tree[idx]:
    stack.append(next_node)
    tree_counter[next_node] += cnt

        
print(' '.join(list(map(str, tree_counter))))

Submission status: Wrong Answer

Solution code:
from heapq import *
n, c = map(int,input().split())
e = [[] for _ in [0]*n]
ans = [0]*n

for i in range(n-1):
  a,b = (int(x) for x in input().split())
  e[a-1].append(b-1)
  
for i in range(c):
  p,x = (int(x) for x in input().split())
  ans[p-1] += x
  
def dijkstra(start,node,edge):
    #node:頂点数
    #q:キュー(スタートからのコスト,頂点)
    #スタートをキューに入れる
    q = [start]
    
    while(len(q) != 0):
    #キューの先頭を取得
      i = heappop(q)
      #キューの先頭から繋がっている頂点を探索
      for j in edge[i]:
        ans[j] += ans[i] 
        heappush(q,j)
  
dijkstra(0,n,e)
  
print(*ans)

Submission status: Wrong Answer

Solution code:
### abc138_d

def solve():
    N,Q = map(int, input().split())
    tree = [[] for _ in range(N)]
    tree_counter = [0] * N
    for _ in range(N-1):
        a,b = map(int, input().split())
        a,b = a-1,b-1
        tree[a].append(b)
    
    for _ in range(Q):
        p, x = map(int, input().split())
        p = p-1
        tree_counter[p] += x
    
    stack = [0]
    while stack:
        idx = stack.pop(0)
        cnt = tree_counter[idx]
        for next_node in tree[idx]:
            stack.append(next_node)
            tree_counter[next_node] += cnt
            
    print(' '.join(list(map(str, tree_counter))))
    
solve()

Submission status: Wrong Answer

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


def add(index,value):
    ans[index]+=value
    for i in range(N):
        if l[index][i]==1:
            add(i,value)
    return    

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

for i in range(Q):
    p,x=map(int,input().split())
    add(p-1,x)

print(*ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# coding: UTF-8


#import scipy.optimize
#import numpy as np

    
##################################################################

if __name__ == "__main__":
    Input1 = [input() for i in range(1)]
    N,Q = list(map(int,Input1[0].split()))
    Input2 = [input() for i in range(1,N+Q)]
    a = [0]*(N-1)
    b = [0]*(N-1)
    p = [0]*(Q)
    x = [0]*Q
    for i in range(N-1):
        a[i],b[i] = list(map(int,Input2[i].split()))
    for i in range(N-1,N-1+Q):
        p[i+1-N],x[i+1-N] = list(map(int,Input2[i].split()))

    Node = [1]*(N+1)
    for i in range(N-1):
        Node[b[i]] = a[i]
    Root = [False]*(N+1)
    Root[1] = True
    Parent = [[],[1]]
    for i in range(2,N+1):
        Parent.append([i])
        j = i
        temp = Node[j]
        Parent[i] = Parent[i]+Parent[temp]
        while Root[i]==False:
            if any([Root[k] for k in Parent[i]]):
                Root[i]=True
            else:
                j = Node[j]
                Parent[i] = Parent[i]+Parent[j]

    Cntr = [0]*(N+1)
    for i in range(Q):
        for j in range(p[i],N+1):
            if p[i] in Parent[j]:
                Cntr[j] += x[i]
        

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

Submission status: Wrong Answer

Solution code:
#Ki -D-
from collections import deque


n,q=map(int,input().split())
data=[deque([]) for i in range(n+1)]

for i in range(n-1):
    a,b=map(int,input().split())
    if a>b:
        data[b].append(a)
    else:
        data[a].append(b)
    
s=deque([0 for i in range(n+1)])

for i in range(q):
    p,x=map(int,input().split())
    s[p]=s[p]+x
    
for i in range(1,n+1):
    while data[i]:
        s[data[i][0]]=s[data[i][0]]+s[i]
        data[i].popleft()

s.popleft()
print(*s)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy as dp
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) ]
a = [ab[i][0] for i in range(N-1)]
b = [ab[i][1] for i in range(N-1)]
dic = {}

def make_dic(nowlist, ab, st):
    nextlist = [ b[i] for i in range(N-1) if st == a[i] ]
    tmp = []
    if nextlist == []:
        dic[st] = [st]
        return [st]
    else:
        for elem in nextlist:
            tmp = tmp + make_dic(nowlist+[st], ab, elem)
        dic[st] = [st] + tmp
        return [st] + tmp

res = make_dic([], dp(ab), 1)
score = [0]*N
for i in range(Q):
    if px[i][0] in dic.keys():
        for no in dic[px[i][0]]:
            #print([i,px[i][0],no])
            score[no-1] += px[i][1]
print(score)