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:
#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った
#は？結局他人の使わないやんけ

n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
tree1=[i for i in tree1 if i not in tree2]

l=0
while tree1!=[]:
    for j in tree2[:][l:]:
        l+=1
        for k in tree1[:]:
            if j[1]==k[0]:
                tree2.append(k)
                tree1.remove(k)
            elif j[1]==k[1]:
                tree2.append(k[::-1])
                tree1.remove(k)

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

for i in tree2:
    c[i[1]-1]+=c[i[0]-1]

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

Submission status: Wrong Answer

Solution code:
#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った

n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
tree1=[i for i in tree1 if i not in tree2]

l=0
while tree1!=[]:
    for j in tree2[:]:
        for k in tree1[:]:
            if j[1]==k[0]:
                tree2.append(k)
                tree1.remove(k)
            if j[1]==k[1]:
                tree2.append(k[::-1])
                tree1.remove(k)

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

for i in tree2:
    c[i[1]-1]+=c[i[0]-1]

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC138 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,q = LI()
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = LI()
    graph[a-1].append(b-1)

def dfs(x,p):
    lst.append(x)
    for i in graph[x]:
        if i != p:
            dfs(i,x)
    lst.append(p)
lst = []
dfs(0,0)
lst.pop()
inde = [[-1,-1] for _ in range(n)]
for i in range(len(lst)):
    if inde[lst[i]][0] < 0:
        inde[lst[i]][0] = i
for i in range(len(lst))[::-1]:
    if inde[lst[i]][1] < 0:
        inde[lst[i]][1] = i

cnt = [0]*(len(lst)+1)
for _ in range(q):
    p,x = LI()
    cnt[inde[p-1][0]] += x
    cnt[inde[p-1][1]+1] -= x
cnt = list(accumulate(cnt))
ans = []
for i in range(n):
    ans.append(cnt[inde[i][0]])
print(*ans)

Submission status: Wrong Answer

Solution code:
#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい
#aiがbiの親であるとは限らないらしい
#これがある以上、木構造を作るしかない
#辞書で(他の人のコードを参照)親子関係を保持できる、初めて知った

n,q=map(int,input().split())
tree1=[list(map(int,input().split())) for i in range(n-1)]
tree2=[i for i in tree1 if i[0]==1]
for i in tree1[:]:
    if i in tree2:
        tree1.remove(i)
l=0
while tree1!=[]:
    #print(tree2)
    #print(tree1)
    for j in tree2[:]:
        for k in tree1[:]:
            if j[1]==k[0]:
                tree2.append(k)
                tree1.remove(k)
            if j[1]==k[1]:
                tree2.append(k[::-1])
                tree1.remove(k)


#print(tree)
#ab=[list(map(int,input().split())) for i in range(n-1)]
#ab.sort(key=lambda x:(x[0],x[1]))
c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
#print(tree2)

for i in tree2:
    c[i[1]-1]+=c[i[0]-1]

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

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit


setrecursionlimit(2 * 10**5) #再帰の回数制限を増やす


def add_num(i, total, parent=-1):
    """隣接リストを取り出して深さ優先探索
    自分より上層の total のカウント数と部分木の頂点としてのカウント数を足す
    親は無視する"""
    for p in adj[i]:
        if p != parent:
            ans_lst[p] = total + tmp[p]
            if adj[p] != [parent]:
                add_num(p, total + tmp[p], i)
    return
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] for _ in range(N)] # 隣接リスト
tmp = [0] * N # 部分木の根としてのカウント数 
ans_lst = [0] * N # 答えのリスト

# 隣接リストの作成
# a が親とは限らないらしいので両方とも記録
for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)
    adj[b].append(a)

print(adj)
# ｔｍｐリストの作成
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x

ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, total)

print(*ans_lst, sep=' ')

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit


setrecursionlimit(2 * 10**5) #再帰の回数制限を増やす


def add_num(i, total):
    """隣接リストを取り出して深さ優先探索
    自分より上層の total のカウント数と部分木の頂点としてのカウント数を足す"""
    for p in adj[i]:
        ans_lst[p] = total + tmp[p]
        if adj[p]:
            add_num(p, total + tmp[p])
    return
    
    
N, Q = [int(i) for i in input().split()]

