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:
import sys
sys.setrecursionlimit(500*500) #再起上限回数突破

class Node:
    class DummyNode:
        def __init__(self):
            self.parent = None
            self.data = 0
        
    def __init__(self, idx):
        self.idx = idx
        self.data = 0
        self.parent = Node.DummyNode()
        self.children = list()
        
    def __repr__(self):
        return str(self.data) + "," + str(self.children)
        
    def add_counter(self, a):
        self.data += a
        for child in self.children:
            child.add_counter(a)
            
    def print_counter(self):
        self.data += self.parent.data
        print(self.data)
        
        
N,Q = map(int, input().split() )

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

for _ in range(N-1):
    a,b = map(int, input().split() )
    #nodes[a-1].children.append(nodes[b-1])
    nodes[b-1].parent = nodes[a-1]
    
#for node in nodes:
#    print(node)

for _ in range(Q):
    p,x = map(int, input().split() )
    #nodes[p-1].add_counter(x)
    nodes[p-1].data += x
    
for node in nodes:
    node.print_counter()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500*500) #再起上限回数突破

class Node:
    def __init__(self, idx):
        self.idx = idx
        self.data = 0
        self.children = list()
        
    def __repr__(self):
        return str(self.data) + "," + str(self.children)
        
    def add_counter(self, a):
        self.data += a
        for child in self.children:
            child.add_counter(a)
        
N,Q = map(int, input().split() )

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

for _ in range(N-1):
    a,b = map(int, input().split() )
    nodes[a-1].children.append(nodes[b-1])
    
#for node in nodes:
#    print(node)

for _ in range(Q):
    p,x = map(int, input().split() )
    nodes[p-1].add_counter(x)
    
for node in nodes:
    print(node.data)

Submission status: Wrong Answer

Solution code:
"""
keyword: dfs
"""

import sys
sys.setrecursionlimit(10**6)

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

def dfs(v, p=-1): # v:これから見るnode、p:親node
    global ans
    ans[v] += ans[p]
    for i in range(len(to[v])):
        u = to[v][i]
        if u == p: continue # 親nodeは確認済みなので見ない
        dfs(u, v)

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

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

dfs(0)

print(*ans)

Submission status: Wrong Answer

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

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

def dfs(v, p=-1): # v:これから見るnode、p:親node
    global ans
    ans[v] += ans[p]
    for i in range(len(to[v])):
        u = to[v][i]
        if u == p: continue # 親nodeは確認済みなので見ない
        dfs(u, v)

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

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

dfs(0)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

branch = [[] for _ in range(N)]
res = [0,]*N


def dfs(p, x):
    res[p-1] += x
    for q in branch[p-1]:
        dfs(q, x)


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


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

