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 C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define lli long long int

#define MAX(a,b) (((a)>(b))?(a):(b))

typedef struct s_path
{
	lli start;
	lli end;
	lli len;
} t_path;

int main(void)
{
	lli n, m;

	scanf ("%lld %lld",&n, &m);

	lli x[m],y[m];
	t_path list[m];
	lli index = 0;
	while (index < m)
	{
		scanf("%lld %lld",&(x[index]),&(y[index]));
		list[index].start = x[index];
		list[index].end = y[index];
		list[index].len = 1;
		index++;
	}
	lli dp[n+1][n+1];
	lli i = 0;
	lli j = 0;
	while (i <= n)
	{
		j = 0;
		while (j <= n)
		{
			dp[i][j] = 0;
			j++;
		}
		i++;
	}
	lli max = 0;
	lli p_max = 0;
	i = 1;
	while (i <= m)
	{
		if (dp[x[i-1]][y[i-1]] == 0)
			dp[x[i-1]][y[i-1]] = 1;
		j = 1;
		while (j <= n)
		{
			if(dp[j][x[i-1]] != 0)
				dp[j][x[i-1]]++;
			if(dp[y[i-1]][j] != 0)
				dp[y[i-1]][j]++;
			p_max = MAX(dp[j][x[i-1]],dp[y[i-1]][j]);
			max = MAX(p_max, max);
			j++;
		}
		i++;
	}
	printf("%lld\n",max);
	return (0);
}