Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)
pairents = [-1]*N

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    X[b-1] += 1

q = X.index(0)

Q = deque([q])
while Q:
    p = Q.popleft()
    for q in G[p]:
        if searched[q] <= searched[p]:
            searched[q] = searched[p] + 1
            Q.append(q)

for a in range(N):
    for b in G[a]:
        if searched[b] == searched[a] + 1:
            pairents[b] = a
for p in pairents:
    print(p+1)
    


Fixed submission:
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
rG = {i: [] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    rG[b-1].append(a-1)
    X[b-1] += 1

q = X.index(0)
T = [0]*N
Q = deque([q])
t = 0
while Q:
    p = Q.popleft()
    T[p] = t
    t += 1
    for q in G[p]:          
        X[q] -= 1
        if X[q] == 0:
            Q.append(q)

for i in range(N):
    c, ans = -1, 0
    for p in rG[i]:
        if c < T[p]:
            c, ans = T[p], p+1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)
pairents = [-1]*N

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    X[b-1] += 1

q = X.index(0)

Q = deque([q])
while Q:
    p = Q.pop()
    for q in G[p]:
        if searched[q] <= searched[p]:
            searched[q] = searched[p] + 1
            Q.append(q)

for a in range(N):
    for b in G[a]:
        if searched[b] == searched[a] + 1:
            pairents[b] = a
for p in pairents:
    print(p+1)
    

Fixed submission:
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
rG = {i: [] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    rG[b-1].append(a-1)
    X[b-1] += 1

q = X.index(0)
T = [0]*N
Q = deque([q])
t = 0
while Q:
    p = Q.popleft()
    T[p] = t
    t += 1
    for q in G[p]:          
        X[q] -= 1
        if X[q] == 0:
            Q.append(q)

for i in range(N):
    c, ans = -1, 0
    for p in rG[i]:
        if c < T[p]:
            c, ans = T[p], p+1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)
pairents = [-1]*N

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    X[b-1] += 1

q = X.index(0)

Q = deque([q])
while Q:
    p = Q.pop()
    for q in G[p]:
        if searched[q] <= searched[p]:
            searched[q] = searched[p] + 1
            Q.append(q)

for a in range(N):
    for b in G[a]:
        if searched[b] == searched[a] + 1:
            pairents[b] = a
for p in pairents:
    print(p+1)
    

Fixed submission:
from collections import deque
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = {i:[] for i in range(N)}
rG = {i: [] for i in range(N)}
X = [0]*(N)
searched = [0]*(N)

for _ in range(N-1+M):
    a, b = inpl()
    G[a-1].append(b-1)
    rG[b-1].append(a-1)
    X[b-1] += 1

q = X.index(0)
T = [0]*N
Q = deque([q])
t = 0
while Q:
    p = Q.popleft()
    T[p] = t
    t += 1
    for q in G[p]:          
        X[q] -= 1
        if X[q] == 0:
            Q.append(q)

for i in range(N):
    c, ans = -1, 0
    for p in rG[i]:
        if c < T[p]:
            c, ans = T[p], p+1
    print(ans)