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:
# https://atcoder.jp/contests/abc138/tasks/abc138_d
# 下に数を最初に合計してから深さ優先探索なり、幅優先探索なりで下にカウンターを伝播させていけば良い。
# 累積和の木構造バージョン


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


# 再帰の上限を緩和する(引数は適当)
import sys
sys.setrecursionlimit(1 << 25)
from collections import defaultdict

N, Q = read_ints()
child = defaultdict(lambda: [])  # ノード0は存在しない
score = [0] * (N + 1)
for _ in range(N - 1):
    a, b = read_ints()
    child[a].append(b)
for _ in range(Q):
    p, x = read_ints()
    score[p] += x

# 木を走査しながらscoreを合計していく
ans = [0] * (N + 1)


def dfs(u, s):  # ノードuの親におけるスコアs
    if u == 0:
        return
    s += score[u]
    ans[u] = s
    for c in child[u]:
        dfs(c, s)


dfs(1, 0)
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

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

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

ret = [0] * n
q = deque(maxlen=n)
q.append((1, 1))
while len(q) > 0:
    f, c = q.popleft()
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        q.append((c, n))

print(*ret)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

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

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

ret = [0] * n
q = deque(maxlen=n)
q.append((1, 1))
while len(q) > 0:
    f, c = q.popleft()
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        if n != f:
            q.append((c, n))

print(*ret)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

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

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

ret = [0] * n
q = deque(maxlen=n)
q.append((1, 1))
while len(q) > 0:
    f, c = q.popleft()
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        q.append((c, n))

print(*ret)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

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

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

ret = [0] * n
q = deque(maxlen=n)
q.append((1, 1))
while len(q) > 0:
    f, c = q.popleft()
    print(f - 1, c - 1, len(ret))
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        q.append((c, n))

print(*ret)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

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

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

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

ret = [0] * n
q = deque(maxlen=n)
q.append((1, 1))
while len(q) > 0:
    f, c = q.popleft()
    print(f - 1, c - 1, len(ret))
    ret[c - 1] = count[c] + ret[f - 1]
    for n in tree[c]:
        if n != f:
            q.append((c, n))

print(*ret)

Submission status: Wrong Answer

Solution code:
from queue import Queue
from collections import defaultdict

def solve(n, q, AB, PX):
    count = defaultdict(int)
    for p, x in PX:
        count[p] += x

    tree = defaultdict(lambda: [])
    for a, b in AB:
        tree[a].append(b)

    ret = [0] * n
    q = Queue()
    q.put((0, 1))
    while not q.empty():
        f, c = q.get()
        ret[c - 1] = count[c] + ret[f - 1]
        for n in tree[c]:
            q.put((c, n))

    return ret

_n, _q = map(int, input().split())
_AB = [map(int, input().split()) for _ in range(_n - 1)]
_PX = [map(int, input().split()) for _ in range(_q)]
print(*solve(_n, _q, _AB, _PX))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,Q=map(int,input().split())

l=[[0]*N for i in range(N)]

def dfs(num,point,lis):
    point+=plus[num]
    ans[num]=point
    
    for index,item in enumerate(l[num]):
        if item==1:
            temp=lis[:]
            temp[num][index]=0
            temp[index][item]=0
            dfs(index,point,temp)
    
    return 0

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

ans=[0]*N
plus=[0]*N
for _ in range(Q):
    p,x=map(int,input().split())
    
    plus[p-1]+=x
    
dfs(0,0,l)
    
print(*ans)

Submission status: Wrong Answer

Solution code:
def func(a,p,s,n,x,b):
    s[p-1] += x
    t=p
    for j in range(0,n-1):
        if a[j] == t:
            t = b[j]
            func(a,t,s,n,x,b)
            t = p


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

p=[0]*q
x=[0]*q

for i in range(0,q):
    p[i],x[i]=map(int,input().split())
    
for i in range(0,q):
    func(a,p[i],s,n,x[i],b)

        
for i in range(0,n):
    print(s[i])

#b=[int(x) for x in input().split()]

Submission status: Wrong Answer

Solution code:
def func(a,p,s,n,x,b):
    s[p-1] += x
    t=p
    #print("func")
    for j in range(0,n-1):
       # print("a[",j,"]:",a[j],"p:",p)
        if a[j] == t:
            t = b[j]
            func(a,t,s,n,x,b)
            t = p


