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:
N,Q = map(int,input().split())
tree ={}
ans=[]
for i in range(1,N+1):
	tree[str(i)] = [i]
for i in range(N+1):
	ans.append(0)

for i in range(N-1):
	a,b = map(int,input().split())
	keys = [k for k, v in tree.items() if a in v ]
	for key in keys:
		tree[key].append(b)
	
for i in range(Q):
	p,x = map(int,input().split())
	for node in tree[str(p)]:
		ans[node] += x

answer=""
for i in range(1,N+1):
	answer += str(ans[i]) + " "

print(answer)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
tree ={}
ans={}
for i in range(1,N+1):
	tree[str(i)] = [i]
for i in range(1,N+1):
	ans[str(i)] = 0

for i in range(N-1):
	a,b = map(int,input().split())
	keys = [k for k, v in tree.items() if a in v ]
	for key in keys:
		tree[key].append(b)
	
for i in range(Q):
	p,x = map(int,input().split())
	for node in tree[str(p)]:
		ans[str(node)]+=x

answer=""
for i in range(1,N+1):
	answer+= str(ans[str(i)]) + " "

print(answer)

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    """
        assert get_answer(["4 3",
                       "1 2", "2 3", "2 4",
                       "2 10", "1 100", "3 1"]) == "100 110 111 110"
    
        1 100 100 100 100
        2   0  10  10  10
        3   0   0   1   0
    
    """
    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    q_index = {}
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]

    t_stack = [1]
    s_stack = [0]
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if i in q_index.keys():
            s += q_index[i]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)
    """

    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
            continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]
    """

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    """
        assert get_answer(["4 3",
                       "1 2", "2 3", "2 4",
                       "2 10", "1 100", "3 1"]) == "100 110 111 110"
    
        1 100 100 100 100
        2   0  10  10  10
        3   0   0   1   0
    
    """
    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    q_index = {}
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]

    t_stack = [1]
    s_stack = [0]
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if i in q_index.keys():
            s += q_index[i]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)
    """

    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
            continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]
    """

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    """
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))
    """
    s = []
    input_s = input().split()
    for i in range(0, len(input_s), 2):
        s.append(input_s[i] + " " + input_s[i + 1])

    print(get_answer(s))

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    """
        assert get_answer(["4 3",
                       "1 2", "2 3", "2 4",
                       "2 10", "1 100", "3 1"]) == "100 110 111 110"
    
        1 100 100 100 100
        2   0  10  10  10
        3   0   0   1   0
    
    """
    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    q_index = {}
    for qq in q_data:
        i = qq[0]
        if i in q_index.keys():
            q_index[i] += qq[1]
        else:
            q_index[i] = qq[1]

    t_stack = [1]
    s_stack = [0]
    while len(t_stack) > 0:
        i = t_stack.pop()
        s = s_stack.pop()
        if i in q_index.keys():
            s += q_index[i]
        score[i - 1] += s
        t_stack += t_index[i]
        for si in range(len(t_index[i])):
            s_stack.append(s)
    """

    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
            continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]
    """

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    """
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))
    """
    print(input().split())

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
from collections import defaultdict
N,Q = map(int,input().split())

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

adds = {}
ans = {}
for i in range(1,N+1):
    adds[i] = 0
    ans[i] = 0
for q in range(Q):
    p,x = map(int,input().split())
    adds[p] += x
# print(tree)
# print(adds)

def dig(i,tmp):
    tmp += adds[i]
    ans[i] = tmp
    for v in tree[i]: dig(v,tmp)

tmp = 0
dig(1,tmp)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
from collections import defaultdict
N,Q = map(int,input().split())

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

adds = {}
ans = {}
for i in range(1,N+1):
    adds[i] = 0
    ans[i] = 0
for q in range(Q):
    p,x = map(int,input().split())
    adds[p] += x

def dig(i,tmp):
    tmp += adds[i]
    ans[i] = tmp
    for v in tree[i]: dig(v,tmp)

tmp = 0
dig(1,tmp)

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

Submission status: Wrong Answer

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


def incre(edges, counter, e, x):
    counter[e] += x

    if edges[e] != 0:
        for n_e in edges[e]:
            incre(edges, counter, n_e, x)


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

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

for _ in range(n-1):
    tmp_e, tmp_ch = map(int, input().split())
    pars[tmp_ch] = tmp_e

q_e = []
q_x = []
for _ in range(q):
    tmp_e, x = map(int, input().split())
    counter[tmp_e] += x

for i in range(2, n+1):
    counter[i] += counter[pars[i]]

