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 collections
N,Q = map(int,input().split())

Cnt = [0 for i in range(N+1)]
G = [[] for i in range(N+1)]
for i in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    #G[b].append(a) #親ノードの保存意味ある？
for i in range(Q):
    p,x = map(int,input().split())
    Cnt[p] += x
print(G)
q = collections.deque()
q.append(1)
flag = [False for i in range(N+1)]
while q:
    v = q.pop()
    flag[v] = True
    for i in G[v]:
        if flag[i]:
            continue
        Cnt[i] += Cnt[v]
        q.append(i)

print(*Cnt[1:])

Submission status: Wrong Answer

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


def dfs(tree, count, p):
    for i in tree[p]:
        count[i] += count[p]
        dfs(tree, count, i)


def main():
    N, Q = map(int, input().split())
    tree = {i+1: [] for i in range(N)}
    count = {i+1: 0 for i in range(N)}

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

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

    dfs(tree, count, 1)

    ans = [str(count[k]) for k in sorted(count.keys())]
    print(" ".join(ans))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i in range(1,N):
    Cnt[i] += Cnt[Par[i]-1]

for i in range(N):
    print(Cnt[i],end =' ')

Submission status: Wrong Answer

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

# 各頂点の親
ki = [0] * (n + 1)
for i in range(n - 1):
    a, b = map(int, input().split())
    ki[b] = a
# print("ki=",ki)

# カウンター
c = [0] * (n + 1)

# 各頂点に点数を足す
for i in range(q):
    p, x = map(int, input().split())
    c[p] += x
# print("c=",c)

# 各頂点の親の点数を足す
for i in range(1, n + 1):
    c[i] += c[ki[i]]
# print(c)

print(" ".join(map(str, c[1:])))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = []
answer_list = [0]
for i in range(n-1):
    tree_list.append(list(map(int,input().split())))
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(n): 
    for _ in tree_list:
        if i == _[0]-1:
            answer_list[_[1]-1] += answer_list[i]
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
tree_list  = []
answer_list = [0]
for i in range(n-1):
    tree_list.append(list(map(int,input().split())))
    answer_list.append(0)
for i in range(m):
    num1,num2 = map(int,input().split())
    answer_list[num1-1] +=  num2
for i in range(n):
    val = answer_list[i]
    for _ in tree_list:
        if i == _[0]-1:
            answer_list[_[1]-1] += val
for i in answer_list:
    print(i)

Submission status: Wrong Answer

Solution code:

def dfs(start,graph,score):
    stack = []
    stack.append(start)
    while len(stack) != 0:
        next = int(stack.pop())
        for num in graph[next-1]:
            stack.append(num)
            score[num-1] += score[next-1]

nq = str.split(input())
n = int(nq[0])
q = int(nq[1])
graph = []
parent = []
score = []
start = 0
end = 0
for i in range(n):
    graph.append([])
    parent.append([])
    score.append(0)
for i in range(n-1):
    startEnd = str.split(input())
    start = int(startEnd[0])
    end = int(startEnd[1])
    graph[start-1].append(end)
for i in range(q):
    xAmount = str.split(input())
    x = int(xAmount[0])
    amount = int(xAmount[1])
    score[x-1] += amount
dfs(1,graph,score)
for num in score:
    print(str(num)+' ',end='')

Submission status: Wrong Answer

Solution code:
def main():
    N, Q = map(int, input().split())
    E = [[] for i in range(N+1)]

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

    counter = [0] * (N+1)
    for i in range(Q):
        # 頂点p を根とする部分木に含まれるカウンターにxを足す
        p, x = map(int, input().split())
        counter[p] += x

    def mktree(v, parent, E):
        children = E[v]
        children.remove(parent)
        for child in children:
            mktree(child, v, E)

    #for child in E[1]: mktree(child, 1, E)

    def get(v, children, counter):
        c = counter[v]
        for child in children[v]:
            counter[child] += c
            get(child, children, counter)

    #get(1, E, counter)
    #print(" ".join(str(x) for x in counter[1:]))

main()

Submission status: Wrong Answer

Solution code:
import sys, collections

def solve():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    edge = [[] for i 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)

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

    q = collections.deque()
    q.append(0)
    while q:
        now = q.popleft()
        if not visited:
            visited = True
            cnow = counter[now]
            for next in edge[now]:
                if not visited[next]:
                    counter[next] += cnow
                    q.append(next)

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

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:

