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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let m = scan.read::<usize>();
    let mut g = vec![vec![];n];
    for _ in 0..n + m - 1 {
        let a = scan.read::<usize>()-1;
        let b = scan.read::<usize>()-1;
        g[a].push(b);
    }
    let mut dag = Dag::new(&g);
    dag.tsort();
    let mut parent = vec![0;n];
    for &p in &dag.vs {
        for &v in &g[p] {
            parent[v] = p + 1;
        }
    }
    for i in 0..n {
        println!("{}", parent[i]);
    }
}

struct Dag {
    g: Vec<Vec<usize>>,
    vs: Vec<usize>,
    n: usize
}

#[allow(dead_code)]
impl Dag {
    fn new(g: &Vec<Vec<usize>>) -> Dag {
        let n = g.len();
        Dag {n: n, g: g.clone(), vs: vec![]}
    }

    fn tsort(&mut self) {
        let mut h = vec![0usize;self.n];
        for i in 0..self.n {
            for &v in &self.g[i] {h[v] += 1;}
        }
        let mut stack = vec![];
        for i in 0..self.n {if h[i] == 0 {stack.push(i);}}
        while let Some(i) = stack.pop() {
            self.vs.push(i);
            for &j in &self.g[i] {
                h[j] -= 1;
                if h[j] == 0 {stack.push(j);}
            }
        }
    }

    fn is_acyclic(&self) -> bool {
        self.vs.len() == self.n
    }

    fn is_unique(&self) -> bool {
        for i in 0..self.n - 1 {
            let a = self.vs[i];
            let b = self.vs[i+1];
            let mut f = false;
            for &v in &self.g[a] {
                if v == b {
                    f = true;
                    break;
                }
            }
            if !f {return false;}
        }
        true
    }

    fn longest_path(&self) -> usize {
        let mut dp = vec![0;self.n];
        for &p in &self.vs {
            for &v in &self.g[p] {
                dp[v] = std::cmp::max(dp[v], dp[p] + 1);
            }
        }
        dp.into_iter().max().unwrap()
    }
}

// region template
#[allow(dead_code)]
fn iu(i: i64) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> i64 { i as i64 }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion