Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(20000)

def dfs(i,x):
    ans[i]+=x
    if dist[i]==bottom:
        return
    for n in tree[i]:
        if dist[n]>dist[i]:
            dfs(n,x)

from collections import deque

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

tree=[[] for _ in range(N)]

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

#BFSで各ノードの深さを調べる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

#カウンターをDFSで制御
ans=[0 for _ in range(N)]
bottom=max(dist)
for j in range(Q):
    p,x=map(int,input().split())
    dfs(p-1,x)

print(*ans)

Fixed submission:
from collections import deque

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

tree=[[] for _ in range(N)]

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

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

#BFSで親のカウンターを子に継がせる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        que.append(w)
        dist[w]=d+1
        ans[w]+=ans[v]

print(*ans)

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

def dfs(i,x):
    ans[i]+=x
    if dist[i]==bottom:
        return
    for n in tree[i]:
        if dist[n]>dist[i]:
            dfs(n,x)

from collections import deque

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

tree=[[] for _ in range(N)]

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

#BFSで各ノードの深さを調べる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

#カウンターをDFSで制御
ans=[0 for _ in range(N)]
bottom=max(dist)
for j in range(Q):
    p,x=map(int,input().split())
    dfs(p-1,x)

print(*ans)

Fixed submission:
from collections import deque

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

tree=[[] for _ in range(N)]

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

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

#BFSで親のカウンターを子に継がせる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        que.append(w)
        dist[w]=d+1
        ans[w]+=ans[v]

print(*ans)

Buggy python submission (verdict: Runtime Error):
def dfs(i,x):
    ans[i]+=x
    if dist[i]==bottom:
        return
    for n in tree[i]:
        if dist[n]>dist[i]:
            dfs(n,x)

from collections import deque

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

tree=[[] for _ in range(N)]

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

#BFSで各ノードの深さを調べる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

#カウンターをDFSで制御
ans=[0 for _ in range(N)]
bottom=max(dist)
for j in range(Q):
    p,x=map(int,input().split())
    dfs(p-1,x)

print(*ans)

Fixed submission:
from collections import deque

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

tree=[[] for _ in range(N)]

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

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

#BFSで親のカウンターを子に継がせる
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in tree[v]:
        if dist[w] > -1:
            continue
        que.append(w)
        dist[w]=d+1
        ans[w]+=ans[v]

print(*ans)