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
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    edges = [None]*N
    edges[0] = 0
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1] = a-1

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

    print(counts[0], end=' ')
    for i in range(1, N):
        if edges[i] != None:
            counts[i] += counts[edges[i]]
            print(counts[i], end=' ')
        else:
            print(counts[0], end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

n, q = map(int, input().split())
tree = [[] for _ in range(n+1)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
adds = [0]*(n+1)
for _ in range(q):
    p, x = map(int, input().split())
    adds[p] += x

res = [0]*(n+1)
def add_x(p, x):
    res[p] += x
    for child in tree[p]:
        new_x = x+adds[child]
        add_x(child, new_x)

add_x(1, adds[1])

print(*res[1:])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    edges = [None]*N
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1] = a-1

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

    for i in range(N):
        if edges[i] != None:
            counts[i] += counts[edges[i]]
            print(counts[i], end=' ')
        else:
            print(counts[i], end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

br = []

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

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

ans = ""
for i in range(len(tree)):
  ans+=str(tree[i])
  ans+=" "
  
  
print(ans)

Submission status: Wrong Answer

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

br = []

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

for i in range(q):
  p,x = map(int,input().split())
  tree[p-1]+=x
  
for i in range(len(br)):
  tree[br[i][1]-1]+=tree[br[i][0]-1]
  
print(tree)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    edges = [0]*N
    edges[0] = 0
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1] = a-1

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

    print(counts[0], end=' ')
    for i in range(1, N):
        counts[i] += counts[edges[i]]
        print(counts[i], end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def input():
    return sys.stdin.readline()[:-1]

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

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

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

# print(edge)
# print(counter)

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

def dfs(fro, p):
    visited[fro] = True
    now = counter[fro] + p
    for to in edge[fro]:
        if not visited[to]:
            dfs(to, now)
    ans[fro] = now
    return

dfs(1, 0)

print(*ans[1:])

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)

if 'LOCAL' in os.environ:
    N = 2* 10** 5
    Q = 2 * 10 ** 5
    AB = [[i, i + 1] for i in range(1, N)]
    PX = [[i, 10] for i in range(Q)]
else:
    N, Q = LI()
    AB = []
    PX = []
    for i in range(N-1):
        #  AB.append(LI())
        AB.append(sorted(LI()))

    for i in range(Q):
        PX.append(LI())


"""
そもそも各頂点の部分木をdictに入れましょうか？
参照するような形で。
2には3
3には4,5
4,5はNoneだと、参照しやすいか？

カウントを若い順にやっていく
"""
AB = sorted(AB, key=lambda x: x[0])
perr('AB')
perr(AB)
d = {}
for i in range(N+1):
    d[i] = 0
for p, x in PX:
    if p not in d.keys():
        d[p] = 0
    d[p] += x

"""
根からスタートするとは？
1-5-3とかっていう組み合わせがあるため、dfsで根から辿っていく必要がある

"""
ans = [0 for _ in range(N)]
ans[0] = d[1]

lines_dict = {}
for ab in AB:
    if ab[0] not in lines_dict.keys():
        lines_dict[ab[0]] = [ab[1]]
        continue
    lines_dict[ab[0]].append(ab[1])
perr(lines_dict)

def dfs(current_node, val, done_list):
    #  target_lines = [ab for ab in AB if current_node in ab and ab[0] not in done_list and ab[1] not in done_list]
    if current_node not in lines_dict.keys():
        return
    target_lines = [num for num in lines_dict[current_node] if num not in done_list]
    done_list.append(current_node)
    for to_node in target_lines:
        perr(to_node)
        ans[to_node-1] = val + d[to_node]
        dfs(to_node, ans[to_node-1], done_list)


dfs(1, d[1], [])
perr(ans)
for a in ans:
    print(a, end=' ')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    edges = [None]*N
    edges[0] = 0
    for _ in range(N-1):
        a, b = map(int, input().split())
        edges[b-1] = a-1

    counts = [0]*N
    for _ in range(Q):
        p, x = map(int, input().split())
        counts[p-1] += x
    return
    print(counts[0], end=' ')
    for i in range(1, N):
        counts[i] += counts[edges[i]]
        print(counts[i], end=' ')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
 
n,q=input().split()
n=int(n)
q=int(q)
l=[[] for i in range(n)]
def count(p,x,l,counter):
    counter[p-1]+=x
    for i in l[p-1]:
        counter[i-1]+=x
        if l[i-1]:
            count2(i,x,l,counter)
def count2(p,x,l,counter):
    for i in l[p-1]:
        counter[i-1]+=x
        if l[i-1]:
            count2(i,x,l,counter)
for i in range(n-1):
    a,b=input().split()
    a=int(a)
    b=int(b)
    l[a-1].append(b)
counter=[0 for i in range(n)]
lis=[0 for i in  range(n)]
for i in range(q):
    p,x=input().split()
    p=int(p)
    x=int(x)
    lis[p-1]+=x
of=0
for i in lis:
    of+=1
    count(of,i,l,counter)
for i in counter:
    print(i,end=" ")
print()