Buggy python submission (verdict: Time Limit Exceeded):
# 20/5/28
import sys
sys.setrecursionlimit(3 * 10 ** 5)
# input = sys.stdin.readline

def bfs(root, parent):
    global AL, cnt
    for v in AL[root]:
        if v != parent:
            cnt[v] += cnt[root]
            bfs(v, root)

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

bfs(1, -1)
print(*cnt[1:])

Fixed submission:
# 20/5/28
from collections import deque

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
stk = deque([1])
while stk:
    root = stk.pop()
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            stk.append(v)

s = " ".join(map(str, cnt[1:]))
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(3 * 10 ** 5)

def bfs(root, parent):
    global AL, cnt
    for v in AL[root]:
        if v != parent:
            cnt[v] += cnt[root]
            bfs(v, root)

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

bfs(1, -1)
s = " ".join(map(str, cnt[1:]))
print(s)

Fixed submission:
# 20/5/28
from collections import deque

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
stk = deque([1])
while stk:
    root = stk.pop()
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            stk.append(v)

s = " ".join(map(str, cnt[1:]))
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
# 20/5/28
import sys
sys.setrecursionlimit(3 * 10 ** 5)

def bfs(root):
    global AL, cnt, visited
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            bfs(v)

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
bfs(1)
s = " ".join(map(str, cnt[1:]))
print(s)

Fixed submission:
# 20/5/28
from collections import deque

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
stk = deque([1])
while stk:
    root = stk.pop()
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            stk.append(v)

s = " ".join(map(str, cnt[1:]))
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
# 20/5/28
import sys
sys.setrecursionlimit(3 * 10 ** 5)

def bfs(root):
    global AL, cnt, visited
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            bfs(v)

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
bfs(1)
s = " ".join(map(str, cnt[1:]))
print(s)

Fixed submission:
# 20/5/28
from collections import deque

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
stk = deque([1])
while stk:
    root = stk.pop()
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            stk.append(v)

s = " ".join(map(str, cnt[1:]))
print(s)

Buggy python submission (verdict: Runtime Error):
class RootedTree():
    def __init__(self, N, root = 1):
        # N is the number of nodes
        # self.nodes is a list of TreeNode
        self.nodes = [None] * (N + 1) # self.nodes[0] is kept None
        self.root = root
        self.N = N # number of nodes

    def add_node(self, tree_node, num):
        self.nodes[num] = tree_node

    def add_val(self, p, x, Q):
        for j in range(1, Q + 1):
            self._add_val(p[j], x[j])

    def _add_val(self, p_val, x_val):
        self.nodes[p_val].increment_counter(x_val)
        for ch in self.nodes[p_val].children:
            self._add_val(ch, x_val)

    def print_counter(self):
        for i in range(1, N):
            print("{} ".format(self.nodes[i].counter), end = "")
        print("{} ".format(self.nodes[N].counter))

class TreeNode():
    def __init__(self, num):
        self.number = num  # integer (vertex number)
        self.counter = 0
        self.children = set()

    def add_child(self, v):
        self.children.add(v)

    def increment_counter(self, x):
        self.counter += x

class Graph():
    def __init__(self, N):
        self.AL = [None] + [set() for i in range(N)] # adjacency list
        self.N = N # number ov vertices

    def add_edge(self, u, v):
        self.AL[u].add(v)
        self.AL[v].add(u)

    def to_rooted_tree(self):
        rooted_tree = RootedTree(self.N, 1)
        self._to_rooted_tree(1, {1}, rooted_tree)
        return rooted_tree

    def _to_rooted_tree(self, v, visited, rooted_tree):
        node = TreeNode(v)
        rooted_tree.add_node(node, v)

        for u in self.AL[v]: # leaf has only one neighbour (parent)
            if not u in visited:
                visited.add(u)
                node.add_child(u)
                self._to_rooted_tree(u, visited, rooted_tree)


# input
N, Q = [int(k) for k in input().split()]
a = [None] * N # a[0] = None
b = [None] * N # b[0] = None
for i in range(1, N):
    a[i], b[i] = [int(k) for k in input().split()]
p = [None] * (Q + 1)
x = [None] * (Q + 1)
for i in range(1, Q + 1):
    p[i], x[i] = [int(k) for k in input().split()]

# create a graph
G = Graph(N)
for i in range(1, N):
    G.add_edge(a[i], b[i])

# run depth first search to form a rooted tree
T = G.to_rooted_tree()


T.add_val(p, x, Q)
T.print_counter()


Fixed submission:
# 20/5/28
from collections import deque

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

cnt = [0] * (N + 1)
for j in range(Q):
    p, x = [int(x) for x in input().split()]
    cnt[p] += x

visited = [False] * (N + 1)
stk = deque([1])
while stk:
    root = stk.pop()
    visited[root] = True
    for v in AL[root]:
        if not visited[v]:
            cnt[v] += cnt[root]
            stk.append(v)

s = " ".join(map(str, cnt[1:]))
print(s)