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 std::collections::HashSet;
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read_option<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok()
}

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let opt = read_option();
    opt.expect("failed to parse token")
}

fn main() {
    let n: usize = read();
    let m: usize = read();
    let mut vec: Vec<HashSet<usize>> = vec![HashSet::new(); n];
    for _ in 0..m {
        let x: usize = read();
        let y: usize = read();
        vec[x - 1].insert(y - 1);
    }
    let mut incoming_degrees = vec![Some(0); n];
    for set in &vec {
        for s in set {
            incoming_degrees[*s] = Some(incoming_degrees[*s].unwrap() + 1);
        }
    }
    let mut sorted = vec![];
    let mut zero_set = vec![];
    for (i, v) in incoming_degrees.clone().iter().enumerate() {
        if *v == Some(0) {
            zero_set.push(i);
            incoming_degrees[i] = None;
        }
    }
    // 多分cloneのせいでTLEになってる。n^2でも計算量的には間に合わない。正しくトポロジカルソートを実装しよう
    // 入次数が0のものを数えるのに毎回全部見る必要はなくて、初回だけ全部みてあとはソートしたものから出てるものだけ見れば良い
    while !zero_set.is_empty() {
        let s = zero_set.pop().unwrap();
        sorted.push(s);
        for &v in &vec[s] {
            if incoming_degrees[v].is_none() {
                continue;
            }
            let incoming_count = std::cmp::max(0, incoming_degrees[v].unwrap() - 1);
            incoming_degrees[v] = Some(incoming_count);
            if incoming_count == 0 {
                zero_set.push(v);
            }
        }
    }
    let mut dp = vec![0; n];
    for v in sorted {
        for &v2 in &vec[v] {
            dp[v2] = std::cmp::max(dp[v2], dp[v] + 1);
        }
    }
    println!("{}", dp.iter().max().unwrap());
}