Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [False] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] = True
# print (lst)

ans_lst = [[-1, 0] for _ in range(N)] #[親, 距離]を要素とするリスト
#根を求める = どこからも辺が伸びていないところ --> check_lst = Falseの場所
n = check_lst.index(False)
ans_lst[n][0] = -1
# print (n)

def dfs(i): #iからの経路を探す
    distance = ans_lst[i][1]
    for j in lst[i]:
        # print (j)
        if distance + 1 > ans_lst[j][1]:#距離が最長となるとき
            ans_lst[j][0] = i
            ans_lst[j][1] = distance + 1
            dfs(j)
    return 

dfs(n)
# print (ans_lst)

# print ('以下答え')
for i, d in ans_lst:
    print (i + 1)


Fixed submission:
import sys
# from heapq import heappop, heappush
from collections import deque
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [0] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] += 1

n = check_lst.index(0)
ans = [0] * N
que = deque()
que.append(n)
while que:
    v = que.popleft()
    for v2 in lst[v]:
        check_lst[v2] -= 1
        if check_lst[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1
print (*ans, sep = '\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [False] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] = True
# print (lst)

ans_lst = [[-1, 0] for _ in range(N)] #[親, 距離]を要素とするリスト
#根を求める = どこからも辺が伸びていないところ --> check_lst = Falseの場所
n = check_lst.index(False)
ans_lst[n][0] = -1
# print (n)

def dfs(i): #iからの経路を探す
    distance = ans_lst[i][1]
    for j in lst[i]:
        # print (j)
        if distance + 1 > ans_lst[j][1]:#距離が最長となるとき
            ans_lst[j][0] = i
            ans_lst[j][1] = distance + 1
            dfs(j)
    return 

dfs(n)
# print (ans_lst)

# print ('以下答え')
for i, d in ans_lst:
    print (i + 1)


Fixed submission:
import sys
# from heapq import heappop, heappush
from collections import deque
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [0] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] += 1

n = check_lst.index(0)
ans = [0] * N
que = deque()
que.append(n)
while que:
    v = que.popleft()
    for v2 in lst[v]:
        check_lst[v2] -= 1
        if check_lst[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1
print (*ans, sep = '\n')

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [False] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] = True
# print (lst)

ans_lst = [[-1, 0] for _ in range(N)] #[親, 距離]を要素とするリスト
#根を求める = どこからも辺が伸びていないところ --> check_lst = Falseの場所
n = check_lst.index(False)
ans_lst[n][0] = -1
# print (n)

def dfs(i): #iからの経路を探す
    distance = ans_lst[i][1]
    for j in lst[i]:
        # print (j)
        if distance + 1 > ans_lst[j][1]:#距離が最長となるとき
            ans_lst[j][0] = i
            ans_lst[j][1] = distance + 1
            dfs(j)
    return 

dfs(n)
# print (ans_lst)

# print ('以下答え')
for i, d in ans_lst:
    print (i + 1)


Fixed submission:
import sys
# from heapq import heappop, heappush
from collections import deque
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N, M = map(int, input().split())
check_lst = [0] * N
lst = [[] for _ in range(N)]
for _ in range(N + M - 1):
    A, B = map(int, input().split())
    A -= 1
    B -= 1
    lst[A].append(B)
    check_lst[B] += 1

n = check_lst.index(0)
ans = [0] * N
que = deque()
que.append(n)
while que:
    v = que.popleft()
    for v2 in lst[v]:
        check_lst[v2] -= 1
        if check_lst[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1
print (*ans, sep = '\n')