counter = counter[1:]
counter = [str(c) for c in counter]
print(' '.join(counter))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
#    for nn in n_data:
#        i = nn[0]
#        t_index[i].append(nn[1])

    t_stack = []
    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
            continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    t_stack = []
    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
            continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    t_stack = []
    for q in q_data:
        if q[0] == 1:
            for i in range(n):
                score[i -1] += q[1]
                continue

        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
pN = [0] * N
c = [0] * N
for i in range(N-1):
    a, b = map(int, input().split())
    pN[b-1] = a-1
for i in range(Q):
    p, x = map(int,input().split())
    c[p-1] += x
for i in range(1,N):
    c[i] += c[pN[i]]
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)]

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

res = [0]*n
for p,x in px:
    p = p-1
    res[p] += x

ans = [0]*n
ans[0] = res[0]
stack = [0]
visited = [0]*n
while stack:
    s = stack.pop()
    for t in tree[s]:
        if visited[t]:continue
        visited[t] = 1
        stack.append(t)
        ans[t] = ans[s] + res[t]
print(*ans)

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

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

res = [0]*n
for p,x in px:
    p = p-1
    res[p] += x

ans = [0]*n
ans[0] = res[0]
stack = [0]
visited = [0]*n
while stack:
    s = stack.pop()
    for t in tree[s]:
        if visited[t]:continue
        visited[t] = 1
        stack.append(t)
        ans[t] = ans[s] + res[t]
print(ans)

Submission status: Wrong Answer

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

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

graph = [[] for _ in range(N+1)]
counter = [0] * (N+1)

# 各頂点に隣接する頂点
for _ in range(N-1):
  a,b = map(int, input().split())
  graph[a].append(b)
  graph[b].append(a)

print(graph)
# 各頂点に点数を足す
for _ in range(Q):
  p,x = map(int, input().split())
  counter[p] += x

def shori(pa,c):
  counter[c] += counter[pa]
  for i in graph[c]:
    if i == pa:
      continue
    shori(c,i)
  
shori(0,1)
  

print(*counter[1:])

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
par = []
for i in range(n):
    par.append([])
ans = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    par[b-1].append(a-1)
for i in range(q):
    f,g = map(int,input().split())
    ans[f-1] += g
for i in range(n):
    if len(par[i]) == 0:
        None
    else:
        for j in range(len(par[i])):
            ans[i] += ans[par[i][j]]
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
n,q = map(int,input().split())
par = [0]*n
ans = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    par[b-1] = a-1
for i in range(q):
    f,g = map(int,input().split())
    ans[f-1] += g
for i in range(1,n):
    ans[i] += ans[par[i]]
print(' '.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))

N, Q = read_int()

p = []
x = []

papd = p.append
xapd = x.append

adj = defaultdict(list)
for i in range(N - 1):
    a, b = read_int()
    if a > b: a, b = b, a
    adj[a].append(b)


for i in range(Q):
    pi, xi = read_int()
    xapd(xi)
    papd(pi)

counter = [0 for _ in range(N + 5)]
ans = [0 for _ in range(N + 5)]

sys.setrecursionlimit(4100000)
def rec(i, n):
    """ i : index
        n : partial sum
    """
    ans[i] = n
    if len(adj[i]) == 0: return
    for next in adj[i]:
        rec(next, n + counter[next])



def solve():
    for i in range(Q):
        counter[p[i]] += x[i]
    rec(1, counter[1])

if __name__ == "__main__":
    solve()
    for i in range(1, N + 1):
        print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, Q = read_int()

p = []
x = []

papd = p.append
xapd = x.append

adj = defaultdict(list)
for i in range(N - 1):
    a, b = read_int()
    if a > b: a, b = b, a
    adj[a].append(b)
# adj = dict(adj)


for i in range(Q):
    pi, xi = read_int()
    xapd(xi)
    papd(pi)

counter = [0 for _ in range(N + 5)]
ans = [0 for _ in range(N + 5)]

sys.setrecursionlimit(4100000)
def rec(i, n):
    """ i : index
        n : partial sum
    """
    ans[i] = n
    if len(adj[i]) == 0: return
    for next in adj[i]:
        rec(next, n + counter[next])



def solve():
    for i in range(Q):
        counter[p[i]] += x[i]
    # print(adj, p, x)
    rec(1, counter[1])

if __name__ == "__main__":
    solve()
    for i in range(1, N + 1):
        if i == N:
            print(ans[i])
            continue
        print(ans[i], end=" ")

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
G = [[] for _ 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)
cnt = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    cnt[p-1] += x
    
flg = [False] * N
for i in range(N):
    flg[i] = True
    for v in G[i]:
        if flg[v]:
            pass
        else:
            cnt[v] += cnt[i]

print(*cnt)

