#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a used,cmp,vs; vector> ori; vector> v,rv; StronglyConnectedComponents(int n):used(n),cmp(n,-1),v(n),rv(n){} void add_edge(int x,int y){ v[x].push_back(y); rv[y].push_back(x); } void dfs(int x){ used[x]=true; for(auto to:v[x]) if(!used[to]) dfs(to); vs.push_back(x); } void rdfs(int x,int k){ cmp[x]=k; for(auto to:rv[x]) if(cmp[to]==-1) rdfs(to,k); } int scc(){ for(int i=0;i> &g){ g.resize(k); for(int i=0;i> g; vector vis; vector dp; vector B,C; void dfs(int now){ if(vis[now]) return; for(auto nex:g[now]){ dfs(nex); chmax(dp[now],dp[nex]); } vis[now]=1; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N,M; cin>>N>>M; B.resize(M); C.resize(M); rep(i,M) cin>>B[i]>>C[i]; vector v; rep(i,M) v.push_back(B[i]); rep(i,M) v.push_back(C[i]); sort(all(v)); v.erase(unique(all(v)),v.end()); int V=v.size(); map mp; rep(i,V) mp[v[i]]=i; StronglyConnectedComponents scc(V); rep(i,M){ scc.add_edge(mp[B[i]],mp[C[i]]); } int S=scc.scc(); scc.build(S,g); dp.resize(S,0); for(int i=0;i