def dfs(start,graph,score,amount):
    stack = []
    stack.append(start)
    while len(stack) != 0:
        next = int(stack.pop())
        score[next-1] += amount
        for num in graph[next-1]:
            stack.append(num)

nq = str.split(input())
n = int(nq[0])
q = int(nq[1])
graph = []
score = []
start = 0
end = 0
for i in range(n):
    graph.append([])
    score.append(0)
for i in range(n-1):
    startEnd = str.split(input())
    start = int(startEnd[0])
    end = int(startEnd[1])
    graph[start-1].append(end)
for i in range(q):
    xAmount = str.split(input())
    x = int(xAmount[0])
    amount = int(xAmount[1])
    dfs(x,graph,score,amount)
for num in score:
    print(str(num)+' ',end='')

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int, input().split())
tree = [[int(i) for i in input().split()] for i in range(n-1)] 
px = [[int(i) for i in input().split()] for i in range(q)] 
A = [set() for i in range(n)]
B = [set() for i in range(n)]
C = [set() for i in range(n)]
flag = [False] * n

for i,a in enumerate(tree):
  A[a[1]-1].add(tree[i][0])
  B[a[0]-1].add(tree[i][1])

for i in range(n):
  C[i] = A[i] | B[i]

ans = [0] * n

for i in px:

  ans[i[0]-1] += i[1]
  
cr = deque()
cr.append(1)
flag[0] = True

while cr:
  c = cr.popleft()
  flag[c-1] = True
  for i in C[c-1]:
    if flag[i-1]:
      continue
    else:
      cr.append(i)
      ans[i-1] += ans[c-1]
ans[0] += 1
print(*ans)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n-1)]
px = [list(map(int, input().split())) for _ in range(q)]
dp = [0] * n
ls = [[] for i in range(n)]
for i in ab:
    ls[i[0]-1].append(i[1]-1)
for i in px:
    dp[i[0]-1] += i[1]
for i in range(n):
    for j in ls[i]:
        dp[j] += dp[i]
print(*dp)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n-1)]
px = [list(map(int, input().split())) for _ in range(q)]
al = [-1] * n
bl = [0] * n
for a, b in ab:
    al[b-1] = a - 1
for p, x in px:
    bl[p-1] += x
for i, v in enumerate(al):
    if i != 0:
        bl[i] += bl[v]
print(*bl)

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n-1)]
px = [list(map(int, input().split())) for _ in range(q)]
al = [-1] * n
bl = [0] * n
for a, b in ab:
    al[b-1] = a - 1
for p, x in px:
    bl[p-1] += x
for i, v in enumerate(al):
    if i != 0:
        bl[i] += bl[v]
print(bl)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10**6)

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

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

seen = [False] * (N + 1)
ans = [0] * (N + 1)


def dfs(e, v, x):
    seen[v] = True
    ans[v] += x
    for next_v in e[v]:
        if seen[next_v]:
            continue
        else:
            dfs(e, next_v, x + q[next_v])


dfs(e, 1, q[1])
print(*ans[1:])

Submission status: Wrong Answer

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

N,Q = map(int,input().split())
aLst = [[int(x) for x in input().split()] for _ in range(N-1)]
pLst = [[int(x) for x in input().split()] for _ in range(Q)]

tmpLst = [[] for _ in range(N+1)]
for i, j in aLst:
    tmpLst[i].append(j)

ansLst= [0] * N
for i in range(Q):
    ansLst[pLst[i][0] - 1] += pLst[i][1]
def dfs(v, ansLst):
    for i in tmpLst[v]:
        ansLst[i - 1] += ansLst[v - 1]
        dfs(i, ansLst)


dfs(1, ansLst)

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

Submission status: Wrong Answer

Solution code:
np = list(map(int, input().split(' ')))
n = np[0]
q = np[1]
p = []
a = []
cnt = []

mp = []
for i in range(n):
    cnt.append(0)
    mp.append([])
while(1):
    try:
        p.append(list(map(int, input().split(' '))))
    except EOFError:
        break

for i in range(len(p)-q):
    a.append(p.pop(0))

for e in a:
    mp[e[0]-1].append(e[1]-1)

for vv in p:
    c = []
    nd = vv[0] - 1
    v = vv[1]
    c.append(nd)
    h = mp[nd]
    while(len(h) != 0):
        for aa in h:
            c.append(aa)
        hh = h
        h = []
        for cc in hh:
            for ccc in mp[cc]:
                h.append(ccc)
    for x in c:
        cnt[x] += v
for i in range(n-1):
  	print(cnt[i], end = ' ')