Submission status: Wrong Answer

Solution code:
def main():
    n, q = map(int, input().split())
    counter = [0] * n
    c = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        c[a - 1].append(b - 1)
    p = []
    for _ in range(q):
        x, y = list(map(int, input().split()))
        p.append([x - 1, y])
    childs = [[i] for i in range(n)]
    for i in range(n):
        q = [i]
        while q != []:
            for j in c[q.pop()]:
                childs[i].append(j)
                q.append(j)
    for i in p:
        counter[i[0]] += i[1]
    sumCounter = [0] * n
    for index, i in enumerate(childs):
        for j in i:
            sumCounter[j] += counter[index]
    print(*sumCounter)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, q = map(int, input().split())
    counter = [0] * n
    c = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        c[a - 1].append(b - 1)
    p = []
    for _ in range(q):
        x, y = list(map(int, input().split()))
        p.append([x - 1, y])
    childs = [[i] for i in range(n)]
    for i in range(n):
        q = [i]
        while q != []:
            for j in c[q.pop()]:
                childs[i].append(j)
                q.append(j)
    for i in p:
        for j in childs[i[0]]:
            counter[j] += i[1]
    print(*counter)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
A = []
c = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
A.sort()
for j in range(Q):
   x = list(map(int,input().split()))
   c[x[0]-1] += x[1]
for a in A:
    c[a[1]-1] += c[a[0]-1]
C=""
for y in c:
    C += str(y)+" "
print(C)

Submission status: Wrong Answer

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

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

for i in range(n):
    for k in inf[i]:
        p_list[k] += p_list[i]

print(*p_list)

Submission status: Wrong Answer

Solution code:
N,Q = map(int,input().split())
A = []
c = [0]*N
for i in range(N-1):
    A.append(list(map(int,input().split())))
for j in range(Q):
   x = list(map(int,input().split()))
   c[x[0]-1] += x[1]
for a in A:
    c[a[1]-1] += c[a[0]-1]
C=""
for y in c:
    C += str(y)+" "
print(C)

Submission status: Wrong Answer

Solution code:
def get_answer(ki):
    n = int(ki[0].split(" ")[0])
    q = int(ki[0].split(" ")[1])
    n_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[1:-q]]]
    q_data = [[int(k[0]), int(k[1])] for k in [k.split(" ") for k in ki[-q:]]]
    score = [0] * n

    t_index = {}
    for i in range(1, n + 1):
        t_index[i] = []
    for nn in n_data:
        i = nn[0]
        t_index[i].append(nn[1])

    t_stack = []
    for q in q_data:
        t_stack = [q[0]]
        while len(t_stack) > 0:
            i = t_stack.pop()
            score[i - 1] += q[1]
            t_stack += t_index[i]

    s_value = str(score[0])
    for i in range(1, n):
        s_value += " " + str(score[i])
    return s_value


if __name__ == "__main__":
    input_ki = []
    while True:
        try:
            input1 = input()
            if not input1:
                break;
            if len(input1) == 0:
                break;
            input_ki.append(input1)
        except EOFError:
            break
    print(get_answer(input_ki))

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

child = [[] for _ in range(n)]
for a,b in ab:
    a -= 1
    b -= 1
    child[a].append(b)
# print(child)

p_val = [0] * n
for p,x in px:
    p_val[p-1] += x

ans = [p_val[0]]+[0]*(n-1)
stack = [0]
visited = [0] * n
while stack:
    s = stack.pop()
    visited[s] = 1
    for c in child[s]:
        if visited[c]:
            continue
        stack+=[c]
        ans[c] = ans[s]+p_val[c]
