atcoder#NIKKEI2019QUALD. Restore the Tree

Restore the Tree

Score : 500500 points

Problem Statement

There is a rooted tree (see Notes) with NN vertices numbered 11 to NN. Each of the vertices, except the root, has a directed edge coming from its parent. Note that the root may not be Vertex 11.

Takahashi has added MM new directed edges to this graph. Each of these MM edges, uvu \rightarrow v, extends from some vertex uu to its descendant vv.

You are given the directed graph with NN vertices and N1+MN-1+M edges after Takahashi added edges. More specifically, you are given N1+MN-1+M pairs of integers, (A1,B1),...,(AN1+M,BN1+M)(A_1, B_1), ..., (A_{N-1+M}, B_{N-1+M}), which represent that the ii-th edge extends from Vertex AiA_i to Vertex BiB_i.

Restore the original rooted tree.

Notes

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

, for example.

Constraints

  • 3N3 \leq N
  • 1M1 \leq M
  • N+M105N + M \leq 10^5
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • AiBiA_i \neq B_i
  • If iji \neq j, (Ai,Bi)(Aj,Bj)(A_i, B_i) \neq (A_j, B_j).
  • The graph in input can be obtained by adding MM edges satisfying the condition in the problem statement to a rooted tree with NN vertices.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1

::

AN1+MA_{N-1+M} BN1+MB_{N-1+M}

Output

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

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

3 1
1 2
1 3
2 3
0
1
2

The graph in this input is shown below:

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

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