Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
input=sys.stdin.readline


def solve():
    N, M = map(int, input().split())
    d = {i:[] for i in range(1, N+1)}

    used = set()
    for _ in range(N+M-1):
        A, B = map(int, input().split())
        d[A].append(B)
        used.add(B)

    root = (set(range(1, N+1))-used).pop()

    q = deque([(root, 0, 0)])
    parents = [0] * N
    dist = [0] * (N+1)

    while q:
        s, r, x = q.popleft()
        if x>dist[s] or r==0:
            dist[s] = x
            parents[s-1] = r
        else:
            continue
        for t in d[s]:
            q.append((t, s, x+1))

    for i in parents:
        print(i)


if __name__ == "__main__":
    solve()


Fixed submission:
import sys
from collections import deque
input=sys.stdin.readline

N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

l = []
h = [0] * (N+1)

for _ in range(N+M-1):
    A, B = map(int, input().split())
    d[A].append(B)
    e[B].append(A)
    h[B] += 1

st = set()
for i in range(1, N+1):
    if h[i]==0:
        st.add(i)

l = []
while st:
    i = st.pop()
    l.append(i)
    for j in d[i]:
        h[j] -= 1
        if h[j] == 0:
            st.add(j)

tp = {j:i for i,j in enumerate(l, start=1)}
ans = [0] * (N+1)
for i in range(1, N+1):
    s, t = 0, 0
    for j in e[i]:
        if s < tp[j]:
            s = tp[j]
            t = j
    print(t)

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


def solve():
    N, M = map(int, input().split())
    d = {i:[] for i in range(1, N+1)}

    used = set()
    for _ in range(N+M-1):
        A, B = map(int, input().split())
        d[A].append(B)
        used.add(B)

    root = (set(range(1, N+1))-used).pop()

    q = [(root, 0, 0)]
    parents = [0] * N
    dist = [0] * (N+1)

    while q:
        s, r, x = q.pop()
        if x>dist[s] or r==0:
            dist[s] = x
            parents[s-1] = r
        else:
            continue
        for t in d[s]:
            q.append((t, s, x+1))

    for i in parents:
        print(i)


if __name__ == "__main__":
    solve()


Fixed submission:
import sys
from collections import deque
input=sys.stdin.readline

N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

l = []
h = [0] * (N+1)

for _ in range(N+M-1):
    A, B = map(int, input().split())
    d[A].append(B)
    e[B].append(A)
    h[B] += 1

st = set()
for i in range(1, N+1):
    if h[i]==0:
        st.add(i)

l = []
while st:
    i = st.pop()
    l.append(i)
    for j in d[i]:
        h[j] -= 1
        if h[j] == 0:
            st.add(j)

tp = {j:i for i,j in enumerate(l, start=1)}
ans = [0] * (N+1)
for i in range(1, N+1):
    s, t = 0, 0
    for j in e[i]:
        if s < tp[j]:
            s = tp[j]
            t = j
    print(t)

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


def solve():
    N, M = map(int, input().split())
    d = {i:[] for i in range(1, N+1)}
    e = {i:[] for i in range(1, N+1)}

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

    root = [k for k,v in e.items() if not v][0]
    dist = [0] * (N+1)

    q = [(root, 0, 0)]
    parents = [0] * (N+1)

    while q:
        s, r, x = q.pop()
        if x>dist[s] or r==0:
            dist[s] = x
            parents[s] = r
        else:
            continue
        for t in d[s]:
            q.append((t, s, x+1))

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


if __name__ == "__main__":
    solve()


Fixed submission:
import sys
from collections import deque
input=sys.stdin.readline

N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

l = []
h = [0] * (N+1)

for _ in range(N+M-1):
    A, B = map(int, input().split())
    d[A].append(B)
    e[B].append(A)
    h[B] += 1

st = set()
for i in range(1, N+1):
    if h[i]==0:
        st.add(i)

l = []
while st:
    i = st.pop()
    l.append(i)
    for j in d[i]:
        h[j] -= 1
        if h[j] == 0:
            st.add(j)

tp = {j:i for i,j in enumerate(l, start=1)}
ans = [0] * (N+1)
for i in range(1, N+1):
    s, t = 0, 0
    for j in e[i]:
        if s < tp[j]:
            s = tp[j]
            t = j
    print(t)

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


def solve():
    N, M = map(int, input().split())
    d = {i:[] for i in range(1, N+1)}
    e = {i:[] for i in range(1, N+1)}

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

    root = [k for k,v in e.items() if not v][0]
    dist = [0] * (N+1)

    q = [(root, 0, 0)]
    parents = [0] * (N+1)

    while q:
        s, r, x = q.pop()
        if x>dist[s]:
            dist[s] = x
            parents[s] = r
        for t in d[s]:
            q.append((t, s, x+1))

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


if __name__ == "__main__":
    solve()


Fixed submission:
import sys
from collections import deque
input=sys.stdin.readline

N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

l = []
h = [0] * (N+1)

for _ in range(N+M-1):
    A, B = map(int, input().split())
    d[A].append(B)
    e[B].append(A)
    h[B] += 1

st = set()
for i in range(1, N+1):
    if h[i]==0:
        st.add(i)

l = []
while st:
    i = st.pop()
    l.append(i)
    for j in d[i]:
        h[j] -= 1
        if h[j] == 0:
            st.add(j)

tp = {j:i for i,j in enumerate(l, start=1)}
ans = [0] * (N+1)
for i in range(1, N+1):
    s, t = 0, 0
    for j in e[i]:
        if s < tp[j]:
            s = tp[j]
            t = j
    print(t)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

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

root = [k for k,v in e.items() if not v][0]
dist = [0] * (N+1)

q = [(root, 0, 0)]
parents = [0] * (N+1)

while q:
    s, r, x = q.pop()
    if x>dist[s]:
        dist[s] = x
        parents[s] = r
    for t in d[s]:
        q.append((t, s, x+1))

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


Fixed submission:
import sys
from collections import deque
input=sys.stdin.readline

N, M = map(int, input().split())
d = {i:[] for i in range(1, N+1)}
e = {i:[] for i in range(1, N+1)}

l = []
h = [0] * (N+1)

for _ in range(N+M-1):
    A, B = map(int, input().split())
    d[A].append(B)
    e[B].append(A)
    h[B] += 1

st = set()
for i in range(1, N+1):
    if h[i]==0:
        st.add(i)

l = []
while st:
    i = st.pop()
    l.append(i)
    for j in d[i]:
        h[j] -= 1
        if h[j] == 0:
            st.add(j)

tp = {j:i for i,j in enumerate(l, start=1)}
ans = [0] * (N+1)
for i in range(1, N+1):
    s, t = 0, 0
    for j in e[i]:
        if s < tp[j]:
            s = tp[j]
            t = j
    print(t)