Buggy python submission (verdict: Time Limit Exceeded):
# another sol
import heapq

def main():
    n, m = map(int, input().split())
    G = [[] for i in range(n+1)]
    Edge = []
    is_root = [True]*(n+1)
    
    for _ in range(n+m-1):
        x, y = map(int, input().split())
        G[x].append(y)
        Edge.append((x, y))
        is_root[y] = False

    #determine root
    root = -1
    for i in range(1, n+1):
        if is_root[i] == True:
            root = i
            break

    que = [(0, root)]
    heapq.heapify(que)
    #dijkstra
    dis = [0]*(n+1)
    while que:
        cost, node = heapq.heappop(que)
        if dis[node] < cost:continue        
        for c in G[node]:
            if dis[c] > cost - 1:
                dis[c] = cost - 1
                heapq.heappush(que,(cost - 1, c))

    par = [0]*(n+1)

    for x, y in Edge:
        if dis[y] + 1 == dis[x]:
            par[y] = x

    for i in range(1, n+1):
        print(par[i])

    return


if __name__ == "__main__":
    main()




Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# restore the tree


from collections import deque
n, m = map(int, input().split())


in_node = [0 for i in range(n + 1)]

# in_node[x]=xに入るノードの数

parent_node = [0 for i in range(n + 1)]

# parent[i]=ねつき木における頂点iの親を返す

in_graph = {i: [] for i in range(1, n + 1)}

out_graph = {i: [] for i in range(1, n + 1)}

# graph[i]=頂点iに入るノードの集合リスト

# out_graph[i]=頂点iから出るノードの集合リスト

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

root = -1


for i in range(1, n + 1):
    if in_node[i] == 0:
        parent = i
        break



flag = True
d = deque()
d.append(parent)
while d:
    x = d.popleft()
    not_yet = False
    for node in out_graph[x]:
        in_node[node] -= 1
        if in_node[node] == 0 and parent_node[node] == 0:
            parent_node[node] = x
            d.append(node)
            not_yet = True

    if not_yet:
        flag = True
    else:
        flag = False

for i in range(1,n+1):
    print(parent_node[i])

Buggy python submission (verdict: Time Limit Exceeded):
## Atcoder problem solving


# type tree
import heapq


def main():
    n, m = map(int, input().split())
    G = [[] for i in range(n+1)]
    Edge = []
    is_root = [True]*(n+1)
    
    for _ in range(n+m-1):
        x, y = map(int, input().split())
        G[x].append(y)
        Edge.append((x, y))
        is_root[y] = False

    #determine root
    root = -1
    for i in range(1, n+1):
        if is_root[i] == True:
            root = i
            break

    que = [(0, root)]
    heapq.heapify(que)

    #dijkstra
    dis = [10**10]*(n+1)
    dis[root] = 0
    while que:

        cost, node = heapq.heappop(que)
        for c in G[node]:
            if dis[c] > cost - 1:
                dis[c] = cost - 1
                heapq.heappush(que,(cost - 1, c))


    par = [0]*(n+1)

    for x, y in Edge:
        if dis[y] + 1 == dis[x]:
            par[y] = x

    for i in range(1, n+1):
        print(par[i])

    return


if __name__ == "__main__":
    main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# restore the tree


from collections import deque
n, m = map(int, input().split())


in_node = [0 for i in range(n + 1)]

# in_node[x]=xに入るノードの数

parent_node = [0 for i in range(n + 1)]

# parent[i]=ねつき木における頂点iの親を返す

in_graph = {i: [] for i in range(1, n + 1)}

out_graph = {i: [] for i in range(1, n + 1)}

# graph[i]=頂点iに入るノードの集合リスト

# out_graph[i]=頂点iから出るノードの集合リスト

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

root = -1


for i in range(1, n + 1):
    if in_node[i] == 0:
        parent = i
        break



flag = True
d = deque()
d.append(parent)
while d:
    x = d.popleft()
    not_yet = False
    for node in out_graph[x]:
        in_node[node] -= 1
        if in_node[node] == 0 and parent_node[node] == 0:
            parent_node[node] = x
            d.append(node)
            not_yet = True

    if not_yet:
        flag = True
    else:
        flag = False

for i in range(1,n+1):
    print(parent_node[i])

Buggy python submission (verdict: Time Limit Exceeded):
## Atcoder problem solving


# type tree
import heapq


def main():
    n, m = map(int, input().split())
    G = [[] for i in range(n+1)]
    Edge = []
    is_root = [True]*(n+1)
    
    for _ in range(n+m-1):
        x, y = map(int, input().split())
        G[x].append(y)
        Edge.append((x, y))
        is_root[y] = False

    #determine root
    root = -1
    for i in range(1, n+1):
        if is_root[i] == True:
            root = i
            break

    que = [(0, root)]
    heapq.heapify(que)

    #dijkstra
    dis = [10**10]*(n+1)
    dis[root] = 0
    while que:

        cost, node = heapq.heappop(que)
        for c in G[node]:
            if dis[c] > cost - 1:
                dis[c] = cost - 1
                heapq.heappush(que,(cost - 1, c))


    par = [0]*(n+1)

    for x, y in Edge:
        if dis[y] + 1 == dis[x]:
            par[y] = x

    for i in range(1, n+1):
        print(par[i])

    return


if __name__ == "__main__":
    main()



Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# restore the tree


from collections import deque
n, m = map(int, input().split())


in_node = [0 for i in range(n + 1)]

# in_node[x]=xに入るノードの数

parent_node = [0 for i in range(n + 1)]

# parent[i]=ねつき木における頂点iの親を返す

in_graph = {i: [] for i in range(1, n + 1)}

out_graph = {i: [] for i in range(1, n + 1)}

# graph[i]=頂点iに入るノードの集合リスト

# out_graph[i]=頂点iから出るノードの集合リスト

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

root = -1


for i in range(1, n + 1):
    if in_node[i] == 0:
        parent = i
        break



flag = True
d = deque()
d.append(parent)
while d:
    x = d.popleft()
    not_yet = False
    for node in out_graph[x]:
        in_node[node] -= 1
        if in_node[node] == 0 and parent_node[node] == 0:
            parent_node[node] = x
            d.append(node)
            not_yet = True

    if not_yet:
        flag = True
    else:
        flag = False

for i in range(1,n+1):
    print(parent_node[i])