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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define		int 			long long int
#define		pb				push_back
#define		fi(n)			for(int i=0;i<n;i++)
#define		fj(n)			for(int j=0;j<n;j++)
#define		fi1(n)			for(int i=1;i<n;i++)
#define		fj1(n)			for(int j=1;j<n;j++)
#define		firev(n)		for(int i=n-1;i>=0;i--)
#define		fjrev(n)		for(int j=n-1;j>=0;j--)
#define		rep(z,a,n)		for(z=a;z<n;z++)
#define		rep2(z,a,n)		for(z=a;z<n;z+=2)
#define		clr(x)			memset(x, 0, sizeof(x))
#define		print(x,n)		for(int i=0;i<n;i++) cout<<x[i]<<" ";  cout<<"\n"
#define		nl 				"\n"
#define		vi				vector<int>
#define		pairi			pair<int,int>
#define		mp				make_pair
#define		vpairi			vector<pair<int,int>>
#define		ff				first
#define		ss				second
#define		inf 			2e18
#define		setp(val,n)		cout<<fixed;cout<<setprecision(n)<<val<<nl;
#define 	input 			"C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//input.txt"
#define 	output 			"C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//output.txt"
#define 	error 			"C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//error.txt"
int ctoi(char a){ int x=a-48;return x;}
string itos(int a){
	string out_string;
	stringstream ss;
	ss<<a;
	out_string=ss.str();
	return out_string;
}
char itoc(int a){ return itos(a)[0];}
int pow2(int e,int x){
	int ans=1;
	while(x>0){
		if(x&1) ans*=e;
		e*=e;
		x>>=1;
		if(ans>1000000000000) return -1;
	}return ans;
}
string bin(int x){
	bitset<sizeof(1)*CHAR_BIT> bits(x);
	// bitset<31> bits(x);
	string b=bits.to_string(); return b;
}
const int nax=1e5+5;
int indegree[nax];
vi adj[nax];
bool visited[nax];
int dis[nax];

void dfs(int a){
	visited[a]=true;
	for(auto x:adj[a]){
		dis[x]=max(dis[x],dis[a]+1);
		indegree[x]--;
		if(indegree[x]==0){
			dfs(x);
		}
	}
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	#ifndef ONLINE_JUDGE
	#endif
	int T=1;
	// cin>>T;
	while(T--){
		int n,m;
		cin>>n>>m;
		fi(m){
			int u,v;cin>>u>>v;
			u--;v--;
			adj[u].pb(v);
			indegree[v]++;
		}
		fi(n){
			if(!visited[i] && indegree[i]==0)
				dfs(i);
		}
		int ans=0;
		fi(n) ans=max(ans,dis[i]);
		cout<<ans<<nl;
	}
	return 0;
}