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: Wrong Answer)

use std::vec::Vec;
use std::cmp;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

fn tp_sort(l: &Vec<Vec<i64>>, h:  &mut Vec<i64>, v: i64) -> Vec<i64> {
  // l: 出力があるノードをリストに持っているベクター
  // h: 各頂点の入力字数を表したベクター
  // v: ノードの数, e: エッジの数
  let mut res: Vec<i64> = Vec::new(); //トポロジカルソートした結果を蓄積するベクター
  let mut s: Vec<i64> = Vec::new(); //入力エッジを持たないノードの集合

  // 入力される辺がない頂点をsに追加
  for i in 0..v as usize {
    if h[i] == 0 {
      s.push(i as i64);
    }
  }

  while !s.is_empty() {
    let n = match s.pop() {
      Some(n) => n,
      None => 0,
    };

    res.push(n);

    for i in &l[n as usize] {
      h[*i as usize] -= 1;

      if h[*i as usize] == 0 {
        s.push(*i);
      }
    }

  }

  return res;
}

fn main() {
    let (n, m) = {
        let v: Vec<usize> = read_vec();
        (v[0], v[1])
    };

    let mut l: Vec<Vec<i64>> = vec![Vec::new(); n];
    let mut h: Vec<i64> = vec![0; n];
    for _ in 0..m {
        let x: Vec<usize> = read_vec();
        l[x[0]-1].push(x[1] as i64 - 1); //x[0]からx[1]にエッジが出力されている
        h[x[1]-1] += 1;
    }

    // トポロジカルソートされた結果のベクター
    let nodes: Vec<i64> = tp_sort(&l, &mut h, n as i64);
    // 各頂点の最大有向パス長を保存する配列
    let mut list: Vec<i64> = vec![0; n];
    // 答えの最大有向パス長を保存する変数
    let mut res: i64 = 0;

    for n in nodes {
      for en in &l[n as usize] {
        list[*en as usize] = list[n as usize] + 1;
        res = cmp::max(res, list[*en as usize]);
      }
    }

    println!("{}", res);
}