adj = [[] for _ in range(N)] # 隣接リスト
tmp = [0] * N # 部分木の根としてのカウント数 
ans_lst = [0] * N # 答えのリスト

# 隣接リストの作成
for _ in range(N - 1):
    a, b = [int(i) - 1 for i in input().split()]
    adj[a].append(b)

# ｔｍｐリストの作成
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    tmp[p-1] += x

ans_lst[0] = tmp[0]
total = tmp[0]
add_num(0, total)

print(*ans_lst, sep=' ')

Submission status: Wrong Answer

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

edge=[list(map(int, input().split())) for i in range(N-1)]
adds=[list(map(int, input().split())) for i in range(Q)]

point=[0]*N

for i in range(Q):
  point[adds[i][0]-1]+=adds[i][1]
  
for i in range(N-1):
  point[edge[i][1]-1]+=point[edge[i][0]-1]
  
print(' '.join(map(str, point)))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tree={}
for i in range(n-1):
    a,b=map(int,input().split())
    if a in tree:
        tree[a].append(b)
    else:
        tree[a]=[b]
c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
for j in tree:
    for k in tree[j]:
        c[k-1]+=c[j-1]
for i in range(n-1):
    print(c[i],end=" ")
print(c[n-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)]
ab.sort(key=lambda x:(x[0],x[1]))
c=[0]*n
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x
for j in range(n-1):
    c[ab[j][1]-1]+=c[ab[j][0]-1]
for i in range(n-1):
    print(c[i],end=" ")
print(c[n-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)]
#px.sort(key=lambda x:x[0])
ab.sort(key=lambda x:x[0])
c=[0]*n
#print(c)
for i in range(q):
    p,x=map(int,input().split())
    c[p-1]+=x

for j in range(n-1):
    c[ab[j][1]-1]+=c[ab[j][0]-1]

for i in range(n-1):
    print(c[i],end=" ")
print(c[n-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)]
px.sort(key=lambda x:x[0])
ab.sort(key=lambda x:x[0])
c=[0]*n
#print(c)
for i in range(q):
    c[px[i][0]-1]+=px[i][1]

for j in range(n-1):
    c[ab[j][1]-1]+=c[ab[j][0]-1]

for i in range(n-1):
    print(c[i],end=" ")
print(c[n-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)]
px.sort(key=lambda x:x[0])
ab.sort(key=lambda x:x[0])
c=[0]*n
#print(c)
for i in range(q):
    c[px[i][0]-1]+=px[i][1]

for j in range(n-1):
    c[ab[j][1]-1]+=c[ab[j][0]-1]

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

Submission status: Wrong Answer

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


def flatten(data):
    return [element
            for item in data
            for element in (flatten(item) if hasattr(item, '__iter__') else [item])]

def search_append(tree,an,bn):
    if an in tree:
        tree.append([bn])
    else:
        for j in tree:
            if isinstance(j,list):
                search_append(j,an,bn)

def search_count(tree,counter,pn,xn):
    if pn in tree:
        for i in flatten(tree):
            counter[i-1]+=xn
    else:
        for j in tree:
            if isinstance(j,list):
                search_count(j,counter,pn,xn)


t=[1]

for i in range(n-1):
    a,b=input().split()
    a,b=int(a),int(b)
    search_append(t,a,b)

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

for i in range(q):
    p,x=input().split()
    p,x=int(p),int(x)
    search_count(t,c,p,x)

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

