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(1000000)
def input():
    return sys.stdin.readline()
n, q = [int(i) for i in input().split()]
e = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = [int(i) - 1 for i in input().split()]
    e[a].append(b)
    e[b].append(a)

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


def dfs(i=0, r=-1):
    p[i] += p[r]
    for j in e[i]:
        if j == r:
            continue
        dfs(j, i)
dfs()

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード

oya=[-1]*N
for i in range(N-1):
  a,b=map(int, input().split())
  
  oya[b-1]=a-1
  
#adds=[0]*N



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

for i in range(N):
  if i >0:
    value[i]+=value[oya[i]]
    ans=value[i]
  else:
    ans=value[i]
  print(ans, end=" ")

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード

oya=[-1]*N
for i in range(N-1):
  a,b=map(int, input().split())
  
  oya[b-1]=a-1
  
#adds=[0]*N



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

for i in range(N):
  if oya[i] >=0:
    ans=value[i]+value[oya[i]]
  else:
    ans=value[i]
  print(ans, end=" ")

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード

oya=[-1]*N
for i in range(N-1):
  a,b=map(int, input().split())
  
  oya[b-1]=a-1
  
#adds=[0]*N

oya=np.array(oya)


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

for i in range(N):
  if oya[i] >=0:
    value[i]+=value[oya[i]]
  print(value[i], end=" ")

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Q=map(int, input().split())
value=np.array([0]*N)
#各ノードの下につくノード
road=[[] for _ in range(N)]
oya=[None]*N
for i in range(N-1):
  a,b=map(int, input().split())
  road[a-1].append(b-1)
  oya[b-1]=a-1
  
#adds=[0]*N

oya=np.array(oya)


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

for i in range(N):
  if oya[i] is not None:
    value[i]+=value[oya[i]]
  print(value[i], end=" ")

Submission status: Wrong Answer

Solution code:
import sys 
input = sys.stdin.readline
N, Q = list(map(int,input().split()))

from collections import defaultdict
tree = defaultdict(set)

for n in range(N-1):
  a,b = list(map(int,input().split()))
  tree[a] |= {b}

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

score = dict()


from queue import Queue
Q = Queue()
Q.put(1)
while not Q.empty():
  q = Q.get()
  for node in tree[q]:
    Q.put(node)
    weight[node] += weight[q]
  
for i in range(1,N+1):
  print(weight[i],end=" ")

Submission status: Wrong Answer

Solution code:
import sys 
input = sys.stdin.readline
N, Q = list(map(int,input().split()))

from collections import defaultdict
tree = defaultdict(set)

for n in range(N-1):
  a,b = list(map(int,input().split()))
  tree[a] |= {b}

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

score = dict()


from queue import Queue
Q = Queue()
Q.put(1)

while not Q.empty():
  q = Q.get()
  for node in tree[q]:
    Q.put(node)
    weight[node] += weight[q]
  
for i in range(1,N+1):
  print(weight[i],end=" ")

Submission status: Wrong Answer

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

table = [[False for i in range(n)] for j in range(n)]

for i in range(n-1):
    before, after = map(int, input().split())
    table[before-1][after-1] = True
    table[after-1][before-1] = True

from collections import defaultdict
scores = defaultdict(int)
for i in range(q):
    node, score = map(int, input().split())
    scores[node-1] = score

ans = defaultdict(int)
def dfs(frm, cur, score):
    score += scores[cur]
    ans[cur] += score

    for i in range(n):
        if table[cur][i] == True and i != frm:
            dfs(cur, i, score)

dfs(-1, 0, 0)

print(*ans.values())

Submission status: Wrong Answer

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

table = [[False for i in range(n)] for j in range(n)]

for i in range(n-1):
    before, after = map(int, input().split())
    table[before-1][after-1] = True

#print(table)

scores = [0 for i in range(n)]
for i in range(q):
    node, score = map(int, input().split())
    scores[node-1] += score

#print(scores)

ans = [0 for i in range(n)]
def dfs(scope, score):
    score += scores[scope]
    ans[scope] += score

    for i in range(n):
        if table[scope][i] == True:
            dfs(i, score)

dfs(0, 0)

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys 
input = sys.stdin.readline
N, Q = list(map(int,input().split()))

from collections import defaultdict
tree = defaultdict(set)

