Problem:
Score : 500 points

Problem Statement

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.

Notes

For "tree" and other related terms in graph theory, see the article in Wikipedia, for example.

Constraints

3 \leq N

1 \leq M

N + M \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

If i \neq j, (A_i, B_i) \neq (A_j, B_j).

The graph in input can be obtained by adding M edges satisfying the condition in the problem statement to a rooted tree with N vertices.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_{N-1+M} B_{N-1+M}

Output

Print N lines.
In the i-th line, print 0 if Vertex i is the root of the original tree, and otherwise print the integer representing the parent of Vertex i in the original tree.

Note that it can be shown that the original tree is uniquely determined.

Sample Input 1

3 1
1 2
1 3
2 3

Sample Output 1

0
1
2

The graph in this input is shown below:

It can be seen that this graph is obtained by adding the edge 1 \rightarrow 3 to the rooted tree 1 \rightarrow 2 \rightarrow 3.

Sample Input 2

6 3
2 1
2 3
4 1
4 2
6 1
2 6
4 6
6 5

Sample Output 2

6
4
2
0
6
2

Solution in Python (3.4.3) (judge verdict: Runtime Error)

# Python program to print topological sorting of a DAG
# from collections import defaultdict

i = [int(each) for each in input().split()]
n, m = i[0], i[1]

edges = []
for i in range(n - 1 + m):
    st = [int(each) - 1 for each in input().split()]
    edges.append((st[0], st[1]))


# Class to represent a graph
class Graph:
    def __init__(self, vertices):
        self.graph = dict()  # dictionary containing adjacency List
        self.V = vertices  # No. of vertices

    # # function to add an edge to graph
    # def add_edge(self, u: int, v: int):
    #     """
    #     Add edge
    #
    #     :param u: parent
    #     :param v: child
    #     :return:  None
    #     """
    #     self.graph[str(u)].append(v)

    # function to add an edge to graph
    def add_edge(self, t):
        """
        Add edge

        :param t: edge()
        :return: None
        """
        a = self.graph.get(str(t[0]), [])
        a.append(t[1])
        self.graph[str(t[0])] = a

    def topological_sort(self):
        result = []
        visited = [False] * self.V

        for node in range(self.V):
            self.visit(node, visited, result)
        return result

    def visit(self, node, visited, result):
        if not visited[node]:
            visited[node] = True
            for out_node in self.graph.get(str(node), []):
                self.visit(out_node, visited, result)
            result.insert(0, node)

    def show_graph(self):
        for key in range(self.V):
            print('key', key)
            for value in self.graph.get(str(key), []):
                print('\t -> ', value)

    def is_parent(self, parent, child):
        return child in self.graph.get(str(parent), [])

    def get_root(self) -> int:
        result = [False] * self.V
        for node in range(self.V):
            for dist in self.graph.get(str(node), []):
                result[dist] = True
        for i, each in enumerate(result):
            if each is False:
                return i
        raise Exception


g = Graph(n)
for edge in edges:
    g.add_edge(edge)

# sorted_nodes = g.topological_sort()
# g.show_graph()

# for i in range(n):
#     node_i_index_in_sorted_nodes = sorted_nodes.index(i)
#     if node_i_index_in_sorted_nodes == 0:
#         print(0)
#     else:
#         search_target_nodes = sorted_nodes[:node_i_index_in_sorted_nodes]
#         search_target_nodes.reverse()
#         for searching_node in search_target_nodes:
#             if g.is_parent(searching_node, i):
#                 print(searching_node + 1)
#                 break

# 深さ探索

# 根を探索
root = g.get_root()  # type: int

current_node = root

visited = [False] * n
result_list = [None] * n  # type: List[int]
result_list[root] = 0
depth_list = [-1] * n  # type: List[int]
depth_list[root] = 0


def get_child(parent, depth):
    childs = g.graph.get(str(parent), [])

    for child in childs:
        if depth_list[child] < depth:
            depth_list[child] = depth
            result_list[child] = parent + 1
        get_child(child, depth + 1)

get_child(root, 0)
for result in result_list:
    print(result)