print(cnt[n-1])

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
tr=[]
for i in range(n-1):
    tr.append(list(map(int,input().split())))
tr.sort()
a=[0]*n
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]+=c
for i in range(n-1):
    a[tr[i][1]-1]+=a[tr[i][0]-1]
print(*a)

Submission status: Wrong Answer

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

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)]
ab.sort()

c = [0]*n

for j in range(q):
    c[px[j][0]-1] += px[j][1]
    
for i in range(n-1):
    c[ab[i][1]-1] += c[ab[i][0]-1]
    
print(*c)

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)]
ab.sort()

c = [0]*n

for j in range(q):
    c[px[j][0]-1] += px[j][1]
    
for i in range(n-1):
    c[ab[i][1]-1] += c[ab[i][0]-1]
    
print(*c)

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

c = [0]*n

for j in range(q):
    c[px[j][0]-1] += px[j][1]
    
for i in range(n-1):
    c[ab[i][1]-1] += c[ab[i][0]-1]
    
print(*c)

Submission status: Wrong Answer

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

s = [0]*(n+1)
t = [0]*(n+1)

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

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

for i in range(1, n+1):
    t[i] += t[s[i]]

print(*t[1:])

Submission status: Wrong Answer

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

s = [0]*(n+1)
t = [0]*(n+1)

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

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

for i in range(len(s)):
    t[i] += t[s[i]]

print(*t[1:])

Submission status: Wrong Answer

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

s = [0]*(n+1)
t = [0]*(n+1)

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

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

for i in range(len(s)):
    t[i] += t[s[i]]

print(*t)

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
vec = [set() for _ in range(n)]
counter = [0 for _ in range(n)]
def count(lis,p):
    l = [p]
    set = {p}
    while l:
        m = []
        for t in l:
            in_s = lis[t] - set
            in_l = list(in_s)
            m += in_l
            set = lis[t] | set
        l = m
    return set
for _ in range(n-1):
    a,b = map(int,input().split())
    vec[a-1].add(b-1)
for i in range(q):
    p,x = map (int,input().split())
    s = count(vec,p-1)
    for j in range(n):
        if j in s:
            counter[j] += x
for k in range(n):
    print(counter[k],end = ' ')

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
table = [[0 for i in range(N)] for j in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    table[a-1][b-1] = 1
for i in range(Q):
    p, x = map(int, input().split())
    table[p-1][p-1] += x
    stack = []
    for i in range(p-1, N):
        if table[p-1][i] != 0 and i != p-1:
            # table[p-1][i] += x
            stack.append(i)
    while len(stack) > 0:
        c = stack[0]
        table[c][c] += x
        for i in range(c, N):
            if table[c][i] != 0 and i != c:
                # table[c][i] += x
                stack.append(i)
        del stack[0]

for i in range(N):
    print(table[i][i], end=' ')

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
graph = dict()
for i in range(1,n+1):
    graph[i] = []
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a].append(b)

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

q = [1]
while len(q) > 0:
    nextcell = graph[q[0]]
    for i in range(len(nextcell)):
        c[nextcell[i]] += c[q[0]]
        q.append(nextcell[i])
    q.pop(0)
for i in range(1,n+1):
    if i != n:
        print(c[i],end= " ")
    else:
        print(c[i])

Submission status: Wrong Answer

Solution code:
def solve(n, q, a, b, p, x): 
    E = {i: [] for i in range(1, n+1)}
    for a_i, b_i in zip(a, b): 
        print(a_i, b_i)
        E[a_i].append(b_i)
        E[b_i].append(a_i)
    res = {i: 0 for i in range(1, n+1)}
    for j in range(q):
        res[p[j]] += x[j]
    used = [False] * (n+1)
    que = [1] 
    while que:
        u = que.pop(0)
        used[u] = True
        for v in E[u]:
            if not used[v]:
                used[v] = True
                res[v] += res[u]
                que.append(v)
    vs = [v for k, v in sorted(res.items())]
    return " ".join(map(str, vs))

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

Submission status: Wrong Answer

Solution code:
import collections
N, Q = map(int, input().split())
T = [[] for _ in range(N + 1)]
counts = [0] * (N + 1)

for _ in range(N-1):
  a, b = map(int, input().split())
  T[a].append(b)
  T[b].append(a)
for _ in range(Q):
  p, x = map(int, input().split())
  counts[p] = x