for n in range(N-1):
  a,b = list(map(int,input().split()))
  tree[a] |= {b}

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

for i in range(1,N+1):
  for node in tree[i]:
    weight[node] += weight[i]
  print(weight[i],end=" ")

Submission status: Wrong Answer

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

from collections import defaultdict
tree = defaultdict(set)

for n in range(N-1):
  a,b = list(map(int,input().split()))
  tree[a] |= {b}

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

for i in range(1,N+1):
  for node in tree[i]:
    weight[node] += weight[i]
  print(weight[i],end=" ")

Submission status: Wrong Answer

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

def cur(x):
    for i in neighbor[x]:
        ans[i]+=ans[x]
        cur(i)

n,q=map(int,input().split())
neighbor=[[]  for _ in range(n+1)]
parent=[-1]*(n+1)
ans=[0]*(n+1)
for _ in range(n-1):
    a,b=map(int,input().split())
    neighbor[a].append(b)

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

cur(1)
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
def sumsum(n,t,l):
  for s in range(n):
    for u in l[s]:
      t[u] = t[u] + t[s]


n,q = map(int,input().split())
t = [0 for _ in range(n)]
l = [[] for _ in range(n)]

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

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

sumsum(n,t,l)

for b in t:
  print(b,end = ' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

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

t = [0 for _ in range(n)]
l = [[] for _ in range(n)]

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

for j in range(q):
  p,x = map(int,input().split())
  t[p-1]= t[p-1] + x
  d = deque()
  
  for s in l[p-1]:  
    d.appendleft(s)
  
  while d:
    q = d.pop()
    t[q] = t[q] + x
    
    for k in l[q]:
      d.appendleft(k)
  

for b in t:
  print(b,end = ' ')

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
ab = sorted(ab)
points = [0]*(N+1)
for _ in range(Q):
  p, x = map(int,input().split())
  points[p] += x

for a, b in ab:
  points[b] += points[a]
  
print(*points[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque
n,q=map(int,input().split())
K=[set() for _ in range(n)]
V=[False]*n
P=[0]*n
for i in range(n-1):
      a,b=map(int,input().split())
      K[a-1].add(b-1)
      K[b-1].add(a-1)
for i in range(q):
      c,d=map(int,input().split())
      P[c-1]=d
que=deque([0])
while que:
      s=que.popleft()
      V[s]=True
      for ni in K[s]:
            if V[ni]==False:
                  P[ni]+=P[s]
                  que.append(ni)
print(*P)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 6)

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

e = [list(map(int, input().split())) for _ in range(n-1)]
cm = [list(map(int, input().split())) for _ in range(q)]
parent = [-1]*n
child = [[]]*n
counter = [0]*n

for i, v in cm:
  counter[i-1] += v

parent[0] = 0
for a, b in e:
  parent[b-1] = a-1
  child[a-1] = child[a-1] + [b-1]

#print(child)
cum_counter = [0]*n
def check(i, cum_cnt):
  cum_cnt += counter[i]
  cum_counter[i] = cum_cnt
  if len(child[i])>0:
    for c in child[i]:
      check(c, cum_cnt)

check(0, 0)
print(*cum_counter)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

deque = deque()

for i in range(q):
    p,x = map(int,input().split())
    deque.append(p-1)
    while len(deque) > 0:
        root = deque.popleft()
        cnt[root] += x
        deque.extend(L[root])

for i in range(n):
    print(cnt[i])

Submission status: Wrong Answer

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

mat = [[0]*N for j in range(N)]

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

for i in range(Q):
  p,x = map(int, input().split())
  cnt[p-1] += x
      
for i in range(N):
  for j, v in enumerate(mat[i]):
    if v == 1:
      cnt[j] += cnt[i]

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

Submission status: Wrong Answer

Solution code:
N,Q = (int(x) for x in input().split())

mat = [[0]*N for j in range(N)]

cnt = [0]*N
for i in range(N-1):
  a,b = (int(x) for x in input().split())
  mat[a-1][b-1] = 1

for i in range(Q):
  p,x = (int(x) for x in input().split())
  cnt[p-1] += x
      
for i in range(N):
  for j, v in enumerate(mat[i]):
    if v == 1:
      cnt[j] += cnt[i]

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

Submission status: Wrong Answer

Solution code:
N,Q = (int(x) for x in input().split())

mat = [[0 for i in range(N)] for j in range(N)]

cnt = [0]*N
for i in range(N-1):
  a,b = (int(x) for x in input().split())
  mat[a-1][b-1] = 1

for i in range(Q):
  p,x = (int(x) for x in input().split())
  cnt[p-1] += x
      
for i in range(N):
  for j, v in enumerate(mat[i]):
    if v == 1:
      cnt[j] += cnt[i]

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

Submission status: Wrong Answer

Solution code:
N,Q = (int(x) for x in input().split())

mat = [[0 for i in range(N)] for j in range(N)]
cnt = [0]*N
for i in range(N-1):
  a,b = (int(x) for x in input().split())
  mat[a-1][b-1] = 1

for i in range(Q):
  stack = []
  p,x = (int(x) for x in input().split())
  stack.append(p-1)
  cnt[p-1] += x
  while( len(stack) > 0 ):
    p = stack.pop()
    for j, v in enumerate(mat[p]):
      if v == 1:
        stack.append(j)
        cnt[j] += x
    
print(' '.join(map(str, cnt)))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc138/tasks/abc138_d
n, q = map(int, input().split())
counter = [0] * n
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)

def dfs(graph, counter, p, x):
    stack = [p]
    while stack:
        node = stack.pop()
        counter[node] += x
        for n in graph[node]:
            stack.append(n)

queries = [0] * n
for _ in range(q):
    # まとめて一括で更新することで、dfsの回数を減らす。
    p, x = map(int, input().split())
    p -= 1
    queries[p] += x

for p, x in enumerate(queries):
    dfs(graph, counter, p, x)

print(*counter)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
counter = [0] * n
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)

