Buggy python submission (verdict: Time Limit Exceeded):
import sys

N,Q = [int(i) for i in sys.stdin.readline().split()]
D=[10**10]*N
D[0]=0
A=[[0]*3 for i in range(N-1)]
Ans=[0]*N
P=[[0]*2 for i in range(Q)]

for i in range(N-1):
    A[i][0],A[i][1] = [int(i) for i in sys.stdin.readline().split()]
    if(A[i][0]==1):
        D[A[i][1]-1]=1
    else:
        D[A[i][1]-1]=D[A[i][0]-1]+1
    A[i][2]=D[A[i][1]-1]
while(max(D)>(10**10)):
    for i in range(N-2,-1,-1):
        if(D[i]>(10**10)):
            D[A[i][1]-1]=D[A[i][0]-1]+1
        A[i][2]=D[A[i][1]-1]

for i in range(Q):
    P[i][0],P[i][1] = [int(i) for i in sys.stdin.readline().split()]
    Ans[P[i][0]-1]+=P[i][1]
A=sorted(A,key=lambda x: x[2])

for i in range (N-1):
    Ans[A[i][1]-1]+=Ans[A[i][0]-1]
print(*Ans)


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys

N,Q = [int(i) for i in sys.stdin.readline().split()]
D=[10**10]*N
D[0]=0
A=[[0]*3 for i in range(N-1)]
Ans=[0]*N
P=[[0]*2 for i in range(Q)]

for i in range(N-1):
    A[i][0],A[i][1] = [int(i) for i in sys.stdin.readline().split()]
    if(A[i][0]==1):
        D[A[i][1]-1]=1
    else:
        D[A[i][1]-1]=D[A[i][0]-1]+1
    A[i][2]=D[A[i][1]-1]
while(max(D)>(10**10)):
    for i in range(N-1):
        if(D[i]>(10**10)):
            D[A[i][1]-1]=D[A[i][0]-1]+1
        A[i][2]=D[A[i][1]-1]

for i in range(Q):
    P[i][0],P[i][1] = [int(i) for i in sys.stdin.readline().split()]
    Ans[P[i][0]-1]+=P[i][1]
A=sorted(A,key=lambda x: x[2])

for i in range (N-1):
    Ans[A[i][1]-1]+=Ans[A[i][0]-1]
print(*Ans)


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
D=[10**10]*N
D[0]=0
A=[[0]*3 for i in range(N-1)]
Ans=[0]*N
P=[[0]*2 for i in range(Q)]

for i in range(N-1):
    A[i][0],A[i][1] = map(int, input().split())
    if(A[i][0]==1):
        D[A[i][1]-1]=1
    else:
        D[A[i][1]-1]=D[A[i][0]-1]+1
    A[i][2]=D[A[i][1]-1]
while(max(D)>(10**10)):
    for i in range(N-1):
        if(D[i]>(10**10)):
            D[A[i][1]-1]=D[A[i][0]-1]+1
        A[i][2]=D[A[i][1]-1]

for i in range(Q):
    P[i][0],P[i][1] = map(int, input().split())
    Ans[P[i][0]-1]+=P[i][1]
A=sorted(A,key=lambda x: x[2])

for i in range (N-1):
    Ans[A[i][1]-1]+=Ans[A[i][0]-1]
print(*Ans)


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
D=[10**10]*N
D[0]=0
A=[[0]*3 for i in range(N-1)]
Ans=[0]*N
P=[[0]*2 for i in range(Q)]

for i in range(N-1):
    A[i][0],A[i][1] = map(int, input().split())
    if(A[i][0]==1):
        D[A[i][1]-1]=1
    else:
        D[A[i][1]-1]=D[A[i][0]-1]+1
    A[i][2]=D[A[i][1]-1]
while(max(D)>(10**10)):
    for i in range(N-1):
        if(A[i][0]==1):
            D[A[i][1]-1]=1
        else:
            D[A[i][1]-1]=D[A[i][0]-1]+1
        A[i][2]=D[A[i][1]-1]

for i in range(Q):
    P[i][0],P[i][1] = map(int, input().split())
    Ans[P[i][0]-1]+=P[i][1]
A=sorted(A,key=lambda x: x[2])

for i in range (N-1):
    Ans[A[i][1]-1]+=Ans[A[i][0]-1]
print(*Ans)


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())

A=[[0]*2 for i in range(N)]
Ans=[0]*(N+1)
P=[[0]*2 for i in range(Q)]

for i in range(N-1):
    A[i][0],A[i][1] = map(int, input().split())
for i in range(Q):
    P[i][0],P[i][1] = map(int, input().split())
    Ans[P[i][0]]+=P[i][1]

A.sort()

for i in range(N):
    Ans[A[i][1]]+=Ans[A[i][0]]

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


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())

A=[[0]*2 for i in range(N)]
Ans=[0]*(N+1)
P=[[0]*2 for i in range(Q)]
X=[0]*Q

for i in range(N-1):
    A[i][0],A[i][1] = map(int, input().split())

for i in range(Q):
    P[i][0],P[i][1] = map(int, input().split())

A.sort()
P.sort()

for i in range(Q):
    Ans[P[i][0]]+=P[i][1]

for i in range(N):
    Ans[A[i][1]]+=Ans[A[i][0]]

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


Fixed submission:
import sys

n, q = [int(i) for i in sys.stdin.readline().split()]
graph = {i: [] for i in range(n)}
ct = [0 for i in range(n)]
for i in range(n - 1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

for i in range(q):
    p, x = [int(i) for i in sys.stdin.readline().split()]
    p -= 1
    ct[p] += x
already = {0}
stack = [(0, ct[0], -1)]
res = [0 for i in range(n)]
while len(stack) > 0:
    cur, cost, _from = stack.pop(-1)
    res[cur] = cost
    for _next in graph[cur]:
        if _next == _from:
            continue
        next_cost = ct[_next]
        stack.append((_next, cost + next_cost, cur))
        already.add(cur)
print(" ".join([str(r) for r in res]))