Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
input = sys.stdin.readline
n, m = map(int, input().split())
edges = [[] for i in range(n)]
edges_r = [[] for i in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges_r[b].append(a)
s = 0
for i,e in enumerate(edges_r):
    if len(e) == 0:
        s = i
        break

INF = 1 << 60
ks = [-1] * n
parents = [-1] * n
q = deque([s])
ks[s] = 0
# 同じ深さのもの同士をつなぐのはありえないので
# 根の部分から徐々に掘り下げながら、真の親を探す
while len(q) > 0:
    u = q.popleft()
    for v in edges[u]:
        if ks[u] >= ks[v]:
            ks[v] = ks[u] + 1
            parents[v] = u
            q.appendleft(v)

for p in parents:
    print(p+1)

Fixed submission:
"""
子孫に向かって有向辺が伸びる
⇨ ループなし
⇨ トポロジカルソート通りに並べると親がわかる
"""
from collections import deque
from collections import defaultdict
n, m = map(int, input().split())

edges = [[] for _ in range(n)]
ps = [[] for _ in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    ps[b].append(a)

def topo(G):
    cut_in = defaultdict(int)
    for vs in G:
        for v in vs:
            cut_in[v] += 1
    res = []
    # 入次数が0のものを洗い出し
    zeros = [i for i in range(n) if cut_in[i] == 0]
    q = deque(zeros)
    # すべての入次数が0になるまでくりかえす
    while len(q) > 0:
        v = q.popleft()
        res.append(v)
        for u in G[v]:
            cut_in[u] -= 1
            if cut_in[u] == 0:
                q.append(u)
    return res

ss = topo(edges)
# print(ss,ps)
ss = {s:i for i,s in enumerate(ss)}

for i in range(n):
    mn = -1
    parent = 0
    if len(ps[i]) == 0:
        print(0)
        continue
    for p in ps[i]:
        if mn < ss[p]:
            mn = ss[p]   
            parent = p
    print(parent+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
edges = [[] for i in range(n)]
edges_r = [[] for i in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges_r[b].append(a)
ns = [(len(e),i) for i, e in enumerate(edges_r)]
ns.sort()
s = ns[0][1]

INF = 1 << 60
ks = [-1] * n
parents = [-1] * n
q = deque([s])
ks[s] = 0
# 同じ深さのもの同士をつなぐのはありえないので
# 根の部分から徐々に掘り下げながら、真の親を探す
while len(q) > 0:
    u = q.popleft()
    for v in edges[u]:
        if ks[u] >= ks[v]:
            ks[v] = ks[u] + 1
            parents[v] = u
            q.appendleft(v)

for p in parents:
    print(p+1)

Fixed submission:
"""
子孫に向かって有向辺が伸びる
⇨ ループなし
⇨ トポロジカルソート通りに並べると親がわかる
"""
from collections import deque
from collections import defaultdict
n, m = map(int, input().split())

edges = [[] for _ in range(n)]
ps = [[] for _ in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    ps[b].append(a)

def topo(G):
    cut_in = defaultdict(int)
    for vs in G:
        for v in vs:
            cut_in[v] += 1
    res = []
    # 入次数が0のものを洗い出し
    zeros = [i for i in range(n) if cut_in[i] == 0]
    q = deque(zeros)
    # すべての入次数が0になるまでくりかえす
    while len(q) > 0:
        v = q.popleft()
        res.append(v)
        for u in G[v]:
            cut_in[u] -= 1
            if cut_in[u] == 0:
                q.append(u)
    return res

ss = topo(edges)
# print(ss,ps)
ss = {s:i for i,s in enumerate(ss)}

for i in range(n):
    mn = -1
    parent = 0
    if len(ps[i]) == 0:
        print(0)
        continue
    for p in ps[i]:
        if mn < ss[p]:
            mn = ss[p]   
            parent = p
    print(parent+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
edges = [[] for i in range(n)]
edges_r = [[] for i in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges_r[b].append(a)
ns = [(len(e),i) for i, e in enumerate(edges_r)]
ns.sort()
s = ns[0][1]

INF = 1 << 60
ks = [-1] * n
parents = [-1] * n
q = deque([s])
ks[s] = 0
# 同じ深さのもの同士をつなぐのはありえないので
# 根の部分から徐々に掘り下げながら、真の親を探す
while len(q) > 0:
    u = q.popleft()
    for v in edges[u]:
        if ks[u] >= ks[v]:
            ks[v] = ks[u] + 1
            parents[v] = u
            q.appendleft(v)

for p in parents:
    print(p+1)

Fixed submission:
"""
子孫に向かって有向辺が伸びる
⇨ ループなし
⇨ トポロジカルソート通りに並べると親がわかる
"""
from collections import deque
from collections import defaultdict
n, m = map(int, input().split())

edges = [[] for _ in range(n)]
ps = [[] for _ in range(n)]
for i in range(n+m-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    ps[b].append(a)

def topo(G):
    cut_in = defaultdict(int)
    for vs in G:
        for v in vs:
            cut_in[v] += 1
    res = []
    # 入次数が0のものを洗い出し
    zeros = [i for i in range(n) if cut_in[i] == 0]
    q = deque(zeros)
    # すべての入次数が0になるまでくりかえす
    while len(q) > 0:
        v = q.popleft()
        res.append(v)
        for u in G[v]:
            cut_in[u] -= 1
            if cut_in[u] == 0:
                q.append(u)
    return res

ss = topo(edges)
# print(ss,ps)
ss = {s:i for i,s in enumerate(ss)}

for i in range(n):
    mn = -1
    parent = 0
    if len(ps[i]) == 0:
        print(0)
        continue
    for p in ps[i]:
        if mn < ss[p]:
            mn = ss[p]   
            parent = p
    print(parent+1)