Problem:
There is a rooted tree (see Notes) with N vertices numbered 1 to N. Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex 1.  Takahashi has added M new directed edges to this graph. Each of these M edges, u \rightarrow v, extends from some vertex u to its descendant v.  You are given the directed graph with N vertices and N-1+M edges after Takahashi added edges. More specifically, you are given N-1+M pairs of integers, (A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the i-th edge extends from Vertex A_i to Vertex B_i.  Restore the original rooted tree.

Incorrect submission (verdict: Wrong Answer):
# coding:utf-8

import sys
from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


# UnionFind
class UnionFind:
    
    
    def __init__(self, size: int) -> None:
        self.nodes = [-1] * size  

    def get_root(self, x: int) -> int:
        if self.nodes[x] < 0:
            return x
        else:
            self.nodes[x] = self.get_root(self.nodes[x])
            return self.nodes[x]

    def unite(self, x: int, y: int) -> None:
        root_x, root_y = self.get_root(x), self.get_root(y)
        if root_x != root_y:
            if self.nodes[root_x] != self.nodes[root_y]:  
                
                
                if self.nodes[root_x] < self.nodes[root_y]:
                    self.nodes[root_y] = root_x
                else:
                    self.nodes[root_x] = root_y
            else:
                
                
                self.nodes[root_x] += -1
                self.nodes[root_y] = root_x



n, m = LI()
T = defaultdict(list)

for i in range(n - 1 + m):
    a, b = LI_()
    T[b].append(a)

# for key in T.keys():
#     print(key, T[key])

tree = defaultdict(int)
notleaf = set()
check = set()

uf = UnionFind(n)

for i in range(n):
    if i not in T.keys():
        tree[i + 1] = 0
        root = i
        notleaf.add(i)
        continue
    if len(T[i]) == 1:
        uf.unite(i, T[i][0])
        tree[i + 1] = T[i][0] + 1
        notleaf.add(i)

    check.add(i)
    for node in T[i]:
        notleaf.add(node)

# print(tree)
# print(T)


used = set()
for i in range(n):
    if len(T[i]) > 1:
        for j in T[i]:
            if j not in used:
                break
        tree[i + 1] = j + 1
        used.add(j)

    
    # if uf.get_root(i) == i and uf.get_root(root) != i:
    #     print(i + 1, T[i])
    #     for j in T[i]:
    #         if j not in used:
    #             break
    #     print(i, j)
    #     tree[i + 1] = j + 1
    #     used.add(j)

# print(tree)
for i in range(n):
    print(tree[i + 1])


Fixed, accepted submission:
# coding:utf-8

import sys
from collections import deque, defaultdict
from operator import itemgetter

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


# def topological_sort(graph: list, indegree: list):
#     from collections import deque

#     order = []


#         order.append(v)
#         for next in graph[v]:
#             indegree[next] -= 1
#             if indegree[next] == 0:

#
#     return order


n, m = LI()

G = [[] for _ in range(n)]
indegree = [0] * n

for _ in range(n + m - 1):
    a, b = LI_()
    G[a].append(b)
    indegree[b] += 1

que = deque()
for i in range(n):
    if indegree[i] == 0:
        que.append(i)
        break

ans = [0] * n
while que:
    v = que.popleft()
    for v2 in G[v]:
        indegree[v2] -= 1
        if indegree[v2] == 0:
            que.append(v2)
            ans[v2] = v + 1

print(*ans, sep='\n')