visited = [0] * (N + 1)
queue = collections.deque()
queue.append(1)
while queue:
  v = queue.pop()
  visited[v] = 1
  x = counts[v]
  for child in T[v]:
    if visited[child] == 1:
      continue
    counts[child] += x
    queue.append(child)
print(*counts[1:])

Submission status: Wrong Answer

Solution code:
nq=list(map(int,input().split()))
CountPj=[]
for i in range(nq[0]):
    CountPj.append(0)
ABi=[]
PXj=[]
for i in range(nq[0]-1):
    ab=list(map(int,input().split()))
    ABi.append(ab)
for i in range(nq[1]):
    px=list(map(int,input().split()))
    PXj.append(px)

def f(j):
    l_list=[]
    for i in range(1,nq[0]):
        if (PXj[j-1])[0]==(ABi[i-1])[0]:
            for l in range(1,nq[1]+1):
                if l==j:
                    l=l
                else:
                    if (ABi[i-1])[1]==(PXj[l-1])[0]:
                        l_list.append(l)
    return l_list


for j in range(1,nq[1]+1):
    lst=[j]
    a=len(lst)
    xj=PXj[j-1][1]
    while a>=1:
        lst2=[]
        for i in range(a):
            b=f(lst[i])
            lst2=lst2+b
            c=len(b)
            for l in range(c):
                CountPj[b[l]]=CountPj[b[l]]+xj
        lst=lst2
        a=len(lst2)

print(*CountPj)

Submission status: Wrong Answer

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

# 遅延評価で加えてあげるだけ

N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)

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

def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        dfs(x,v,value[v])

dfs(1,0,0)

answer = ' '.join(map(str,value[1:]))
print(answer)

Submission status: Wrong Answer

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

# 遅延評価で加えてあげるだけ

N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)


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

def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

answer = ' '.join(map(str,value[1:]))
print(answer)

Submission status: Wrong Answer

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

point = [0] * n

for i in range(q):
  point[p[i][0]-1] += p[i][1]

for j in range(n-1):
  point[e[j][1]-1] += point[e[j][0]-1]
  
for h in range(n-1):
    print(point[h], end = ' ')
print(point[n-1], end = '')

Submission status: Wrong Answer

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

point = [0] * n

for i in range(q):
  point[p[i][0]-1] += p[i][1]

for j in range(n-1):
  point[e[j][1]-1] += point[e[j][0]-1]
  
for h in range(n):
    print(point[h], end = ' ')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
ab = [list(map(int,input().split(" "))) for _ in range(n-1)]
ab.sort()
px = [list(map(int,input().split(" "))) for _ in range(q)]
z = [0]*n
for k in range(q):
  z[px[k][0]-1] += px[k][1]
w = z[:]
for l in range(n-1):
  if type(z[ab[l][0]-1]) == float or ab[l][0] == 1:
    z[ab[l][1]-1] += z[ab[l][0]-1] + 0.0
  else:
    z[ab[l][0]-1] += z[ab[l][1]-1] + 0.0
print(*list(map(int,z)))

Submission status: Wrong Answer

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

point = [0] * n

for i in range(q):
  start_place = p[i][0]
  point[start_place-1] += p[i][1]

for j in range(n-1):
  point[e[j][1]-1] += point[e[j][0]-1]
  
for h in range(n):
    print(point[h], end = ' ')

Submission status: Wrong Answer

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

point = [0] * n

for i in range(q):
  point_temp = [0] * n
  start_place = p[i][0]
  point_temp[start_place-1] += p[i][1]
  for j in range(n-1):
    point_temp[e[j][1]-1] += point_temp[e[j][0]-1]
  for j in range(n):
    point[j] += point_temp[j]
  
