Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]

sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

def dfs(v, p = -1):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

    AB = [list(map(int,input().split())) for _ in range(N-1)]
    AB.sort()

    for a,b in AB:
        tree[a].append(b)
        tree[b].append(a)
    # print(tree)

    for _ in range(Q):
        p,x = map(int, input().split())
        ans[p] += x
    # print(ans)

    dfs(1)
    print(*ans[1:])

if __name__ == "__main__":
    main()


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
def input():
    return sys.stdin.readline()[:-1]

N, Q = map(int, input().split())
tree = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

def dfs(v, p = -1):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

    AB = [list(map(int,input().split())) for _ in range(N-1)]
    AB.sort()

    for a,b in AB:
        tree[a].append(b)
        tree[b].append(a)

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

    dfs(1)
    print(*ans[1:])

if __name__ == "__main__":
    main()


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    # AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for _ in range(N-1):
        root,leaf = map(int,input().split())
        roots[leaf-1] = root-1

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

if __name__ == "__main__":
    main()


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

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

if __name__ == "__main__":
    main()


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    AB = [list(map(int,input().split())) for _ in range(N-1)]
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

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

if __name__ == "__main__":
    main()


Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    roots = {}
    for root,leaf in AB:
        roots[leaf] = root

    ans = [0 for _ in range(N)]
    for _ in range(Q):
        ans[int(p)-1] += int(x)
    # print(ans)
    for i in range(1,N):
        ans[i] += ans[roots[i+1]-1]
    print(*ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**6)
N, Q = map(int, input().split())
tree = [[] for _ in range(N)]
ans = [0] * N

def dfs(v, p):
    for u in tree[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)

def main():

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

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

    dfs(0, -1)
    print(*ans)

if __name__ == "__main__":
    main()