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


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def dfs(parent, G, C, V):
    V[parent] = True
    for child in G[parent]:
        if not V[child]:
            C[child] += C[parent]
            dfs(child, G, C, V)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    dfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

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


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def dfs(parent, G, C, V):
    V[parent] = True
    for child in G[parent]:
        if not V[child]:
            C[child] += C[parent]
            dfs(child, G, C, V)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    dfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from collections import deque



def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [0 for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    depth = [-1 for i in range(N+1)]
    parents = [-1 for i in range(N+1)]
    q = deque()
    q.append(1)
    depth[1] = 0
    parents[1] = 0
    while len(q) > 0:
        node = q.pop()
        for neighbor in G[node]:
            if depth[neighbor] < 0:
                depth[neighbor] = depth[node] + 1
                parents[neighbor] = node
                q.append(neighbor)
    order = np.argsort(depth[1:]) + 1
    V[1] = C[1]
    for i in order:
        child = i
        parent = parents[i]
        V[child] = V[parent] + C[child]
    return ' '.join(map(str, V[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from collections import deque



def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [0 for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    depth = [-1 for i in range(N+1)]
    q = deque()
    q.append(1)
    depth[1] = 0
    while len(q) > 0:
        node = q.pop()
        for neighbor in G[node]:
            if depth[neighbor] < 0:
                depth[neighbor] = depth[node] + 1
                q.append(neighbor)
    order = np.argsort(depth[1:])
    V[1] = C[1]
    for node in np.arange(1, N+1)[order]:
        for neighbor in G[node]:
            if depth[neighbor] < depth[node]:
                parent = neighbor
                V[node] = V[parent] + C[node]
    return ' '.join(map(str, V[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [i for i in range(N+1)]
    PX = list()
    G[1] = 0  # 終点
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[b] = a  # 逆辺を張る
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [-1 for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    for i in range(N, 0, -1):
        node = i
        v = 0
        while node > 0:
            if V[node] >= 0:  # use cache
                v += V[node]
                break
            v += C[node]
            node = G[node]
        V[i] = v
    return ' '.join(map(str, V[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [i for i in range(N+1)]
    PX = list()
    G[1] = 0  # 終点
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[b] = a  # 逆辺を張る
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [-1 for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    for i in range(N, 0, -1):
        node = i
        v = 0
        while node > 0:
            if V[node] > 0:  # use cache
                v += V[node]
                break
            v += C[node]
            node = G[node]
        V[i] = v
    return ' '.join(map(str, V[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [i for i in range(N+1)]
    PX = list()
    G[1] = 0  # 終点
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[b] = a  # 逆辺を張る
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [-1 for i in range(N+1)]
    values = deque()
    for p, x in PX:
        C[p] += x
    for i in range(1, N+1):
        node = i
        v = 0
        while node > 0:
            if V[node] > 0:  # use cache
                v += V[node]
                break
            v += C[node]
            node = G[node]
        V[i] = v 
        values.append(v)
    return ' '.join(map(str, values))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [i for i in range(N+1)]
    PX = list()
    G[1] = 0  # 終点
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[b] = a  # 逆辺を張る
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    values = deque()
    for p, x in PX:
        C[p] += x
    for i in range(1, N+1):
        node = i
        v = 0
        while node > 0:
            v += C[node]
            node = G[node]
        values.append(v)
    return ' '.join(map(str, values))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


Fixed submission:
import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))