print("")
'''

#木の問題と書いてあるが、実際は木の構造がわからなくても解ける
#根の方から順に足していってあげればいい

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)]
px.sort(key=lambda x:x[0])
c=[0]*n
#print(c)
for i in range(q):
    c[px[i][0]-1]+=px[i][1]

for j in range(n-1):
    c[ab[j][1]-1]+=c[ab[j][0]-1]

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
a = [0] * (N - 1)
b = [0] * (N - 1)
p = [0] * Q
x = [0] * Q
for i in range(N - 1):
    a[i], b[i] = map(int, input().split())
aa, bb = zip(*sorted(zip(a, b)))
for i in range(Q):
    p[i], x[i] = map(int, input().split())
pp, xx = zip(*sorted(zip(p, x)))
ans = [0] * N
for i in range(Q):
    ans[pp[i] - 1] += xx[i]
for i in range(N - 1):
    ans[bb[i] - 1] += ans[aa[i] - 1]
print(*ans)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
a = [0] * (N - 1)
b = [0] * (N - 1)
p = [0] * Q
x = [0] * Q
for i in range(N - 1):
    a[i], b[i] = map(int, input().split())
aa, bb = zip(*sorted(zip(a,b)))
for i in range(Q):
    p[i], x[i] = map(int, input().split())
ans = [0] * N
for i in range(Q):
    ans[p[i] - 1] += x[i]
for i in range(N - 1):
    ans[bb[i] - 1] += ans[aa[i] - 1]
print(*ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[0]*(N-1)
b=[0]*(N-1)
p=[0]*Q
x=[0]*Q
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],x[i]=map(int,input().split())
v=[0]*N
 
for i in range(Q):
  num=p[i]-1
  v[num]+=x[i]
  
for i in range(N-1):
  numa=a[i]-1
  numb=b[i]-1
  v[numb]+=v[numa]
  
for i in range(N):
  print(v[i],end=" ")

Submission status: Wrong Answer

Solution code:
N,Q=map(int,input().split())
a=[0]*(N-1)
b=[0]*(N-1)
p=[0]*Q
x=[0]*Q
for i in range(N-1):
  a[i],b[i]=map(int,input().split())
for i in range(Q):
  p[i],x[i]=map(int,input().split())
v=[0]*N
 
for i in range(Q):
  box=[]
  box.append(p[i])
  while True:
    y=0
    for k in range(N-1):
      if a[k] in box :
        if not b[k] in box:
          box.append(b[k])
          y=1
    if y==0 : break
  for l in range(len(box)):
    num=box[l]-1
    v[num]+=x[i]
 
for h in range(N):
  print(v[h], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = [int(x) for x in input().split()]
counter_increments = {}
parents = [None for i in range(N+1)] # parents[i] is parent of vertex numberd i
for _ in range(N-1): # each node except 1 has exactly 1 parent
    a, b = [int(x) for x in input().split()]
    parents[b] = a
for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    counter_increments[p] = counter_increments.get(p, 0) + x
try:
    result = [counter_increments.get(1, 0)] # results[i] corresponds to vertex i+1
    for i in range(2, N+1):
        result.append(result[parents[i]-1] + counter_increments.get(i, 0))
    print(' '.join(str(r) for r in result))
except:
    print('hi')

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
G = [[] for _ in range(N)]

count = [0]*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)
for i in range(Q):
    p, x = map(int, input().split())
    count[p-1]+=x
q = deque([0])

print(G)

flag = [False]*N
while q:
    v = q.pop()
    flag[v] = True
    for e in G[v]:
        if flag[e]:
            continue
        count[e]+=count[v]
        q.append(e)
print(*count)

Submission status: Wrong Answer

Solution code:
N, Q = list(map(int,input().split()))
a = []
b = []
p = []
x = []

for i in range(N-1):
    a_, b_ = list(map(int,input().split()))
    a.append(a_ - 1)
    b.append(b_ - 1)

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

for i in range(N-1):    
    parent[int(b[i])] = int(a[i])

ans = []
for i in range(N):
	ans.append(0)

for i in range(Q):
    ans[p[i]] += x[i]
    
for i in range(N):
    if parent[i] != -1:
        ans[i] += ans[parent[i]]

print(' '.join([str(c) for c in ans]))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split(' '))
 
listTree = [[] for i in range(N)]
listNode = [0] * N
 
for i in range(N-1):
  a, b  = map(int, input().split(' '))
  listTree[a-1] += [b]

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

listResult = [0] * N
 
def calcNode(target):
  listResult[target - 1] = listNode[target - 1]
  targets = listTree[target - 1]
  for j in targets:
    listNode[j - 1] += listNode[target - 1]
  return targets

next_targets = [1]
while True:
  _next_targets = []
  for i in next_targets:
    result = calcNode(i)
    _next_targets += result
  next_targets = _next_targets
  if len(next_targets) == 0:
    break

str_result = ' '.join(map(str, listResult))
print(str_result)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10000000)

def dfs(tree, root, counter, parent):
    counter[root] += parent
    count = counter[root]
    childs = tree[root]
    for c in childs:
        dfs(tree, c, counter, count)


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

counter = [0 for _ in range(n)]
children = [[] for _ in range(n)]

# read a_i b_i
for _ in range(n-1):
    a, b = map(int, input().split())
    children[a-1].append(b-1)

# read p_i x_i
for _ in range(q):
    p, xi = map(int, input().split())
    counter[p-1] += xi

dfs(children, 0, counter, 0)

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

Submission status: Wrong Answer

Solution code:
N,Q=[int(s) for s in input().split()]
e_list=[[int(s) for s in input().split()] for i in range(N-1)]
answer=[0 for i in range(N+1)]
for i in range(Q):
  a,b=[int(s) for s in input().split()]
  answer[a]+=b
for e in e_list:
  answer[e[1]]+=answer[e[0]]
print(*answer[1:])

Submission status: Wrong Answer

Solution code:
N,Q=[int(s) for s in input().split()]
e_list=[[int(s) for s in input().split()] for i in range(N-1)]
answer=[0 for i in range(N+1)]
for i in range(Q):
  a,b=[int(s) for s in input().split()]
  answer[a]+=b
for e in e_list:
  answer[e[1]]+=answer[e[0]]
answer.remove(0)
print(*answer)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
C = [0 for _ in range(N)]
d = deque()
d.append(0)

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

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

while len(d) > 0:
  t = d.popleft()
  for i in tree[t]:
      C[i] += C[t]
      d.append(i)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = map(int, input().split())
tree = [[] for i in range(N)]
C = [0 for i in range(N)]
ckd = []
d = deque()
d.append(0)

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

while d:
  t = d.popleft()
  ckd.append(t)
  for i in tree[t]:
    if i not in ckd:
      C[i] += C[t]
      d.append(i)

for i in C:
  print(i, end = " ")
  
print("\n")

Submission status: Wrong Answer

Solution code:
N,Q=[int(s) for s in input().split()]
e_list=[[int(s) for s in input().split()] for i in range(N-1)]
q_list=[[int(s) for s in input().split()] for i in range(Q)]
#########深さ優先探索DFS
#########到達可能なもののうち新しく追加されたものを見る
#########訪れた順番を出力
def euler_tour(edge_list,start):
  reach=[[start,1,0]]
  ans=[start]
  path=[start]
  e_list=[[e[0],e[1],0] for e in edge_list]
  vertex=start
  endflag=0
  depth=1
  while True :
    ###まずvertexから到達可能なものをすべてreachに入れる
    #辺の始点がvertexであるもの
    for e in e_list:
      if (e[0]==vertex and e[2]==0):
        reach.append([e[1],0,depth])
        e[2]=1
      elif (e[1]==vertex and e[2]==0):
        reach.append([e[0],0,depth])
        e[2]=1
    ###起点になっていないもののうち最後にreachに入ったものを、次の起点とする
    for i in range(len(reach)+1):
      if reach[-i][1]==0 :
        vertex=reach[-i][0]
        #####depthが戻ったか？
        if reach[-i][2]<depth:
           ##戻った分だけpathをansに追加
          for j in range(1,depth-reach[-i][2]+1):
            path.remove(path[-1])
            ans.append(path[-1])
        ans.append(vertex)
        path.append(vertex)
        depth=reach[-i][2]+1
        #####ここでprint(vertex)などを入れる
        reach[-i][1]=1
        break
      if i==len(reach):  ####全ての場所に行っていれば終了
        for j in range(len(path)-1):
            path.remove(path[-1])
            ans.append(path[-1])
        return ans
        break
euler=euler_tour(e_list,1)
def eu(num):
  if num==1:
    return [i+1 for i in range(N)]
  ans=[]
  i=-1
  while True :
    i+=1
    if euler[i]==num:
      ii=euler[i-1]
      break
  while True:
    if euler[i]==ii:
      break
    ans.append(euler[i])
    i+=1
  return list(set(ans))
answer=[0 for i in range(N)]
for q in q_list:
  for v in eu(q[0]):
    answer[v-1]+=q[1]
print(answer)

Submission status: Wrong Answer

Solution code:
def main(edge, add_list):
    node_len = len(edge)
    counter = [0] * node_len

    for i in range(node_len):
        counter[i] += add_list[i]
        for item in edge[i]:
            counter[item] += counter[i]
            while i in edge[item]:
                edge[item].remove(i)

    return " ".join(str(i) for i in counter)


if __name__ == "__main__":
    n, q = map(int, input().split())
    edge = [[] for _ in range(n)]

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

    add_list = [0]*n

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

    print(main(edge, add_list))

Submission status: Wrong Answer

Solution code:
def main(edge, add_list):
    node_len = len(edge)
    counter = [0] * node_len

    for i in range(node_len):
        counter[i] += add_list[i]
        for item in edge[i]:
            counter[item] += counter[i]
            if i in edge[item]:
                edge[item].remove(i)

    return " ".join(str(i) for i in counter)


if __name__ == "__main__":
    n, q = map(int, input().split())
    edge = [[] for _ in range(n)]

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

    add_list = [0]*n

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

    print(main(edge, add_list))

Submission status: Wrong Answer

Solution code:
import copy

counter = {}
done = []
tree = []


def main():
    N, Q = map(int, input().split(' '))

    # init counter for result
    for i in range(1, N + 1):
        counter[i] = 0

    # make tree
    for i in range(N - 1):
        branch = list(map(int, input().split(' ')))
        tree.append(copy.copy(branch))

    # caliculation
    for i in range(Q):
        target, point = list(map(int, input().split(' ')))
        add_point(target, point)
        done.clear()

    result = list(map(str, counter.values()))
    print(' '.join(result))


def add_point(target, point):
    if target not in done:
        counter[target] += point
        done.append(target)

        # search child branch..
        for b in tree:
            if target == b[0]:
                # add point child branch..
                add_point(b[1], point)


main()

Submission status: Wrong Answer

Solution code:
import copy

counter = {}
done = []
tree = []


def main():
    N, Q = map(int, input().split(' '))

    # init counter for result
    for i in range(1, N + 1):
        counter[i] = 0

    # make tree
    for i in range(N - 1):
        branch = list(map(int, input().split(' ')))
        tree.append(copy.copy(branch))

    # caliculation
    for i in range(Q):
        target, point = list(map(int, input().split(' ')))
        add_point(target, point)
        done.clear()

    result = list(map(str, counter.values()))
    print(' '.join(result))


def add_point(target, point):
    if target not in done:
        counter[target] += point
        done.append(target)

    # search child branch..
    for b in tree:
        if target == b[0]:
            # add point child branch..
            add_point(b[1], point)


main()

Submission status: Wrong Answer

Solution code:
import collections

N, Q = map(int, input().split())
Links = [[] for _ in range(N)]
AnsArr = [0] * N

# 辺の数だけ
for i in range(N - 1):
    a, b = map(int, input().split())
    if a < b:
        Links[a - 1].append(b - 1)
    else:
        Links[b - 1].append(a - 1)

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

# BFS
# 頂点1から開始
q = collections.deque([0])
visited = [-1] * N
while q:
    t = q.pop()
    for e in Links[t]:
        # if visited[e] == t:
        #     continue
        # visited[t] = e
        # visited[e] = t
        # 親の値を子に受け継ぐ
        AnsArr[e] += AnsArr[t]
        q.appendleft(e)

print(*AnsArr)

Submission status: Wrong Answer

Solution code:
import collections

N, Q = map(int, input().split())
Links = [[] for _ in range(N)]
AnsArr = [0] * N

# 辺の数だけ
for i in range(N-1):
    a, b = map(int, input().split())
    Links[a - 1].append(b - 1)

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

# BFS
# 頂点1から開始
q = collections.deque([0])
visited = [-1] * N
while q:
    t = q.pop()
    for e in Links[t]:
        if visited[e] == t :
            continue
        visited[t] = e
        visited[e] = t
        # 親の値を子に受け継ぐ
        AnsArr[e] += AnsArr[t]

        q.appendleft(e)

print(*AnsArr)

Submission status: Wrong Answer

Solution code:
import collections

N, Q = map(int, input().split())
Links = [[] for _ in range(N)]
AnsArr = [0] * N

# 辺の数だけ
for i in range(N-1):
    a, b = map(int, input().split())
    Links[a - 1].append(b - 1)

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

# BFS
# 頂点1から開始
q = collections.deque([0])
visited = [0] * N
while q:
    t = q.pop()
    visited[t] = 1
    for e in Links[t]:
        if visited[e]:
            continue
        # 親の値を受け継ぐ
        AnsArr[e] += AnsArr[t]
        q.appendleft(e)

print(*AnsArr)

Submission status: Wrong Answer

Solution code:
import collections

N, Q = map(int, input().split())
Links = [[] for _ in range(N)]
AnsArr = [0] * N

# 辺の数だけ
for i in range(N-1):
    a, b = map(int, input().split())
    Links[a - 1].append(b - 1)

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

# BFS
# 頂点1から開始
q = collections.deque([0])
while q:
    t = q.pop()
    for e in Links[t]:
        # 親の値を受け継ぐ
        AnsArr[e] += AnsArr[t]
        q.appendleft(e)

print(*AnsArr)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

N, Q = map(int, input().split())
node = [list() for i in range(N+1)]
count = [0] * len(node)
ans = [0] * len(node)

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

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

for i in range(1, len(node)):
    x = count[i]
    for j in node[i]:
        count[j] += x

print(*count[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

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

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

def add_tree(x, i):
    global node, count
    count[i] += x
    x = count[i]
    for j in node[i]:
        add_tree(x, j)

add_tree(0, 1)

#print(N, Q)
#print(node)
#print(count)
print(*count[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,q = map(int,input().split())
a_b_list = [list(map(int,input().split())) for i in range(n-1)]
p_x_list = [list(map(int,input().split())) for i in range(q)]

graph_direct_list = [[] for _ in range(n+1)]
for i in range(n-1):
    a_i = a_b_list[i][0]
    b_i = a_b_list[i][1]
    graph_direct_list[a_i].append(b_i)
    graph_direct_list[b_i].append(a_i)

counter_init_list = [0 for _ in range(n+1)]

for j in range(q):
    p_j = p_x_list[j][0]
    x_j = p_x_list[j][1]
    counter_init_list[p_j] += x_j

counter_list = [0 for _ in range(n+1)]

for k in range(n+1):
    counter_list[k] += counter_init_list[k]
    for l in range(len(graph_direct_list[k])):
        if graph_direct_list[k][l] > k:
            counter_list[graph_direct_list[k][l]] += counter_list[k]

print(*counter_list[1:])

Submission status: Wrong Answer

Solution code:
n,q    = [int(i) for i in input().split()]
counts = [0 for i in range(n)]
parents= [-1 for i in range(n)]

for j in range(n-1):
  a,b = [int(i) for i in input().split()]
  parents[b-1]=a-1
  
for j in range(q):
  node,score = [int(i) for i in input().split()]
  counts[node-1] += score
  
for i in range(1,n):
  counts[i] += counts[parents[i]]
  
for i in range(n):
  print(counts[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
# print(n,q)
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]

# print(tree)
# print(px)

c = [0]*n

for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]

# print(c)

# for i in range(1,n):
#     c[i] += c[min(tree[i])-1]

visited = [False]*n
visited[0] = True
# print(visited)
check = visited.count(False)
# print(check)
while check!=0:
    for i in range(1,n):
        # mi = min(tree[i])-1
        for j in range(len(tree[i])):
            node_tmp = tree[i][j]
            if visited[node_tmp-1]:
                c[i] += c[node_tmp-1]
                visited[i] = True
                # print("OK")
            else:
                pass
                # print("NG")
    check = visited.count(False)

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int, input().split())
# print(n,q)
tree = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int, input().split())
    tree[a-1].append(b)
    tree[b-1].append(a)
px = [ list(map(int,input().split())) for _ in range(q) ]

print(tree)
print(px)

c = [0]*n

for i in range(len(px)):
    c[px[i][0]-1] += px[i][1]

print(c)

# for i in range(1,n):
#     c[i] += c[min(tree[i])-1]

visited = [False]*n
visited[0] = True
# print(visited)
check = visited.count(False)
# print(check)
while check!=0:
    for i in range(1,n):
        # mi = min(tree[i])-1
        for j in range(len(tree[i])):
            node_tmp = tree[i][j]
            if visited[node_tmp-1]:
                c[i] += c[node_tmp-1]
                visited[i] = True
                # print("OK")
            else:
                pass
                # print("NG")
    check = visited.count(False)

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