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

N, M = map(int, input().split())
BtoA = [[] for i in range(N + 1)]


for i in range(N - 1 + M):
    A, B = map(int, input().split())
    BtoA[B].append(A)

memo = {}
def depth(b):
    if b in memo:
        return memo[b]
    
    res = 0
    for a in BtoA[b]:
        res = max(res, depth(a) + 1)

    memo[b] = res
    return res

for i in range(1, N + 1):
    ans = 0
    for a in BtoA[i]:
        if depth(a) + 1 == depth(i):
            ans = a
            break
    print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

N, M = map(int, input().split())
BtoA = [[] for i in range(N + 1)]


for i in range(N - 1 + M):
    A, B = map(int, input().split())
    BtoA[B].append(A)

memo = {}
def depth(b):
    if b in memo:
        return memo[b]
    
    res = 0
    for a in BtoA[b]:
        res = max(res, depth(a) + 1)

    memo[b] = res
    return res

for i in range(1, N + 1):
    ans = 0
    for a in BtoA[i]:
        if depth(a) + 1 == depth(i):
            ans = a
            break
    print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
BtoA = [[] for i in range(N + 1)]


for i in range(N - 1 + M):
    A, B = map(int, input().split())
    BtoA[B].append(A)

memo = {}
def depth(b):
    if b in memo:
        return memo[b]
    
    res = 0
    for a in BtoA[b]:
        res = max(res, depth(a) + 1)

    memo[b] = res
    return res

for i in range(1, N + 1):
    ans = 0
    for a in BtoA[i]:
        if depth(a) + 1 == depth(i):
            ans = a
            break
    print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

N, M = map(int, input().split())
BtoA = [[] for i in range(N + 1)]


for i in range(N - 1 + M):
    A, B = map(int, input().split())
    BtoA[B].append(A)

memo = {}
def depth(b):
    if b in memo:
        return memo[b]
    
    res = 0
    for a in BtoA[b]:
        res = max(res, depth(a) + 1)

    memo[b] = res
    return res

for i in range(1, N + 1):
    ans = 0
    for a in BtoA[i]:
        if depth(a) + 1 == depth(i):
            ans = a
            break
    print(ans)

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

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

v = [[] for i in range(N + 1)]
vin = [0 for i in range(N + 1)]

parent = [0 for i in range(N + 1)]
# visited = [False for i in range(N + 1)]

for i in range(N + M - 1):
    A, B = map(int, input().split())
    v[A].append(B)
    vin[B] += 1

Q = deque()
for i in range(1, N + 1):
    if vin[i] == 0:
        Q.append((i, 0))
        break

while len(Q) != 0:
    now, pa = Q.popleft()
    parent[now] = pa
    # if visited[now]:
    #     continue
    for i in v[now]:
        Q.append((i, now))
    # visited[now] = True

for i in range(1, N + 1):
    print(parent[i])

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

N, M = map(int, input().split())
BtoA = [[] for i in range(N + 1)]


for i in range(N - 1 + M):
    A, B = map(int, input().split())
    BtoA[B].append(A)

memo = {}
def depth(b):
    if b in memo:
        return memo[b]
    
    res = 0
    for a in BtoA[b]:
        res = max(res, depth(a) + 1)

    memo[b] = res
    return res

for i in range(1, N + 1):
    ans = 0
    for a in BtoA[i]:
        if depth(a) + 1 == depth(i):
            ans = a
            break
    print(ans)