Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Topological Sort

A directed acyclic graph (DAG) can be used to represent the ordering of tasks. Tasks are represented by vertices and constraints where one task can begin before another, are represented by edges. For example, in the above example, you can undertake task B after both task A and task B are finished. You can obtain the proper sequence of all the tasks by a topological sort.

Given a DAG $G$, print the order of vertices after the topological sort.

Input

A directed graph $G$ is given in the following format:

$|V|\;|E|$

$s_0 \; t_0$

$s_1 \; t_1$

:

$s_{|E|-1} \; t_{|E|-1}$

$|V|$ is the number of vertices and $|E|$ is the number of edges in the graph. The graph vertices are named with the numbers $0, 1,..., |V|-1$ respectively.

$s_i$ and $t_i$ represent source and target nodes of $i$-th edge (directed).

Output

Print the vertices numbers in order. Print a number in a line.

If there are multiple possible solutions, print any one of them (the solution is judged by a special validator).

Constraints

$1 \leq |V| \leq 10,000$

$0 \leq |E| \leq 100,000$

There are no parallel edges in $G$

There are no self loops in $G$

Sample Input

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

Sample Output

0
3
1
4
5
2

Solution in Rust (judge verdict: Accepted)

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

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let v = scan.read::<usize>();
    let e = scan.read::<usize>();
    let mut g = vec![vec![];v];
    for _ in 0..e {
        let s = scan.read::<usize>();
        let t = scan.read::<usize>();
        g[s].push(t);
    }
    let mut dag = Dag::new(&g);
    dag.tsort();
    for i in 0..v { println!("{}", dag.vs[i]);}
}

struct Dag {
    g: Vec<Vec<usize>>,
    used: Vec<bool>,
    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(), used: vec![false; n], vs: vec![]}
    }

    fn dfs(&mut self, v: usize) {
        self.used[v] = true;
        for m in self.g[v].clone() {
            if !self.used[m] { self.dfs(m) }
        }
        self.vs.push(v);
    }

    fn tsort(&mut self) {
        for i in 0..self.n { if !self.used[i] { self.dfs(i) } }
        self.vs.reverse();
    }

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

    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] = max(dp[v], dp[p] + 1);
            }
        }
        dp.into_iter().max().unwrap()
    }
}

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

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

#[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> {
        let mut ret = vec![];
        for _ in 0..n { ret.push(self.read::<T>()) }
        ret
    }

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