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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"strconv"
	"math"
)

var INF = math.MaxInt32
var INF64 = math.MaxInt64

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func readLineNums(bf *bufio.Reader) []int {
	line, _ := bf.ReadString('\n')
	line = strings.Trim(line, "\n")
	fields := strings.Split(line, " ")
	nums := []int{}
	for _, field := range fields {
		num, _ := strconv.Atoi(field)
		nums = append(nums, num)
	}
	return nums
}

func readLineFloats(bf *bufio.Reader) []float64 {
	line, _ := bf.ReadString('\n')
	line = strings.Trim(line, "\n")
	fields := strings.Split(line, " ")
	nums := []float64{}
	for _, field := range fields {
		num, _ := strconv.ParseFloat(field, 64)
		nums = append(nums, num)
	}
	return nums
}

func readLineStrs(bf *bufio.Reader) []string {
	line, _ := bf.ReadString('\n')
	line = strings.Trim(line, "\n")
	return strings.Split(line, " ")
}

func dfs(v int, indegrees []int, dists []int, edges map[int][]int) {
	for _, nv := range edges[v] {
		dists[nv] = max(dists[nv], dists[v]+1)
		indegrees[nv]--
		if indegrees[nv] == 0 {
			dfs(nv, indegrees, dists, edges)
		}
	}
}

func main() {
	bf := bufio.NewReader(os.Stdin)
	nm := readLineNums(bf)
	N, M := nm[0], nm[1]
	indegrees := make([]int, N+1) // 每个节点的入度
	dists := make([]int, N+1) // 开始节点(虚拟节点)到每个节点距离
	edges := make(map[int][]int)
	for i:=0; i<M; i++ {
		ft := readLineNums(bf)
		if _, ok := edges[ft[0]]; !ok {
			edges[ft[0]] = make([]int, 0)
		}
		edges[ft[0]] = append(edges[ft[0]], ft[1])
		indegrees[ft[1]]++
	}

	for i:=1; i<=N; i++ {
		if indegrees[i] == 0 {
			dfs(i, indegrees, dists, edges)
		}
	}

	ans := 0
	for i:=1; i<=N; i++ {
		ans = max(ans, dists[i])
	}
	fmt.Println(ans)
}