n,q = map(int,input().split())
s=[0]*n
#print(type(n))
a=[0]*(n-1)
b=[0]*(n-1)
for i in range(0,n-1):
    a[i],b[i] = map(int,input().split())
    #print(i,":","a:",a,"b:",b)
for i in range(0,q):
    p,x=map(int,input().split())
    #print(i,":","p:",p,"x:",x)
    
    func(a,p,s,n,x,b)
    #print(i,":","s",s)

        
for i in range(0,n):
    print(s[i])

#b=[int(x) for x in input().split()]

Submission status: Wrong Answer

Solution code:
def func(a,p,s,n,x,b):
    s[p-1] += x
    t=p
    #print("func")
    for j in range(0,n-1):
       # print("a[",j,"]:",a[j],"p:",p)
        if a[j] == t:
            t = b[j]
            func(a,t,s,n,x,b)
            t = p


n,q = map(int,input().split())
s=[0]*n
#print(type(n))
a=[0]*(n-1)
b=[0]*(n-1)
for i in range(0,n-1):
    a[i],b[i] = map(int,input().split())
    #print(i,":","a:",a,"b:",b)
for i in range(0,q):
    p,x=map(int,input().split())
    #print(i,":","p:",p,"x:",x)
    
    func(a,p,s,n,x,b)
    #print(i,":","s",s)

            


        
        
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for _ in range(n-1):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
  
for _ in range(q): 
  v,val=map(int,input().split())
  cnt[v]+=val    
  
q = deque()
q.append(1)
checked = [0]*(n+1) 
while q:
  v = q.pop()
  checked[v] = 1
  for vc in g[v]:
    if checked[vc] == 1:
      continue
    else:
      cnt[vc] += cnt[v]
      q.append(vc)
    
print(*cnt)

Submission status: Wrong Answer

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

tree = {}

for ab in a_b:
  if ab[0] not in tree:
    tree[ab[0]] = [ab[1]]
  else:
    tree[ab[0]].append(ab[1])

#print(tree)

ope = {}
for px in p_x:
  if px[0] not in ope:
    ope[px[0]] = px[1]
  else:
    ope[px[0]] += px[1]
#print(ope)
ans = [0]*N

for i in range(1, N + 1):
  #頂点に足す
  if i in ope:
    ans[i - 1] += ope[i]
  #下のやつに足す
  if i in tree:
    for j in tree[i]:
      ans[j - 1] += ans[i - 1]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(*cnt)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

counter = defaultdict(lambda: 0)
parent_dict = defaultdict(lambda: 0)  # 親ノード
rank_dict = defaultdict(lambda: 0)  # 根からの距離

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

# 根からの距離(after test用)
for a, b in ab:
    if a == 1:
        rank_dict[b] = 1
        parent_dict[b] = 1
    else:
        if rank_dict[a] != 0 and rank_dict[b] == 0:
            rank_dict[b] = rank_dict[a] + 1
            parent_dict[b] = a
        elif rank_dict[a] == 0 and rank_dict[b] != 0:
            rank_dict[a] = rank_dict[b] + 1
            parent_dict[a] = b

rank_sorted = sorted(rank_dict.items(), key=lambda x: x[1])
for k, v in rank_sorted:
    counter[k] += counter[parent_dict[k]]

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

counter = defaultdict(lambda: 0)
ba_dict = defaultdict(lambda: 0)

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

for a, b in ab:
    ba_dict[b] = a
for i in range(2, N+1):
    counter[i] += counter[ba_dict[i]]

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

counter = defaultdict(lambda: 0)

for p, x in px:
    counter[p] += x
for a, b in ab:
    counter[b] += counter[a]
ans = []
for i in range(1, N+1):
    ans.append(counter[i])