print(*res)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
v = [[0,0] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    v[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    v[p][1] += x
q = [1]
while q:
    now = q.pop()
    for i in v[now][2:]:
        v[i][1] += v[now][1]
    q += v[now][2:]
s = []
for i in v:
    s.append(i[1])
print(*s[1:])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
v = [[0,0] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    v[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    v[p][1] += x
q = [1]
while q:
    now = q.pop()
    # print(now)
    for i in v[now][2:]:
        v[i][1] += v[now][1]
    q += v[now][2:]
    # print(q)
print(v)
s = []
for i in v:
    s.append(i[1])
print(*s[1:])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque

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

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


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


for i in range(q):
    p, x = list(map(int, input().split()))

    queue = deque([p-1])
    while(len(queue) > 0):
        index = queue.pop()
        count[index] += x

        for index2 in tree[index]:
            queue.append(index2)

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

Submission status: Wrong Answer

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

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

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


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


for i in range(q):
    p, x = list(map(int, input().split()))

    queue = [p-1]
    while(len(queue) > 0):
        index = queue.pop()
        count[index] += x

        for index2 in tree[index]:
            queue.append(index2)

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

Submission status: Wrong Answer

Solution code:
import sys


def count(i, r=-1):
    for e in es[i]:
        if e != r:
            counter[e] += counter[i] + rc[e]
            count(e, i)


if __name__ == "__main__":
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    es = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a-1].append(b-1)
    counter = [0]*N
    rc = [0]*N
    for _ in range(Q):
        p, x = map(int, input().split())
        rc[p-1] += x
    counter[0] = rc[0]
    count(0)
    print(*counter)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, Q = map(int, input().split())
child = [[i] for i in range(N)]
counter = [0] * N
graph = [deque([]) for _ in range(N + 1)]
for i in range(N - 1):
  a, b =  map(int, input().split())
  graph[a - 1].append(b - 1)
  
for i in range(Q):
  p, x = map(int, input().split())
  counter[p - 1] += x 

#print(graph, counter)
# 深さ優先探索
def dfs(v):
    #global time
    #time += 1
    visited[v] = 1
    stack = [v]
    #print(stack, v)
    #arrive_time[v] = time
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if visited[w] == 0:
              visited[w] = 1
              counter[w] += counter[v]
              stack.append(w) 
        else:
            stack.pop()          
    return 0

visited = [0] * N
# 孤立している頂点対策
for i in range(N):
    if visited[i] == 0:
       dfs(i)


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

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,Q = IL()
graph = [[] for i in range(N)]

for _ in range(N-1):
  a,b = IL()
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)
  
count = [0 for i in range(N)]
newQ = [0 for i in range(N)]

Q = ILL(Q)

for x,n in Q:
  newQ[x-1] += n
  
q = deque([0])
count[0] = newQ[0]
while q:
  x = q.pop()
  for y in graph[x]:
    if count[y] == 0:
      count[y] = count[x] + newQ[y]
      q.append(y)

print(*count)

Submission status: Wrong Answer

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

children=[[] for _ in range(N)]
for _ in range(N-1):
  A,B=map(int,input().split())
  children[A-1].append(B-1)
  
points=[0]*N
for i in range(Q):
  P,X=map(int,input().split())
  points[P-1]+=X
  
for i in range(N):
  print(points[i],end=" ")
  for c in children[i]:
    points[c]+=points[i]

Submission status: Wrong Answer

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

children=[[] for _ in range(N)]
for _ in range(N-1):
  A,B=map(int,input().split())
  children[A-1].append(B-1)
  
points=[0]*N
for i in range(Q):
  P,X=map(int,input().split())
  points[P-1]+=X
  
for i in range(N):
  print(points[i])
  for c in children[i]:
    points[c]+=points[i]

Submission status: Wrong Answer

Solution code:
# 20/5/28
import sys
sys.setrecursionlimit(3 * 10 ** 5)

def bfs(root):
    global AL, cnt, visited
    add = cnt[root]
    cnt[root] = -1  # 訪問済
    for v in AL[root]:
        if cnt[v] >= 0:
            cnt[v] += add
            bfs(v)

N, Q = [int(x) for x in input().split()]
AL = [[] for _ in range(N + 1)]
for i in range(1, N):
    a, b = [int(x) for x in input().split()]
    AL[a].append(b)
    AL[b].append(a)

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

bfs(1)
s = " ".join(map(str, cnt[1:]))
print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque

n, q = map(int, input().split())
a, b = [0] * (-1 + n), [0] * (n - 1)
path = [[] for _ in range(n)]
for i in range(n - 1):
    a[i], b[i] = map(int, input().split())
    a[i] -= 1
    b[i] -= 1
    path[a[i]].append(b[i])
    path[b[i]].append(a[i])
add = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    p -= 1
    add[p] += x
q = deque()
ans = [0] * n
# 根 0 から見ていく
q.append(0)
visit = [0] * n
visit[0] = 1
ans[0] = add[0]
while q:
    pos = q.popleft()
    for v in path[pos]:
        if visit[v] == 0:
            ans[v] = ans[pos] + add[v]
            q.append(v)
            visit[v] = 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
ab=[]
s={1}
for i in range(n-1):
  a,b=map(int,input().split())
  if a in s:
    ab.append([a,b])
  else:
    ab.append([b,a])
  s|={a,b}
ab.sort()
p,x=[0]*q,[0]*q
for i in range(q):
  p[i],x[i]=map(int,input().split())
c=[0]*(n+1)
for i in range(q):
  c[p[i]]+=x[i]
for ab_i in ab:
  c[ab_i[1]]+=c[ab_i[0]]
print(*c[1:])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

class Node(object):
    def __init__(self, num):
        self.num = num
        self.adjacency_list = []
        self.values = 0
        self.visited = False

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


for i in range(1, N + 1):
    exec("a"+str(i)+" = Node("+str(i)+")")

for i in range(N - 1): # 0 ~ N-2
    exec("a"+str(ab[i][0])+".adjacency_list.append("+str(ab[i][1])+")")#a>b
    exec("a"+str(ab[i][1])+".adjacency_list.append("+str(ab[i][0])+")")#b>a
    #append使った

for i in range(Q):
    exec("a" + str(px[i][0]) + ".values +=" + str(px[i][1]) )

def dfs138(parent, n, val_p): #children is list
    exec("global a" + str(n))
    exec("a" + str(n) + ".values += " + str(val_p))#parent kara keisyou
    val_n = eval("a" + str(n) + ".values") #val_n toshite syotoku
    adj = eval("a" + str(n) + ".adjacency_list")
    children = adj.copy()
    if parent in adj:
        children.remove(parent)
    for i in children:
        dfs138(n, i, val_n) #val_nを流す
#finish

dfs138([],1,0)

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,q=n1()
AB=n3(n-1)
PX=n3(q)

d={i:[] for i in range(1,n+1)}
for a,b in AB:
    d[a].append(b)

ans=[0]*(n+1)
for p,x in PX:
    ans[p]+=x

q=deque([1])
while len(q)>0:
    i=q.popleft()
    for j in d[i]:
        ans[j]+=ans[i]
        q.append(j)
        
print(*ans[1:])

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,q=n1()
AB=n3(n-1)
PX=n3(q)

d={i:[] for i in range(1,n+1)}
for a,b in AB:
    d[a].append(b)

ans=[0]*(n+1)
for p,x in PX:
    ans[p]+=x

for i in range(1,n+1):
    for j in d[i]:
        ans[j]+=ans[i]
        
print(*ans[1:])

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,q=n1()
AB=n3(n-1)
PX=n3(q)

d={i:[] for i in range(1,n+1)}
for a,b in AB:
    d[a].append(b)
for i in range(1,n+1):
    c=0
    while c<len(d[i]):
        d[i].extend(d[d[i][c]])
        c+=1

ans=[0]*(n+1)
for p,x in PX:
    ans[p]+=x
    for i in d[p]:
        ans[i]+=x
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import sys


def count(i, c):
    counter[i] = c+rc[i]
    for e in es[i]:
        if counter[e] == 0:
            count(e, counter[i])


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    N, Q = map(int, input().split())
    es = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a-1].append(b-1)
        es[b-1].append(a-1)
    counter = [0]*N
    rc = [0]*N
    for _ in range(Q):
        p, x = map(int, input().split())
        rc[p-1] += x
    count(0, 0)
    print(*counter)

Submission status: Wrong Answer

Solution code:
import sys


def count(i, c):
    counter[i] = c+rc[i]
    for e in es[i]:
        count(e, counter[i])


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    N, Q = map(int, input().split())
    es = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a].append(b)
    counter = [0]*(N+1)
    rc = [0]*(N+1)
    for _ in range(Q):
        p, x = map(int, input().split())
        rc[p] += x
    count(1, 0)
    for i in range(1, N+1):
        if i == N:
            print(counter[i], end="\n")
        else:
            print(counter[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys


def count(i, c):
    counter[i] += c+rc[i]
    for e in es[i]:
        count(e, counter[i])


if __name__ == "__main__":
    sys.setrecursionlimit(2*(10**5))
    N, Q = map(int, input().split())
    es = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        es[a].append(b)
    counter = [0 for _ in range(N+1)]
    rc = [0 for _ in range(N+1)]
    for _ in range(Q):
        p, x = map(int, input().split())
        rc[p] += x
    count(1, 0)
    for i in range(1, N+1):
        if i == N:
            print(counter[i], end="\n")
        else:
            print(counter[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

    cnt = [0 for _ in range(n)]
    for p, x in X:
        cnt[p - 1] += x

    dfs(0, -1, 0)
    print(cnt)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
par=[-1]*n
par[0]=0
chi=[[] for _ in range(n)]
ans=[0]*n
for i in range(n-1):
    a,b=map(int,input().split())
    if par[a-1]>-1:
        par[b-1]=a
        chi[a-1].append(b)
    elif par[b-1]>-1:
        par[a-1]=b
        chi[b-1].append(a)

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

que=deque([1])
while que:
    x=que.popleft()
    for i in chi[x-1]:
        ans[i-1]+=ans[x-1]
        que.append(i)
print(*ans)

Submission status: Wrong Answer

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]

n,q = LI()

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

f = [0]*n
for i in range(q):
	p,x = LI()
	p -= 1
	f[p] += x

def dfs(x,d,s):
	for y in v[x]:
		if d[y]:
			s += f[y]
			ans[y] = s
			d[y] = 0
			dfs(y,d,s)
			s -= f[y]

ans = [0]*n
d = [1]*n
d[0] = 0
ans[0] = f[0]
s = f[0]
dfs(0,d,s)
print(*ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
par=[-1]*n
par[0]=0
ans=[0]*n
for i in range(n-1):
    a,b=map(int,input().split())
    if par[a-1]>-1:
        par[b-1]=a
    elif par[b-1]>-1:
        par[a-1]=b

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

for i in range(1,n):
    ans[i]+=ans[par[i]-1]
print(*ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
par=[-1]*n
par[0]=0
ans=[0]*n
for i in range(n-1):
    a,b=map(int,input().split())
    if par[a-1]>-1:
        par[b-1]=a
    elif par[b-1]>-1:
        par[a-1]=b

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

for i in range(1,n):
    ans[i]+=ans[par[i]-1]
print(ans)

Submission status: Wrong Answer

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

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

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

#再帰dfs
def dfs(s):
  ans[s-1] += x
  for i in T[s-1]:
    dfs(i)

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

print(*ans)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque, defaultdict

    n, q, *abpx = map(int, open(0).read().split())

    g = defaultdict(list)
    for u, v in zip(*[iter(abpx[:2 * (n - 1)])] * 2):
        g[u].append(v)
        g[v].append(u)

    f = defaultdict(int)

    for p, x in zip(*[iter(abpx[2 * (n - 1):])] * 2):
        f[p] += x

    vis = [False for _ in range(n + 1)]

    q = deque([])
    q.append([1, 0])
    res = []
    while q:
        i, c = q.pop()
        if vis[i]:
            continue
        vis[i] = True
        ch = g[i]
        w = c + f[i]
        res.append(w)
        for u, v in zip(ch, [w] * len(ch)):
            q.appendleft([u, v])

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,Q=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(N-1)]
s=[list(map(int,input().split())) for i in range(Q)]
c=[0]*N
for i,j in s:
   c[i-1]+=j
for i,j in l:
   c[j-1]+=c[i-1]
print(*c)

Submission status: Wrong Answer

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

L = []
ans = []

def main():
    global L,ans,sums
    N,Q= map(int, input().split())
    ans = [0]*(N+1)

    L=[]
    [L.append([])for _ in range(N+1)]

    for _ in range(N-1):
        A,B = map(int, input().split())
        L[A].append(B)
#       L[B].append(A)


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

    dfs(1,0)

    #ansS = [str(n) for n in ans[1:len(ans)]]

    print(*ans[1:N+1])


def dfs(p,s):
    global L,ans
    ans[p] += s
    for i in range(len(L[p])):
        dfs(L[p][i],ans[p])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

L = []
sums = []
ans = []

def main():
    global L,ans,sums
    N,Q= map(int, input().split())
    ans = [0]*(N+1)
    sums = [0]*(N+1)

    L=[]
    [L.append([])for _ in range(N+1)]

    for _ in range(N-1):
        A,B = map(int, input().split())
        L[A].append(B)
#       L[B].append(A)


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

    dfs(1,0)

    #ansS = [str(n) for n in ans[1:len(ans)]]

    print(*ans[1:N+1])


def dfs(p,s):
    global L,ans,sums
    ans[p] = s + sums[p]
    for i in range(len(L[p])):
        dfs(L[p][i],ans[p])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

L = []
sums = []
ans = []

def main():
    global L,ans,sums
    N,Q= map(int, input().split())
    ans = [0]*(N+1)
    sums = [0]*(N+1)

    L=[]
    [L.append([])for _ in range(N+1)]

    for _ in range(N-1):
        A,B = map(int, input().split())
        L[A].append(B)
#       L[B].append(A)


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

    dfs(1,0)

    ansS = [str(n) for n in ans[1:len(ans)]]

    print(' '.join(ansS))


def dfs(p,s):
    global L,ans,sums
    ans[p] = s + sums[p]
    for i in range(len(L[p])):
        dfs(L[p][i],ans[p])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

L = []
ans = []

def main():
    global L,ans
    N,Q= map(int, input().split())
    ans = [0]*(N+1)

    L=[]
    [L.append([])for _ in range(N+1)]

    for _ in range(N-1):
        A,B = map(int, input().split())
        L[A].append(B)

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

    ansS = [str(n) for n in ans[1:len(ans)]]

    print(' '.join(ansS))


def dfs(p,x):
    global L,ans
    ans[p] += x
    for i in range(len(L[p])):
        dfs(L[p][i],x)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque 
N,Q=map(int, input().split())
G=[[] for _ in range(N)]
for i in range(N-1):
    a,b=map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
X=[0]*N
for i in range(Q):
    p,x=map(int, input().split())
    X[p-1]+=x
que=deque([(0,-1)])
print(que)
while que:
    now, pre=que.pop()
    for nex in G[now]:
        if nex==pre:
            continue
        else:
            X[nex]+=X[now]
            que.append((nex,now))

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

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
T=[[[],0,True] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    T[a-1][0].append(b-1)
    T[b-1][0].append(a-1)
for i in range(q):
    p,x=map(int,input().split())
    T[p-1][1]+=x
for t in T:
    for s in t[0]:
        if T[s][2]:
            T[s][1]+=t[1]
    t[2]=False
print(' '.join((map(lambda x:str(x[1]),T))))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
T=[[[],0] for _ in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    T[a-1][0].append(b-1)
for i in range(q):
    p,x=map(int,input().split())
    T[p-1][1]+=x
for t in T:
    for s in t[0]:
        T[s][1]+=t[1]
print(' '.join((map(lambda x:str(x[1]),T))))

Submission status: Wrong Answer

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

N,Q = map(int,input().split())
node = [[]for i in range(0,N+1,1)]
for i in range(0,N-1,1):
    a,b = map(int,input().split())
    node[a].append(b)
node_score = [0]*(N+1)#スコア
for i in range(0,Q,1):
    p,x = map(int,input().split())
    node_score[p] += x

ans = [0]*(N+1)
def bfs(n,preN):
    ans[n] = ans[preN]+node_score[n]
    for to in node[n]:
        if to != preN:
            bfs(to,n)
bfs(1,0)
print(" ".join(list(map(str,ans[1:N+1]))))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, q = map(int, input().split())
edge = sorted([set(map(lambda x: int(x)-1, input().split())) for _ in range(n-1)])
parent = [-1] * n
depth = [0] * n
for a, b in edge:
    if a != 0 and parent[b] != -1:
        parent[a] = b
        depth[a] = depth[b] + 1
    else:
        parent[b] = a
        depth[b] = depth[a] + 1
add = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    add[p-1] += x

order = sorted(list(zip(range(n), depth)), key=itemgetter(1))
ans = [0] * n
for i, _ in order:
    ans[i] = ans[parent[i]] + add[i]

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

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n, q = map(int, input().split())
parent = [-1] * n
depth = [0] * n
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    if a != 0 and parent[b] != -1:
        parent[a] = b
        depth[a] = depth[b] + 1
    else:
        parent[b] = a
        depth[b] = depth[a] + 1
add = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    add[p-1] += x

order = sorted(list(zip(range(n), depth)), key=itemgetter(1))
ans = [0] * n
for i, _ in order:
    ans[i] = ans[parent[i]] + add[i]

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