print(*ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

child = [[] for _ in range(n)]
for a,b in ab:
    a -= 1
    b -= 1
    child[a].append(b)
# print(child)

p_val = [0] * n
for p,x in px:
    p-=1
    p_val[p] += x

ans = [0]*n
for i,x in enumerate(p_val):
    visted = [0]*n
    ch = deepcopy(child[i])
    ans[i] += x
    # print(p,x)
    while ch:
        c = ch.pop()
        if visted[c]:continue
        ans[c] += x
        visted[c] = 1
        ch += child[c]
        # print("c,child[c]",c,child[c])
        # print(ch)
        # print(ans)
print(*ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

child = [[] for _ in range(n)]
for a,b in ab:
    a -= 1
    b -= 1
    child[a].append(b)
# print(child)

ans = [0]*n
for p,x in px:
    visted = [0]*n
    p -= 1
    ch = deepcopy(child[p])
    ans[p] += x
    # print(p,x)
    while ch:
        c = ch.pop()
        if visted[c]:continue
        ans[c] += x
        visted[c] = 1
        ch += child[c]
        # print("c,child[c]",c,child[c])
        # print(ch)
        # print(ans)
print(*ans)

Submission status: Wrong Answer

Solution code:
def rtrace(ptr):
  if cnti[ti[ptr]] == -1:
    rtrace(ti[ptr])
  else:
    cnti[ptr] = cnti[ti[ptr]] + vi[ptr]


tmp  = list(map(int, input().split()))
n    = tmp[0]
q    = tmp[1]

ti = [0 for i in range(n)]
for i in range(n-1):
  tmp  = list(map(int, input().split()))
  ti[tmp[1]-1] = tmp[0]-1

vi = [0 for i in range(n)]
for i in range(q):
  tmp = list(map(int, input().split()))
  vi[tmp[0]-1] += tmp[1]
  
cnti = [-1 for i in range(n)]
cnti[0] = vi[0]

for i in range(1,n):
  rtrace(i)

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

Submission status: Wrong Answer

Solution code:
def rtrace(ptr):
  if cnti[ti[ptr]] == -1:
    rtrace(ti[ptr])
  else:
    cnti[ptr] = cnti[ti[ptr]] + vi[ptr]


tmp  = list(map(int, input().split()))
n    = tmp[0]
q    = tmp[1]

ti = [0 for i in range(n)]
for i in range(n-1):
  tmp  = list(map(int, input().split()))
  ti[tmp[1]-1] = tmp[0]-1

vi = [0 for _ in range(n)]
for i in range(q):
  tmp = list(map(int, input().split()))
  vi[tmp[0]-1] += tmp[1]
  
cnti = [-1 for i in range(n)]
cnti[0] = vi[0]

for i in range(1,n):
  rtrace(i)

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

Submission status: Wrong Answer

Solution code:
tmp  = list(map(int, input().split()))
n    = tmp[0]
q    = tmp[1]

ti = sorted(input() for i in range(n-1))

vi = [0 for _ in range(n)]
for i in range(q):
  tmp = list(map(int, input().split()))
  vi[tmp[0]-1] += tmp[1]
  
cnti = [vi[i] for i in range(n)]

for i in range(n-1):
  tmp  = list(map(int, ti[i].split()))
  ai = tmp[0]
  bi = tmp[1]
  
  cnti[bi-1] += cnti[ai-1]


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

Submission status: Wrong Answer

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

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

for ind, num in enumerate(inf):
    for i in num:
        p_list[i] += p_list[ind]
print(*p_list)

Submission status: Wrong Answer

Solution code:
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n+1)]
p_list = [0 for _ in range(n+1)]

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

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

for ind, num in enumerate(inf):
    for i in num:
        p_list[i] += p_list[ind]

print(*p_list[1:])

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

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

for i in range(1, n+1):
  c[i] += c[ki[i]]

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

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
nodes = [0 for _ in range(N+1)]
for _ in range(N-1):
    a, b = map(int, input().split())
    nodes[b] = a

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

for i in range(1, N+1):
    counter[i] += counter[nodes[i]]

print(' '.join(map(str, counter[1:])))

Submission status: Wrong Answer

Solution code:
n, q = map(int, input().split())
count = [0 for _ in range(n)] # counter of each node
parent = [0 for _ in range(n)] # the parent of node(i)
for _ in range(n-1):
    a, b = map(int, input().split())
    parent[b-1] = a-1

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

for i in range(1, n):
    count[i] += count[parent[i]]

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

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, v):
        self.__value = v
        self.counter = 0
        self.children = []


tree_map = []
n, q = map(int, input().split())
for i in range(1, n + 1):
    tree_map.append(Node(i))

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

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

operations = [[tree_map[0], 0]]
while len(operations) > 0:
    node, v = operations.pop()
    node.counter += v
    for c in node.children:
        operations.append([c, node.counter])

counters = [str(tree_map[i].counter) for i in range(n)]
print(" ".join(counters))

Submission status: Wrong Answer

Solution code:
N, Q = map(int, input().split())
Tree_Data = [list(map(int, input().split())) for _ in range(N-1)]
Tree_Data.sort()
Operata_Data = [list(map(int, input().split())) for _ in range(Q)]

oyalist = [0] * (N+1)
for i in range(N-1):
    oyalist[Tree_Data[i][1]] = Tree_Data[i][0]

cnt = [0] * (N+1)
for i in range(Q):
    cnt[Operata_Data[i][0]] += Operata_Data[i][1]

ans = [0] * (N+1)
for i in range(1, N+1):
    ans[i] += (cnt[i] + ans[oyalist[i]])

ans = [str(i) for i in ans[1:N+1]]

print(" ".join(ans))