Buggy python submission (verdict: Runtime Error):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P_cand = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P_cand[b].add(a)
for p in range(1, N):
    if len(P_cand[p]) == 0:
        break
D = [-1] * (N + 1)
P = [0] * (N + 1)


def dfs(p, d):
    for c in G[p]:
        if D[c] < d + 1:
            D[c] = d + 1
            P[c] = p
            dfs(c, d + 1)


dfs(p, 0)
[print(_) for _ in P[1:]]


Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P_cand = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P_cand[b].add(a)
for p in range(1, N):
    if len(P_cand[p]) == 0:
        break
D = [-1] * (N + 1)
P = [-1] * (N + 1)


def bfs(p):
    H = [[p, 0, 0]]
    while H:
        c, p, d = heapq.heappop(H)
        if D[c] < d:
            D[c] = d
            P[c] = p
            for c2 in G[c]:
                heapq.heappush(H, [c2, c, d + 1])


bfs(p)
[print(_) for _ in P[1:]]


Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')

Buggy python submission (verdict: Runtime Error):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P_cand = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P_cand[b].add(a)
for p in range(1, N):
    if len(P_cand[p]) == 0:
        break
D = [[0, -1] for _ in range(N + 1)]
D[p] = [0, 0]


def dfs(p):
    for _ in range(len(G[p])):
        for c in G[p]:
            if D[c][1] < D[p][1] + 1:
                D[c][1] = D[p][1] + 1
                D[c][0] = p
                dfs(c)


dfs(p)
[print(_[0]) for _ in D[1:]]


Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P[b].add(a)
for i in range(1, N):
    if len(P[i]) == 0:
        break


def bellman_ford(V, G, i):
    ans = [0] * V
    INF = float('inf')
    D = [INF] * V
    D[i] = 0
    for _ in range(1, V):
        update = False
        for p in range(1, V):
            for c in G[p]:
                if D[p] != INF and D[c] > D[p] - 1:
                    D[c] = D[p] - 1
                    ans[c] = p
                    update = True
        if not update:
            return ans
        if _ == V:
            return False


D = bellman_ford(N + 1, G, i)[1:]
[print(_) for _ in D]


Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P[b].add(a)
for i in range(1, N):
    if len(P[i]) == 0:
        break


def bellman_ford(V, G, i):
    ans = [0] * V
    INF = float('inf')
    D = [INF] * V
    D[i] = 0
    for _ in range(1, V):
        update = False
        for p in range(1, V):
            for c in G[p]:
                if D[p] != INF and D[c] > D[p] - 1:
                    D[c] = D[p] - 1
                    ans[c] = p
                    update = True
        if not update:
            return ans
        if _ == V:
            return False


D = bellman_ford(N + 1, G, i)[1:]
[print(_) for _ in D]


Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')