Problem:
Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:

Solution in Rust (1.15.1) (judge verdict: Accepted)

use io::*;
use std::*;

fn solve(N: i64, M: i64, x: Vec<i64>, y: Vec<i64>) {
    let mut map: collections::HashMap<usize, Vec<usize>> = collections::HashMap::new();
    for (xval, yval) in x.iter().zip(y.iter()) {
        if map.get(&(*xval as usize)).is_some() {
            map.get_mut(&(*xval as usize)).unwrap().push(*yval as usize);
        }
        else {
            map.insert(*xval as usize, vec![*yval as usize]);
        }
    }
    // println!("{:?}", map);
    let mut dp = vec![0; N as usize + 1];
    fn topo(curr: usize, map: &collections::HashMap<usize, Vec<usize>>, dp: &mut Vec<usize>) -> usize {
        if dp[curr] != 0 {
            return dp[curr];
        } else {
            if map.get(&curr).is_some() {
                for &n in map.get(&curr).unwrap().iter() {
                    dp[curr] = cmp::max(dp[curr], topo(n, map, dp) + 1);
                }
                return dp[curr];
            } else {
                return 0;
            }
        }
    }
    for i in (1..dp.len()) {
        if dp[i] == 0 {
            topo(i, &map, &mut dp);
        }
    }
    let mut res = (0usize, 0usize);
    for (idx, value) in dp.iter().enumerate() {
        if *value > res.1 {
            res.1 = *value;
            res.0 = idx;
        }
    }
    println!("{}", res.1);
}

// Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
fn main() {
    let con = read_string();
    let mut scanner = Scanner::new(&con);
    let mut N: i64;
    N = scanner.next();
    let mut M: i64;
    M = scanner.next();
    let mut x: Vec<i64> = vec![0i64; (M) as usize];
    let mut y: Vec<i64> = vec![0i64; (M) as usize];
    for i in 0..(M) as usize {
        x[i] = scanner.next();
        y[i] = scanner.next();
    }
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(move || solve(N, M, x, y)).unwrap().join().unwrap();
}

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

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

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}