print(" ".join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

counter = defaultdict(lambda: 0)

for p, x in px:
    counter[p] += x
ab = sorted(ab)
for a, b in ab:
    counter[b] += counter[a]

ans = []
for k, v in sorted(counter.items()):
    ans += [v]

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

counter = defaultdict(lambda: 0)

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

ans = []
for k, v in sorted(counter.items()):
    ans += [v]

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

Submission status: Wrong Answer

Solution code:
nq = list(map(int, input().split()))
n, q = nq[0], nq[1]
ab_list = [list(map(int, input().split())) for _ in range(n - 1)]
px_list = [list(map(int, input().split())) for _ in range(q)]

parents = {1: None}
for ab in ab_list:
    a, b = ab[0], ab[1]
    parents[b] = a

cn = [0 for _ in range(n)]
for px in px_list:
    p, x = px[0], px[1]
    cn[p - 1] += x

for i in range(n):
    parent = parents.get(i + 1)
    if parent is not None:
        cn[i] += cn[parent - 1]

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

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q = map(int,input().split(" "))
cnt = [0 for _ in range(N)]
edge = [set([i]) for i in range(N)]
for i in range(N-1):
    a,b = map(lambda x: int(x)-1, input().split(" "))
    edge[a].add(b)
    for e in edge:
        if a in e:
            e.add(b)

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

print(*cnt)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    G = {i:[] for i in range(1, N+1)}
    
    for i in range(N-1):
        a, b = map(int, input().split())
        
        G[a].append(b)
        
    count = [0] * (N+1)
    
    for i in range(Q):
        p, x = map(int, input().split())
        count[p] += x
    
    q = [(1, 0)]
    
    while q:
        v, c = q.pop(0)
        count[v] += c
        for u in G[v]: q.append((u, count[v]))
        
    print(' '.join(map(str, count[1:])))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
edges = [[] for i in range(N+1)]
counts = [0 for i in range(N+1)]

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

def dfs(u, x):
  colors = ["white"] * (N+1)
  stack = deque()
  stack.append(u)
  while stack:
    v = stack.pop()
    colors[v] = "black"
    counts[v] += x

    for e in edges[v]:
      if colors[e] != "black" and e != v:
        stack.append(e)


for j in range(Q):
  p, x = map(int, input().split())
  dfs(p, x)

print(*counts[1:])

Submission status: Wrong Answer

Solution code:

N, Q = map(int, input().split())
edges = [[] for i in range(N+1)]
counts = [0 for i in range(N+1)]

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

def dfs(u, x):
  colors = ["white"] * (N+1)
  stack = [u]
  while stack:
    v = stack.pop()
    colors[v] = "black"
    counts[v] += x

    for e in edges[v]:
      if colors[e] != "black":
        stack.append(e)


for j in range(Q):
  p, x = map(int, input().split())
  dfs(p, x)

print(*counts[1:])

Submission status: Wrong Answer

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


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


stack = [[0, ans[0]]]
visited = [False] * N
while stack:
    n, v = stack.pop()
    visited[n] = True

    for to in T[n]:
        if visited[to]:
            continue

        stack.append([to, v + ans[to]])
        ans[to] += v

print(*ans, sep=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque
N,Q = map(int,input().split())
ab = [[] for i in range(N)]
for i in range(N-1):
    a,b = list(map(int,input().split()))
    a -= 1
    b -= 1
    ab[a].append(b)
px = [0 for i in range(N)]
for i in range(Q):
    p,x = list(map(int,input().split()))
    px[p-1] += x
que = deque()
alr = [0]
que.append(0)
while que:
    q = que.popleft()
    sumq = px[q]
    for i in ab[q]:
        if i not in alr:
            que.append(i)
            alr.append(i)
            px[i] += sumq
print(*px)

Submission status: Wrong Answer

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

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

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

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

que=deque()
que.append(0)
used=set()
while(len(que)>0):
    ind=que.popleft()
    used.add(ind)
    child=v_child[ind]
    for child_i in child:
        if(child_i in used):
            continue
        else:
            v_counter[child_i]+=v_counter[ind]
            que.append(child_i)
            used.add(child_i)

print(*v_counter)

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, number: int):
        self.number = number
        self.counter = 0
        self.children = list()

    def find(self, number: int):
        pass

    def add_node(self, number: int, node):
        if self.number == number:
            self.children.append(node)
        else:
            for c in self.children:
                c.add_node(number, node)

    def add_counter(self, number: int, count: int):
        if number is None:
            self.counter += count
            for c in self.children:
                c.add_counter(None, count)
        elif self.number == number:
            self.counter += count
            for c in self.children:
                c.add_counter(None, count)
        else:
            for c in self.children:
                c.add_counter(number, count)

    def make_result(self, result: list):
        index = self.number - 1
        result[index] = self.counter
        for c in self.children:
            c.make_result(result)


nq = list(map(int, input().split()))
n, q = nq[0], nq[1]
ab_list = [list(map(int, input().split())) for _ in range(n - 1)]
px_list = [list(map(int, input().split())) for _ in range(q)]

root = Node(1)

# 木の構築
for ab in ab_list:
    a, b = ab[0], ab[1]
    root.add_node(a, Node(b))

# カウントアップ処理
for px in px_list:
    p, x = px[0], px[1]
    root.add_counter(p, x)

r = [None for _ in range(n)]
root.make_result(r)
print(" ".join(list(map(str, r))))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
T=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    T[a].append(b)
tasu=[0]*(n+1)
for i in range(q):
    p,x=map(int,input().split())
    tasu[p]+=x
    
def bfs(a):
    ans=[-1]*(n+1)
    q=deque([a])
    ans[a]=tasu[a]
    while q:
        x=q.popleft()
        moto=ans[x]
        for w in T[x]:
            if ans[w]<0:
                ans[w]=moto+tasu[w]
                q.append(w)
    return ans
kotae = bfs(1)[1:]
print(*kotae, sep=' ')

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
T=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    T[a].append(b)
tasu=[0]*(n+1)
for i in range(q):
    p,x=map(int,input().split())
    tasu[p]+=x
    
def dfs(a):
    ans=[0]*(n+1)
    stack=[a]
    ans[a]=tasu[a]
    while stack:
        x=stack.pop()
        moto=ans[x]
        for w in T[x]:
            ans[w]=moto+tasu[w]
            stack.append(w)
    return ans
kotae = dfs(1)[1:]
print(*kotae, sep=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
T=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    T[a].append(b)
tasu=[0]*(n+1)

for i in range(q):
    p,x=map(int,input().split())
    tasu[p]+=x
def bfs(a):
    ans=[0]*(n+1)
    q=deque([a])
    ans[a]=tasu[a]
    while q:
        x=q.popleft()
        moto=ans[x]
        for w in T[x]:
            ans[w]=moto+tasu[w]
            q.append(w)
    return ans
kotae = bfs(1)[1:]
print(*kotae, sep=' ')

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a = [0] * n
b = [0] * n
for i in range(n-1):
    ta,tb = map(int,input().split())
    a[tb-1] = ta -1
for j in range(q):
    ta,tb = map(int,input().split())
    b[ta-1] += tb
for k in range(1,n):
    b[k] += b[a[k]]
print(*b)

Submission status: Wrong Answer

Solution code:



from collections import defaultdict
from collections import deque

n,q = map(int,input().split())
a = [0]*n
b = [0]*n
c = [0]*n
for i in range(n-1):
    ta,tb = map(int,input().split())
    # d[ta-1].append(tb-1)
    # d[tb-1].append(ta-1)
    a[tb-1] = ta-1
for i in range(q):
    ta,tb = map(int,input().split())
    b[ta-1] +=tb
for i in range(1,n):
    b[i]+=b[a[i]]
print(*b)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

n,q = map(int,input().split())
res = [0]*n
dic = [0]*n
d = defaultdict(list)
dp = [0]*n
ep = [0] * n
for i in range(n-1):
    ta,tb = map(int,input().split())
    d[ta-1].append(tb-1)
    d[tb-1].append(ta-1)
    dic[tb-1] = ta-1
for i in range(q):
    ta,tb = map(int,input().split())
    dp[ta-1] +=tb
for i in range(1,n):
    dp[i]+=dp[dic[i]]
print(*dp)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,q = inpl()
g = [[]for i in range(n)]
c = [0 for i in range(n)]
seen = [False] * n
for i in range(n-1):
    a,b = inpl()
    g[a-1].append(b-1)
    g[b-1].append(a-1)
for i in range(q):
    a,b = inpl()
    c[a-1] += b
def dfs(node):
    for v in g[node]:
        if v in seen:
            continue
        c[v] += c[node]
        seen[v] = True
        dfs(v) 
seen[0] = True
dfs(0)
for i in c:
    print(i)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a_b = [[int(i) for i in input().split()] for _ in range(n-1)]
p = [0]*q
x = [0]*q
for i in range(q): 
    p[i],x[i] = map(int,input().split())
c = [0]*n

for i in range(q): 
    c[p[i]-1] += x[i]

for i in range(n-1): 
    c[a_b[i][1]-1] += c[a_b[i][0]-1]

for i in range(n-1):
    print(c[i],end=" ")
else:
    print(c[n-1])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
a_b = [[int(i) for i in input().split()] for _ in range(n-1)]
a_b.sort(key= lambda x:x[0])
p = [0]*q
x = [0]*q
for i in range(q): 
    p[i],x[i] = map(int,input().split())
c = [0]*n

for i in range(q): 
    c[p[i]-1] += x[i]

for i in range(n-1): 
    c[a_b[i][1]-1] += c[a_b[i][0]-1]
    
for i in range(n-1):
    print(c[i],end=" ")
else:
    print(c[n-1])

Submission status: Wrong Answer

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

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

cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

for i in range(1, n):
    cost[i] += cost[tree[i]]    

print(*cost)

Submission status: Wrong Answer

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

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

tree.sort()
cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

for i in range(1, n):
    cost[i] += cost[tree[i]]    

print(*cost)

Submission status: Wrong Answer

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

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

tree.sort()
cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

for a, b in tree:
    cost[a] += cost[b]

print(*cost)

Submission status: Wrong Answer

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

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

cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

for a, b in tree:
    cost[a] += cost[b]

print(*cost)

Submission status: Wrong Answer

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

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

cost = [0 for _ in range(n)]
for _ in range(q):
    pos, point = map(int, input().split())
    cost[pos-1] += point

ans = [0 for _ in range(n)]
ans[0] = cost[0]
for a, b in tree:
    ans[a] = ans[b] +cost[a]

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y


N,Q=LI()
TR=[[] for i in range(N+1)]
TR_score = [0] * (N+1)
RES = [0]*(N+1)

for i in range(N-1):
    i,j=LI()
    TR[i].append(j)

for i in range(Q):
    p, x = LI()
    TR_score[p] += x

ST = [(1, TR_score[1])]
while ST:
    i, score = ST.pop()
    RES[i] = score
    for j in TR[i]: ST.append((j, score + TR_score[j]))

for value in RES[1:-1]: print(value, end=" ")
print(RES[-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
N,Q = map(int,input().split())
ab = [ tuple(map(int,input().split())) for i in range(N-1) ]
px = [ tuple(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    dic[ab[i][0]].append(ab[i][1])
ab=0
for i in range(Q):
    dic2[px[i][0]] += px[i][1]
px=0
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200001)
N,Q = map(int,input().split())
ab = [ tuple(map(int,input().split())) for i in range(N-1) ]
px = [ tuple(map(int,input().split())) for i in range(Q) ]
dic = {}
dic2=[0]*(N+1)
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    dic[ab[i][0]].append(ab[i][1])
ab=0
for i in range(Q):
    dic2[px[i][0]] += px[i][1]
px=0
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200001)
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) ]
dic = {}
dic2=[0]*N
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
for i in range(N-1):
    dic[ab[i][0]].append(ab[i][1])
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1-1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node-1]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200001)
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) ]
dic = {}
dic2=[0]*N
dic3=[0]*N

