Buggy python submission (verdict: Runtime Error):
from collections import deque, defaultdict

N, M = map(int, input().split())
ab = []
for _ in range(N + M - 1):
    ab.append(tuple(map(int, input().split())))

in_cnt = defaultdict(int)
outs = defaultdict(list)
for a, b in ab:
    in_cnt[b - 1] += 1
    outs[a - 1].append(b - 1)

res = []
queue = deque([i for i in range(N) if in_cnt[i] == 0])
while queue:
    v = queue.popleft()
    res.append(v)
    for v2 in outs[v]:
        in_cnt[v2] -= 1
        if in_cnt[v2] == 0:
            queue.append(v2)

order = [i for i, v in sorted(enumerate(res), key=lambda x: x[1])]

ans = [0] * N
ans[res[0]] = 0
for v in res[:0:-1]:
    ans[v] = max([(order[v2], v2) for v2 in rev[v]])[1] + 1

for a in ans:
    print(a)


Fixed submission:
from collections import deque, defaultdict

N, M = map(int, input().split())
ab = []
for _ in range(N + M - 1):
    ab.append(tuple(map(int, input().split())))

in_cnt = defaultdict(int)
rev = defaultdict(list)
outs = defaultdict(list)
for a, b in ab:
    in_cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    rev[b - 1].append(a - 1)

res = []
queue = deque([i for i in range(N) if in_cnt[i] == 0])
while queue:
    v = queue.popleft()
    res.append(v)
    for v2 in outs[v]:
        in_cnt[v2] -= 1
        if in_cnt[v2] == 0:
            queue.append(v2)

order = [i for i, v in sorted(enumerate(res), key=lambda x: x[1])]

ans = [0] * N
ans[res[0]] = 0
for v in res[:0:-1]:
    ans[v] = max([(order[v2], v2) for v2 in rev[v]])[1] + 1

for a in ans:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
# from collections import deque
N, M = map(int, input().split())

G = {k: [] for k in range(N+1)}
r = [True]*(N+1)
for _ in range(N+M-1):
    a, b = map(int, input().split())
    r[b] = False
    # 有向グラフ
    G[a].append(b)

for i in range(1, N+1):
    if r[i] is True:
        root = i


P = [-1]*(N+1)
P[root] = 0
dist = [-1]*(N+1)

que = [(root, 0, 0)]


while que:
    ci, pi, d = que.pop()
    if d > dist[ci]:
        P[ci] = pi
        dist[ci] = d

    for ni in G[ci]:
        if d+1 > dist[ni]:
            que.append((ni, ci, d+1))

print(*P[1:], sep='\n')


Fixed submission:
from collections import deque, defaultdict

N, M = map(int, input().split())
ab = []
for _ in range(N + M - 1):
    ab.append(tuple(map(int, input().split())))

in_cnt = defaultdict(int)
rev = defaultdict(list)
outs = defaultdict(list)
for a, b in ab:
    in_cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    rev[b - 1].append(a - 1)

res = []
queue = deque([i for i in range(N) if in_cnt[i] == 0])
while queue:
    v = queue.popleft()
    res.append(v)
    for v2 in outs[v]:
        in_cnt[v2] -= 1
        if in_cnt[v2] == 0:
            queue.append(v2)

order = [i for i, v in sorted(enumerate(res), key=lambda x: x[1])]

ans = [0] * N
ans[res[0]] = 0
for v in res[:0:-1]:
    ans[v] = max([(order[v2], v2) for v2 in rev[v]])[1] + 1

for a in ans:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())

G = {k: [] for k in range(N+1)}
r = [True]*(N+1)
for _ in range(N+M-1):
    a, b = map(int, input().split())
    r[b] = False
    # 有向グラフ
    G[a].append(b)

for i in range(1, N+1):
    if r[i] is True:
        root = i


P = [-1]*(N+1)
P[root] = 0
dist = [-1]*(N+1)

que = deque([(root, 0, 0)])


while que:
    ci, pi, d = que.popleft()
    if d > dist[ci]:
        P[ci] = pi
        dist[ci] = d

    for ni in G[ci]:
        que.append((ni, ci, d+1))

print(*P[1:], sep='\n')


Fixed submission:
from collections import deque, defaultdict

N, M = map(int, input().split())
ab = []
for _ in range(N + M - 1):
    ab.append(tuple(map(int, input().split())))

in_cnt = defaultdict(int)
rev = defaultdict(list)
outs = defaultdict(list)
for a, b in ab:
    in_cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    rev[b - 1].append(a - 1)

res = []
queue = deque([i for i in range(N) if in_cnt[i] == 0])
while queue:
    v = queue.popleft()
    res.append(v)
    for v2 in outs[v]:
        in_cnt[v2] -= 1
        if in_cnt[v2] == 0:
            queue.append(v2)

order = [i for i, v in sorted(enumerate(res), key=lambda x: x[1])]

ans = [0] * N
ans[res[0]] = 0
for v in res[:0:-1]:
    ans[v] = max([(order[v2], v2) for v2 in rev[v]])[1] + 1

for a in ans:
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())

G = {k: [] for k in range(N+1)}
r = [True]*(N+1)
for _ in range(N+M-1):
    a, b = map(int, input().split())
    r[b] = False
    # 有向グラフ
    G[a].append(b)

for i in range(1, N+1):
    if r[i] is True:
        root = i


P = [-1]*(N+1)
P[root] = 0
dist = [-1]*(N+1)

que = deque([(root, 0, 0)])


while que:
    ci, pi, d = que.popleft()
    if d > dist[ci]:
        P[ci] = pi
        dist[ci] = d

    for ni in G[ci]:
        que.append((ni, ci, d+1))

print(*P[1:], sep='\n')


Fixed submission:
from collections import deque, defaultdict

N, M = map(int, input().split())
ab = []
for _ in range(N + M - 1):
    ab.append(tuple(map(int, input().split())))

in_cnt = defaultdict(int)
rev = defaultdict(list)
outs = defaultdict(list)
for a, b in ab:
    in_cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    rev[b - 1].append(a - 1)

res = []
queue = deque([i for i in range(N) if in_cnt[i] == 0])
while queue:
    v = queue.popleft()
    res.append(v)
    for v2 in outs[v]:
        in_cnt[v2] -= 1
        if in_cnt[v2] == 0:
            queue.append(v2)

order = [i for i, v in sorted(enumerate(res), key=lambda x: x[1])]

ans = [0] * N
ans[res[0]] = 0
for v in res[:0:-1]:
    ans[v] = max([(order[v2], v2) for v2 in rev[v]])[1] + 1

for a in ans:
    print(a)