Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
def bfs(root):
    q=deque([root])
    visited=[(0,0) for _ in range(n)]
    visited[root]=(0,-1)
    while q:
        p=q.popleft()
        dp,pp=visited[p]
        for c in edge[p]:
            dc,cp=visited[c]
            if dc<dp+1:
                visited[c]=(dp+1,p)
                q.append(c)
    return visited
visited=bfs(root)
#print(visited)
for d,i in visited:
    print(i+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
edgerev=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edgerev[b].append(a)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
visited=[False]*n
to=[] #topological order
def dfs(root):
    if visited[root]:
        return
    visited[root]=True
    for i in edge[root]:
        dfs(i)
    to.append(root)
dfs(root)
to=to[::-1]
#print(to)
result=[0]*n
for i in range(n):
    result[to[i]]=i #頂点の順番
#print(result)
#print(edgerev)
for i in range(n):
    if not edgerev[i]:
        print(0)
        continue
    order=-1
    for j in edgerev[i]:
        if order<result[j]:
            ans=j
            order=result[j]
    print(ans+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
def bfs(root):
    q=deque([root])
    visited=[[0,0] for _ in range(n)]
    visited[root]=[0,-1]
    while q:
        p=q.popleft()
        dp,pp=visited[p]
        for c in edge[p]:
            dc,cp=visited[c]
            if dc<dp+1:
                visited[c]=[dp+1,p]
                q.append(c)
    return visited
visited=bfs(root)
#print(visited)
for d,i in visited:
    print(i+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
edgerev=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edgerev[b].append(a)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
visited=[False]*n
to=[] #topological order
def dfs(root):
    if visited[root]:
        return
    visited[root]=True
    for i in edge[root]:
        dfs(i)
    to.append(root)
dfs(root)
to=to[::-1]
#print(to)
result=[0]*n
for i in range(n):
    result[to[i]]=i #頂点の順番
#print(result)
#print(edgerev)
for i in range(n):
    if not edgerev[i]:
        print(0)
        continue
    order=-1
    for j in edgerev[i]:
        if order<result[j]:
            ans=j
            order=result[j]
    print(ans+1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
def bfs(root):
    q=deque([root])
    visited=[[0,0] for _ in range(n)]
    visited[root]=[0,-1]
    while q:
        p=q.popleft()
        dp,pp=visited[p]
        for c in edge[p]:
            dc,cp=visited[c]
            if dc<dp+1:
                visited[c]=[dp+1,p]
                q.append(c)
    return visited
visited=bfs(root)
#print(visited)
for d,i in visited:
    print(i+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input=sys.stdin.readline
n,m=map(int,input().split())
s=set()
edge=[[] for _ in range(n)]
edgerev=[[] for _ in range(n)]
for _ in range(n+m-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edgerev[b].append(a)
    s.add(b)
for i in range(n):
    if i not in s:
        root=i
        break
visited=[False]*n
to=[] #topological order
def dfs(root):
    if visited[root]:
        return
    visited[root]=True
    for i in edge[root]:
        dfs(i)
    to.append(root)
dfs(root)
to=to[::-1]
#print(to)
result=[0]*n
for i in range(n):
    result[to[i]]=i #頂点の順番
#print(result)
#print(edgerev)
for i in range(n):
    if not edgerev[i]:
        print(0)
        continue
    order=-1
    for j in edgerev[i]:
        if order<result[j]:
            ans=j
            order=result[j]
    print(ans+1)