for i in range(N):
    dic[i+1] = []
    #dic2[i+1] = 0
    #dic3[i+1] = 0
for i in range(N-1):
    tmp1 = ab[i][0]
    tmp2 = ab[i][1]
    dic[tmp1].append(tmp2)
for i in range(Q):
    tmp1 = px[i][0]
    tmp2 = px[i][1]
    dic2[tmp1-1] += tmp2
def rec(now_node,prevval):
    nowval = prevval + dic2[now_node-1]
    dic3[now_node-1] = nowval
    for elem in dic[now_node]:
        rec(elem,nowval)
rec(1,0)
res = [str(dic3[i-1]) for i in range(1,N+1)]
print(' '.join(res))

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)]

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

pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].append(b)
for p,x in PX:
    pt[p]+=x
eda=[sorted(i) for i in eda]

stack=[[1,eda[1]]]
while stack:
    while stack[-1][1]:
        a=stack[-1][1].pop()
        pt[a]+=pt[stack[-1][0]]
        stack.append([a,eda[a]])
    stack.pop()
print(" ".join(list(map(str,pt[1:]))))

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)]

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

pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].append(b)
for p,x in PX:
    pt[p]+=x
eda=[sorted(i) for i in eda]

for i,lst in enumerate(eda):
    for e in lst:
        pt[e]+=pt[i]
print(" ".join(list(map(str,pt[1:]))))

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)]

eda=[[] for i in range(N+1)]
pt= [0 for i in range(N+1)]
for a,b in AB:
    eda[a].append(b)
for p,x in PX:
    pt[p]+=x

for i,lst in enumerate(eda):
    for e in lst:
        pt[e]+=pt[i]
print(" ".join(list(map(str,pt[1:]))))