def dfs(graph, counter, p, x):
    stack = [p]
    while stack:
        node = stack.pop()
        counter[node] += x
        for n in graph[node]:
            stack.append(n)

queries = []
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    dfs(graph, counter, p, x)

print(*counter)

Submission status: Wrong Answer

Solution code:
from collections import deque
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)]
E = [[] for i in range(N+1)]
for i in range(N-1):
    E[ab[i][0]].append(ab[i][1])

d = [0]*(N+1)
for i in range(Q):
    d[px[i][0]]+=px[i][1]
def bfs(s):
    q = deque([s])
    while q:
        v = q.pop()
        for j in E[v]:
            q.append(j)
            d[j] += d[v]
            
bfs(1)
for i in range(1,N+1):
    print(d[i])

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)]
E = [[] for i in range(N+1)]
for i in range(N-1):
    E[ab[i][0]].append(ab[i][1])

d = [0]*(N+1)
def bfs(s,point):
    q = [s]
    d[s] += point
    while q:
        v = q.pop()
        for j in E[v]:
            q.append(j)
            d[j] += point
            
for i in range(Q):
    bfs(px[i][0],px[i][1])
print(d[1:])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

points = []
for _ in range(N):
    points.append([[], [], [0]])

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

for _ in range(Q):
    point, score = list(map(int, input().split()))
    points[point - 1][2][0] += score

# print(points)


def dfs(points, i, score):
    points[i][2][0] += score
    for next_point in points[i][1]:
        dfs(points, next_point, points[i][2][0])


dfs(points, 0, 0)
# print(points)
text = ''
for point in points:
    text += (' ' + str(point[2][0]))
print(text.strip(' '))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

points = []
counters = []
for _ in range(N):
    points.append([])
    counters.append(0)

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

operations = []
for _ in range(Q):
    operations.append(list(map(int, input().split())))

# print(points)
# print(operations)


def dfs(points, counters, i, count):
    counters[i - 1] += count
    for next_point in points[i - 1]:
        dfs(points, counters, next_point, count)


for operation in operations:
    dfs(points, counters, operation[0], operation[1])
# print(counters)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

points = []
counters = []
for _ in range(N):
    points.append([])
    counters.append(0)

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

operations = []
for _ in range(Q):
    operations.append(list(map(int, input().split())))

# print(points)
# print(operations)


def dfs(points, counters, i, count):
    counters[i - 1] += count
    for next_point in points[i - 1]:
        dfs(points, counters, next_point, count)


for operation in operations:
    dfs(points, counters, operation[0], operation[1])
