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++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <math.h>
#include <map>
#include <ios>     // std::left, std::right
#include <iomanip> // std::setw(int), std::setfill(char)
#include <queue>
using namespace std;


int main(){
  int N,M;
  cin>>N>>M;
  int a,b;
  vector<vector<int>> gh;
  vector<vector<int>> gh_n;
  gh.resize(N);
  gh_n.resize(N);
  //入次数の管理
  int h[N]={};
  //トポロジカルソート の実装
  for(int i=0;i<N+M-1;i++){
    cin>>a>>b;
    //aからbへの有向グラフ
    gh[a-1].push_back(b-1);
    gh_n[b-1].push_back(a-1);
    h[b-1]+=1;
  }
  //入次数0の要素をキューとして管理
  queue<int> q;
  for(int i=0;i<N;i++){
    if(h[i]==0){
      q.push(i);
    }
  }

  vector<int> ans;
  int tmp,tmp_2;
  while(q.size()){
    int i=q.front();
    q.pop();
    ans.push_back(i);
    //隣接する頂点の入次数を-1する
    for(int j=0;j<gh[i].size();j++){
      tmp=gh[i][j];
      h[tmp]-=1;
      if(h[tmp]==0){
        q.push(tmp);
      }
    }
  }

  int judge=0;
  int result[N]={};
  result[ans[0]]=(-1);

  //順番に親を出力していく。親は入次してくる者のうち、もっともトポロジカルソートで下位の者
  for(int i=1;i<N;i++){
    tmp=ans[i];
    //tmpについて親を調べる
    for(int k=i-1;k>=0;k--){
      tmp_2=ans[k];
      //tmp2が親たりうるかしらベル
      judge=0;
    for(int j=0;j<gh_n[tmp].size();j++){
      if(gh_n[tmp][j]==tmp_2){
        judge=1;
      }
    }
    if(judge==1){
      result[tmp]=tmp_2;
      break;
    }
  }
  }

  for(int i=0;i<N;i++){
    cout<<result[i]+1<<"\n";
  }
return 0;
}