Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_out = [[] for _ in range(N + 1)]
e_in = [[] for _ in range(N + 1)]
for _ in range(N + M - 1):
    a, b = map(int, readline().split())
    e_out[a].append(b)
    e_in[b].append(a)


# 1. 入次数が0のやつが親
# 2. 親が決まれば、そのこの入次数を減らす,1に戻る

# 親を決めるよ
for i, e in enumerate(e_in[1:], start=1):
    if len(e) == 0:
        root = i
        break

node = [root]
parent = [-1] * (N + 1)
parent[root] = 0
diff = [0] * (N + 1)  # 入次数をどれだけ減らすか

while node:
    s = node.pop()
    for t in e_out[s]:
        diff[t] += 1
        if len(e_in[t]) - diff[t] == 0:
            parent[t] = s
            node.append(t)

print(*parent[1:], sep="\n")


Fixed submission:
from collections import deque
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_from = [[] for _ in range(N+1)]
e_to = [[] for _ in range(N+1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_from[a].append(b)
    e_to[b].append(a)


add = np.zeros(N+1, dtype=int)
rank = [-1]*(N+1)

# まず根を探す
for i in range(1, N+1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i

# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)

print(*rank[1:], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_from = [set([]) for _ in range(N+1)]
e_to = [set([]) for _ in range(N+1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_from[a].add(b)
    e_to[b].add(a)


rank = [-1] * (N + 1)  # 親のノード番号
seen = set([])  # 既に確定したノード

# まず根を探す
for i in range(1, N+1):
    if len(e_to[i]) == 0:
        root = i
        break

# BFS
seen.add(root)
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    for t in e_from[s]:
        if len(e_to[t] - seen) == 0:
            rank[t] = s
            seen.add(t)
            node.append(t)

print(*rank[1:], sep="\n")


Fixed submission:
from collections import deque
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_from = [[] for _ in range(N+1)]
e_to = [[] for _ in range(N+1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_from[a].append(b)
    e_to[b].append(a)


add = np.zeros(N+1, dtype=int)
rank = [-1]*(N+1)

# まず根を探す
for i in range(1, N+1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i

# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)

print(*rank[1:], sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_from = [set([]) for _ in range(N+1)]
e_to = [set([]) for _ in range(N+1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_from[a].add(b)
    e_to[b].add(a)


rank = [-1] * (N + 1)  # 親のノード番号
seen = set([])  # 既に確定したノード

# まず根を探す
for i in range(1, N+1):
    if len(e_to[i]) == 0:
        root = i
        break

# BFS
seen.add(root)
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    for t in e_from[s]:
        if len(e_to[t] - seen) == 0:
            rank[t] = s
            seen.add(t)
            node.append(t)

print(*rank[1:], sep="\n")


Fixed submission:
from collections import deque
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
e_from = [[] for _ in range(N+1)]
e_to = [[] for _ in range(N+1)]
for _ in range(N + M - 1):
    a, b = map(int, input().split())
    e_from[a].append(b)
    e_to[b].append(a)


add = np.zeros(N+1, dtype=int)
rank = [-1]*(N+1)

# まず根を探す
for i in range(1, N+1):
    add[i] = len(e_to[i])
    if len(e_to[i]) == 0:
        root = i

# BFS
rank[root] = 0
node = deque([root])
while node:
    s = node.popleft()
    add[e_from[s]] -= 1
    for t in e_from[s]:
        if add[t] == 0:
            rank[t] = s
            node.append(t)

print(*rank[1:], sep="\n")