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::cmp;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

pub struct Graph(Vec<Vec<usize>>);

impl Graph {
    pub fn from_pairs(n: i32, pairs: Vec<(usize, usize)>) -> Self {
        let mut g = Vec::new();
        for _ in 0..n {
            g.push(Vec::new());
        }

        for (x,y) in pairs {
            g[x].push(y);
        }

        Graph(g)
    }

    fn search_internal(&self, current: usize, answer: &mut Vec<usize>, checked: &mut Vec<bool>) {
        let branches = self.0[current].clone();

        for b in branches {
            if !checked[b] {
                self.search_internal(b, answer, checked);
            }
            answer[current] = cmp::max(answer[b] + 1, answer[current]);
        }

        checked[current] = true;
    }

    pub fn search(self, n: i32) -> usize {
        let mut ans = vec![];
        for _ in 0..n {
            ans.push(0);
        }

        let mut checked = vec![];
        for _ in 0..n {
            checked.push(false);
        }

        for i in 0..n {
            if ans[i as usize] != 0 {
                continue;
            }

            self.search_internal(i as usize, &mut ans, &mut checked);
        }

        ans.into_iter().max().unwrap()
    }
}

fn main() {
    input!{
        n: i32,
        m: i32,
        xys: [(usize, usize); m],
    }

    let graph = Graph::from_pairs(n, xys.into_iter().map(|(x,y)| (x-1,y-1)).collect::<Vec<_>>());

    println!("{:?}", graph.search(n));
}