print(counters)

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

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
N, Q = map(int,input().split())
G = [[0]*N for _ in range(N)]
for i in range(N-1) :
    a, b = map(int,input().split())
    G[a-1][b-1] = 1
p = []
x = []
for i in range(Q) :
    pi, xi = map(int,input().split())
    p.append(pi-1)
    x.append(xi)

ans = [0 for _ in range(N)]
for i in range(Q) :
    ans[p[i]] += x[i]

def dfs(n) :
    visited[n] = 1
    for i in range(N) :
        if G[n][i] == 1 :
            if visited[i] == 1 :
                continue
            ans[i] += ans[n]
            dfs(i)

visited = [0]*N
dfs(0)

print(*ans, sep=" ")

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(20000)
N, Q = list(map(int, input().split()))

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

operations = []
for _ in range(Q):
    operations.append(list(map(int, input().split())))

# print(points)
# print(operations)


def dfs(points, counters, i, count):
    counters[i - 1] += count
    for next_point in points[i - 1]:
        dfs(points, counters, next_point, count)


for operation in operations:
    dfs(points, counters, operation[0], operation[1])
# print(counters)

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, Q = map(int,input().split())
G = [[0]*N for _ in range(N)]
for i in range(N-1) :
    a, b = map(int,input().split())
    G[a-1][b-1] = 1
p = []
x = []
for i in range(Q) :
    pi, xi = map(int,input().split())
    p.append(pi-1)
    x.append(xi)

ans = [0 for _ in range(N)]
for i in range(Q) :
    ans[p[i]] += x[i]

def dfs(n) :
    for i in range(N) :
        if G[n][i] == 1 :
            ans[i] += ans[n]
            dfs(i)
    return True

dfs(0)

print(*ans, sep=" ")

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor
 
import numpy as np
 
if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
 
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
 
N, Q = list(map(int, sys.stdin.readline().split()))
AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)]
PX = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]
 
graph = [[] for _ in range(N + 1)]
for a, b in AB:
    graph[a].append(b)
 
counts = defaultdict(int)
for p, x in PX:
    counts[p] += x
 
