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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		// 入力フェーズ
		string[] input = Console.ReadLine().Split(' ');
        int N = int.Parse(input[0]);
        int M = int.Parse(input[1]);

        // 辺の情報を取得して配列にする
        List<List<int> > graph = createGraph(N, M);

        // 各辺について、削除できるか検証する
        // 削除可能→子ノードを0に更新
        deleteBars(ref graph, M);

        //Console.WriteLine(" AAAA");
        // graphから出力用の表記を生成する
        int[] ret = output(graph);
        //Console.WriteLine(" BBBB");
        //出力
        for (int i = 0; i < N; i++)
        {
            Console.WriteLine(ret[i]);
        }
    }

    ///Ai→Biの表記から、各頂点と子供を管理するタイプの表記にする
    /// return List<int[]> ret
    /// ret[i] は i+1のノードから伸びている子供の番号を配列にして格納する
    static private List<List<int> > createGraph(int N, int M)
    {
        List<List<int>> graph = new List<List<int> >();

        // graphをN行にする
        for (int i = 0; i < N; i++)
        {
            List<int> arr = new List<int>();
            graph.Add(arr);
        }

        for (int i = 0; i < N-1+M; i++)
        {
            string[] ab = Console.ReadLine().Split(' ');
            int a = int.Parse(ab[0]); // 親 1→0番目に格納する
            int b = int.Parse(ab[1]); // 子
            //Console.WriteLine(a + " " + b + " " + bars.GetLength(1));
            graph[a - 1].Add(b);
        }
        //Console.WriteLine(graph.Count);
        return graph;
    }

    /// たとえば1→2という有向線があったとき、この線を使わずに1から2にたどり着ければ、
    /// 1→2は削除できるので削除する
    /// というのを全ての辺、またはM本削除するまで繰り返す
    static private void deleteBars(ref List<List<int>> graph, int M)
    {
				// graphをコピーすればいいじゃん
				List<List<int>> accessibleList = new List<List<int>>(graph);

        for (int i = 0; i < graph.Count; i++)
        {
            if (M == 0)
            {
                // 検証終わり
                break;
            }
            List<int> child = graph[i];
            for (int j = 0; j < child.Count; j++)
            {
                if (M == 0)
                {
                    // 検証終わり
                    break;
                }
                // i 親ノード番号
                // child[j] 子ノード番号（今回検証する）
                //Console.WriteLine("検証 " + i + " " + child[j]);
                if(isEnableDetour(graph, i, child[j], true, accessibleList[i]))
                {
                    //Console.WriteLine("デリート " + i + " " + child[j]);
                    graph[i][j] = 0; // デリートすると順番ずれるしめんどい
                    M--;
                }
            }
        }
    }

    /// graphを使って、iからchild[j]まで、i→child[j]を使わずに迂回できたら削除できる(true)
    /// isTop = trueのときだけゴールじゃない

    static private bool isEnableDetour(List<List<int>> graph, int start, int goal, bool isTop, List<int> acList)
    {
        if (!isTop && (graph[start].Contains(goal) || acList.Contains(goal)))
        {
            // 一回目じゃないときで、startノードの子にgoalノードがあれば迂回成功
						return true;
        }
        else
        {
            List<int> child = graph[start];
            // 一回目のときは、goal以外のノードから迂回ルートを探すようにする
            for (int i = 0; i < child.Count; i++)
            {
								acList.Add(child[i]);
                if (isTop && child[i] == goal) continue;
                //Console.WriteLine("検証2 " + (child[i] - 1) + " " + goal);

                if ((child[i] - 1) >= 0 && isEnableDetour(graph, child[i] - 1, goal, false, acList)) // 親ノード番号にするときは添え字にあわせる
                {
                    return true;
                }
            }
        }
        // 葉ノードに到達しちゃったら迂回失敗を返す
        return false;
    }

    /// graph表記から、出力できる形に変形する
    static private int[] output(List<List<int>> graph)
    {
        int N = graph.Count;
        int[] ret = new int[N];

        //Console.WriteLine(N);
        for (int i = 0; i < N; i++)
        {
            //Console.WriteLine(i + " i" );
            List<int> child = graph[i];
            //Console.WriteLine(child.Count+ " c");
            for (int j = 0; j < child.Count; j++)
            {
                int node = child[j]; // 子ノード, i=親ノード番号
                //Console.WriteLine(node + " node");
                if (node == 0) continue; // 0は削除済み
                ret[node - 1] = i + 1;
            }
        }
        return ret;
    }
}