for h in range(n):
    print(point[h], end = ' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = list(map(int, input().split()))
edges = {n:[] for n in range(N+1)}
nodes = [0 for _ in range(N+1)]
cr = deque()

# 経路
for n in range(N-1):
    a, b = list(map(int, input().split()))
    edges[a].append(b)
    edges[b].append(a)

# 各ノードの点数
for q in range(Q):
    p, x = list(map(int, input().split()))
    nodes[p] += x

# 各ノードを通ったことを覚えておく
flags = [False]*(N+1)

# ルートから探索
cr.append(1)
flags[1] = True

# ルートから葉っぱまで点数を行き渡らせる
while cr:
    c = cr.popleft()
    flags[c] = True

    # 続く葉っぱがない、または、点数が0なら飛ばす
    if not edges[c] or not nodes[c]:
        continue

    # 幹に点数を与え、幹を伸ばす
    pts = nodes[c]
    for trunk in edges[c]:
        if not flags[trunk]:
            flags[trunk] = True
            nodes[trunk] += pts
            cr.append(trunk)

print(*nodes[1:])

Submission status: Wrong Answer

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

point = [0] * n
#print(e)
#print(e[2][1])
#print(p)
#print(point)

for i in range(q):
  start_place = p[i][0]
  point[start_place-1] += p[i][1]
#  print(point)
  for j in range(n-1):
    if e[j][0] == start_place:
#      print(e[j][1])
      point[e[j][1]-1] += p[i][1]
#      print(point)
      for k in range(n-1):
        if e[k][0] == e[j][1]:
          point[e[k][1]-1] += p[i][1]

for h in range(n):
    print(point[h], end = ' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = list(map(int, input().split()))
edges = {n+1:[] for n in range(N)}
nodes = [0 for _ in range(N+1)]
cr = deque()

# 経路
for n in range(N-1):
    a, b = list(map(int, input().split()))
    edges[a].append(b)

# 各ノードの点数
for q in range(Q):
    p, x = list(map(int, input().split()))
    nodes[p] += x

# 各ノードを通ったことを覚えておく
flags = [False]*(N+1)

# ルートから探索
cr.append(1)
flags[1] = True

# ルートから葉っぱまで点数を行き渡らせる
while cr:
    c = cr.popleft()
    flags[c] = True

    # 続く葉っぱがない、または、点数が0なら飛ばす
    if not edges[c] or not nodes[c]:
        continue

    # 幹に点数を与え、幹を伸ばす
    pts = nodes[c]
    for trunk in edges[c]:
        if not flags[trunk]:
            flags[trunk] = True
            nodes[trunk] += pts
            cr.append(trunk)

print(*nodes[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = list(map(int, input().split()))
edges = {n+1:[] for n in range(N)}
nodes = [0 for _ in range(N+1)]
cr = deque()

# 経路
for n in range(N-1):
    a, b = list(map(int, input().split()))
    edges[a].append(b)

# 各ノードの点数
for q in range(Q):
    p, x = list(map(int, input().split()))
    nodes[p] += x

# 各ノードを通ったことを覚えておく
flags = [False]*(N+1)

# ルートから探索
cr.append(1)
flags[1] = True

# ルートから葉っぱまで点数を行き渡らせる
while cr:
    c = cr.popleft()
    flags[c] = True

    # 続く葉っぱがない、または、点数が0なら飛ばす
    if not edges[c] or not nodes[c]:
        continue

    # 幹に点数を与える
    pts = nodes[c]
    for trunk in edges[c]:
        flags[trunk] = True
        nodes[trunk] += pts

    # 次の幹
    cr.extend(edges[c])

print(*nodes[1:])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
sco = [0]*(n+1)
edge = [[i] for i in range(n+1)]

def dfs(i,s):
    E = edge[i]
    if(len(E)==1):
        return 0
    for e in E:
        if(e==i):continue
        sco[e]+=s
        dfs(e,s)

for i in range(n-1):
    a,b = map(int,input().split())
    edge[b].append(a)
#print(edge)
for i in range(q):
    p,x=map(int,input().split())
    sco[p]+=x
for i in range(1,n+1):
    E=edge[i]
    for e in E:
        if(e==i):continue
        sco[i]+=sco[e]
print(*sco[1:])

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = list(map(int, input().split()))
edges = {n+1:[] for n in range(N)}
nodes = [0 for _ in range(N+1)]
cr = deque()

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

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

for n in range(N, 0, -1):
    cr.clear()

    if len(edges[n]) == 0:
        continue
    cr.extend(edges[n])

    x = nodes[n]

    if x == 0:
        continue

    while len(cr):
        c = cr.popleft()
        nodes[c] += x
        cr.extend(edges[c])

print(*nodes[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

tree = defaultdict(lambda :list([]))

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

counter = [0]*(n+1)
lazy = [0]*(n+1)

def dfs(src, end):
	vis_set = {src, }
	stack = [src]
	while stack:
		v = stack.pop()
		counter[v] += lazy[v] 
		for next_v in tree[v]:
			if not next_v in vis_set:
				lazy[next_v] += lazy[v]
				vis_set.add(next_v)
				stack.append(next_v)
		lazy[v] = 0

for query in range(q):
	node, inc = map(int, input().split()) 
	lazy[node] += inc
dfs(1, n)
for idx, v in enumerate(lazy):
	counter[idx] += v
print(*counter[1:])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
ab = [list(map(int,input().split(" "))) for _ in range(n-1)]
ab.sort()
px = [list(map(int,input().split(" "))) for _ in range(q)]  
z = [0]*n
for k in range(q):
  z[px[k][0]-1] += px[k][1]
w = z[:]
for l in range(n-1):
  if z[ab[l][0]-1] > w[ab[l][0]-1] or ab[l][0] == 1:
    z[ab[l][1]-1] += z[ab[l][0]-1]
  else:
    z[ab[l][0]-1] += z[ab[l][1]-1]
print(*z)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import numpy


class Node(object):
    def __init__(self):
        self.counter = 0
        self.parent = None
        self.memo = None

    def value(self):
        if self.parent is None:
            return self.counter
        else:
            if self.memo is None:
                self.memo = self.parent.value() + self.counter
            return self.memo


N, Q = map(int, sys.stdin.readline().rstrip().split(" "))
nodes = [Node() for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, sys.stdin.readline().rstrip().split(" "))
    nodes[b - 1].parent = nodes[a - 1]
for _ in range(Q):
    p, x = map(int, sys.stdin.readline().rstrip().split(" "))
    nodes[p - 1].counter += x

print(" ".join(str(node.value()) for node in nodes))
exit(0)

Submission status: Wrong Answer

Solution code:
from collections import deque

n,q = map(int, input().split())
tree = [[int(i) for i in input().split()] for i in range(n-1)] 
px = [[int(i) for i in input().split()] for i in range(q)] 
calc_tree = [[] for i in range(n)]
ans = [0] * n
ans2 = [0] * n
flag = [1] * n

for i in range(n-1):
  if flag[tree[i][1]-1]:
    calc_tree[tree[i][0]-1].append(tree[i][1]-1)
    flag[tree[i][1]-1] = 0
  elif flag[tree[i][0]-1]:
    calc_tree[tree[i][1]-1].append(tree[i][0]-1)
    flag[tree[i][0]-1] = 0
  else:
    calc_tree[tree[i][1]-1].append(tree[i][0]-1)

for i in px:
  ans[i[0]-1] += i[1]

yet = deque()
yet.append(flag.index(1))

while yet:
  ans2[yet[0]] = ans[yet[0]]
  for i in calc_tree[yet[0]]:
    #print(i)
    ans[i] += ans[yet[0]]
    yet.append(i)
  #print(yet,'pop前')
  yet.popleft()
  #print(yet)
  
for i in ans2:
  print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, Q = list(map(int, input().split()))
edges = {n+1:[] for n in range(N)}
nodes = [0 for _ in range(N+1)]
cr = deque()

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

for q in range(Q):
    p, x = list(map(int, input().split()))
    cr.append(p)
    while len(cr):
        c = cr.popleft()
        nodes[c] += x
        cr.extend(edges[c])

print(*nodes[1:])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

to=[[] for i in range(n)]
ans=np.zeros(n)

for i in range(n-1):
  y,z=map(int,input().split())
  y=y-1
  z=z-1
  to[y].append(z)
  
for i in range(q):
  y,z=map(int,input().split())
  y=y-1
  ans[y]=ans[y]+z

for i in range(n):
  for j in to[i]:
    ans[j]=ans[j]+ans[i]

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

Submission status: Wrong Answer

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

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

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

ans=[0 for i in range(n)]
for i in range(n):
    tmp=i
    flg=1
    cntans=0
    while(flg):
        cntans=cntans+cnt[tmp]
        if tmp==0:
            flg=0
        tmp=l[tmp]
    ans[i]=cntans
for i in range(n):
    print(ans[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)]
ab.sort()
p = [0]*n
for i in range(q):
  a, b = map(int, input().split())
  p[a - 1] += b
s = ""
for i in ab:
  p[i[1]-1] += p[i[0]-1]
print(*p)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
AB = [[0]*2 for i in range(N-1)]
for i in range(N-1):
  AB[i] = list(map(int, input().split()))
C = [0]*N
AB.sort()

for i in range(Q):
  p, x = map(int, input().split())
  C[p-1] += x
s = ""
for i,j in AB:
  C[j-1] += C[i-1]
  
print(*C)

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
AB = [[0]*2 for i in range(N-1)]
for i in range(N-1):
  AB[i] = list(map(int, input().split()))
C = [0]*N
AB.sort()

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

for i,j in AB:
  C[j-1] += C[i-1]
  
print(*C)