ans = [0] * (N + 1)
ans[1] = counts[1]
que = deque([1])
while que:
    v = que.popleft()
    cnt = ans[v]
    for u in graph[v]:
        que.append(u)
        ans[u] = counts[u] + cnt
 
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
import sys
sys.setrecursionlimit(1000000)
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
4 3
1 2
2 3
2 4
2 10
1 100
3 1
'''

def solve(adj,n,add):
	n = len(adj)
	q = [0]
	res = [0]*n
	res[0] = add[0]
	while(q):
		src = q.pop(0)
		for nn in adj[src]:
			res[nn] += res[src] + add[nn]
			q.append(nn)
	return res


N,Q = list(map(int, stdin.readline().rstrip().split()))
adj = [[] for i in range(N)]
for _ in range(N-1):
	a,b = list(map(int, stdin.readline().rstrip().split()))
	a,b = a-1,b-1
	adj[min(a,b)].append(max(a,b))
add = [0]*N
for _ in range(Q):
	p,x = list(map(int, stdin.readline().rstrip().split()))
	p,x = p-1,x
	add[p] += x
ans = solve(adj,N,add)
print(*ans)

Submission status: Wrong Answer

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

def dfs(t: dict, i: int, counter: list, x: int) -> None:
    counter[i] += x

    if len(t[i]) == 0:
        return

    for _p in t[i]:
        dfs(t, _p, counter, x)

def main():
    n, q = map(int, input().split())
    t = {}
    counter = [0]*(n+1)
    for _ in range(n-1):
        a, b = map(int, input().split())
        t.setdefault(a, [])
        t.setdefault(b, [])

        t[a].append(b)

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

    counter.pop(0)
    print(*counter)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

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

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


def plus(n, val):
    ans[n] += val
    for i in info[n]:
        plus(i, val + Query[i])


Query = [0 for _ in range(N)]
for _ in range(Q):
    p, x = map(int, input().split())
    Query[p - 1] += x
plus(0, Query[0])
ans = list(map(str, ans))
print(" ".join(ans))

Submission status: Wrong Answer

Solution code:
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

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

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


def plus(n, val):
    ans[n] += val
    for i in info[n]:
        plus(i, val)


for _ in range(Q):
    p, x = map(int, input().split())
    plus(p - 1, x)
ans = list(map(str, ans))
print(" ".join(ans))

Submission status: Wrong Answer

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

def main():
    n,q=map(int,input().split())
    t=[[] for _ in range(n+1)]
    cnt=[0]*(n+1)
    for i in range(n-1):
        a,b=map(int,input().split())
        t[a].append(b)
    for i in range(q):
        p,x=map(int,input().split())
        cnt[p]+=x
    d=deque()
    d.append(1)
    while d:
        chk=d.popleft()
        for i in t[chk]:
            d.append(i)
            cnt[i]+=cnt[chk]
    print(' '.join(map(str,cnt[1:])))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

stack = [(1,0)]
ans = [0]*(N+1)
while stack:
    v,cnt = stack.pop()
    ans[v] = cnt+add[v]
    for edge in edges[v]:
        stack.append((edge,ans[v]))

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

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque
n,q=map(int,input().split())
g=[deque([]) for i in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
add=[0]*n
for _ in range(q):
    a,b=map(int,input().split())
    add[a-1]+=b
st=set()  ; ans=[0]*n ; stack=[[0,add[0]]]
while stack:
    s=stack[-1]
    if s[0] not in st : 
        st.add(s[0])
        ans[s[0]]+=s[1]
    else : stack.pop()
    while g[s[0]]:
        if g[s[0]][0] in st:
            g[s[0]].popleft()
        else:
            stack.append([g[s[0]][0],s[1]+add[g[s[0]][0]]])
            break
print(*ans)

Submission status: Wrong Answer

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

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

not_yet = deque(node[0])
while not_yet:
  key = not_yet.popleft()
  a, b = key[0], key[1]
  ans[b] += ans[a]
  for i in range(len(node[b])):
    not_yet.append(node[b][i])
  
print(*ans)

Submission status: Wrong Answer

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

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

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

print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

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

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

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

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

def dfs(parent,i):
    parents[i] = parent
    for k in edge[i]:
        if k != parent:
            dfs(i,k)

dfs(-1,1)

ans = cnt[1:]

print(*ans)

Submission status: Wrong Answer

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

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

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

notv = [True]*N
notv[0] = False
stack = [0]

while stack:
    now = stack.pop(0)
    for i in graph[now]:
        if notv[i]:
            notv[i] = False
            stack.append(i)
            v[i] += v[now]

print(*v)

Submission status: Wrong Answer

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

print(graph)

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

notv = [True]*N
notv[0] = False
stack = [0]

while stack:
    now = stack.pop(0)
    for i in graph[now]:
        if notv[i]:
            notv[i] = False
            stack.append(i)
            v[i] += v[now]

print(*v)

Submission status: Wrong Answer

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

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

# 隣接リスト
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, readline().split())
    # ここが違う
    edges[a-1].append(b)
    edges[b-1].append(a)

# スコア
proc = [0] * n
for i in range(q):
    p, x = map(int, readline().split())
    proc[p-1] += x

result = [0] * n

# 深さ優先探索


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(*result)

Submission status: Wrong Answer

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

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

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

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

result = [0] * n


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Wrong Answer

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

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

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

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

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


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

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

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

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

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


def dfs(edge, add_v):
    global result
    result[edge-1] += add_v
    for next_edge in edges[edge-1]:
        if result[next_edge-1] == 0:
            dfs(next_edge, add_v+proc[next_edge-1])


dfs(1, proc[0])
print(" ".join(map(str, result)))

Submission status: Wrong Answer

Solution code:
# 深さ優先探索の問題
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

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

# 隣接リスト
t = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = [int(i) for i in readline().split()]
    t[a-1].append(b-1)
    t[b-1].append(a-1)

# スコア
score = [0] * N
for _ in range(Q):
    p, x = [int(i) for i in readline().split()]
    #s[p-1] = s[p-1] + x
    score[p-1] += x

ans = [0] * N

# 深さ優先探索 (スタック)
rootchecker = [0] * N
rootchecker[0] = 1
stack = [0]
while stack:
    print(stack)
    s = stack.pop()
    ans[s] += score[s]
    for c in t[s]:
        if rootchecker[c] == 0:
            ans[c] += ans[s]
            rootchecker[c] = 1
            stack.append(c)

print(*ans)