#include <cstring> #include <cctype> #include <cstdio> #include <vector> #include <queue> #define N 100005 using namespace std; vector<int>G[N]; inline void Read(int &x) { register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()); for(;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar()); } bool instack[N]; int n,m,ans,stack[N],low[N],dfn[N],col[N],sum[N],f1[N],f2[N],in1[N],in2[N],sumcol,tim,top; inline int min(int a,int b) {return a>b?b:a;} inline int max(int a,int b) {return a>b?a:b;} void tarjan(int x) { low[x]=dfn[x]=++tim; stack[++top]=x; instack[x]=true; for(int i=0;i<G[x].size();++i) { int v=G[x][i]; if(!dfn[v]) { tarjan(v); low[x]=min(low[x],low[v]); } else if(instack[v]) low[x]=min(low[x],dfn[v]); } if(low[x]==dfn[x]) { int k; sumcol++; do { k=stack[top--]; instack[k]=false; col[k]=sumcol; sum[sumcol]++; }while(k!=x); } } struct node { vector<int>E[N]; int f[N],in[N]; bool Map[1005][1005]; void ins(int u,int v) { in[v]++; E[u].push_back(v); } void tppx() { queue<int>q; for(int i=1;i<=sumcol;++i) if(!in[i]) q.push(i); f[col[1]]=sum[col[1]]; for(int now;!q.empty();) { now=q.front();q.pop(); for(int i=0;i<E[now].size();++i) { int v=E[now][i]; f[v]=max(f[v],f[now]+sum[v]); in[v]--; if(!in[v]) q.push(v); } } } }; node a,b; int main() { Read(n);Read(m); for(int x,y;m--;) { Read(x);Read(y); G[x].push_back(y); } for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i); for(int i=1;i<=n;++i) { for(int j=0;j<G[i].size();++j) { int v=G[i][j]; if(col[i]!=col[v]&&!a.Map[col[i]][col[v]]) a.ins(col[i],col[v]),a.Map[col[i]][col[v]]=1; if(col[i]!=col[v]&&!b.Map[col[v]][col[i]]) b.ins(col[v],col[i]),b.Map[col[v]][col[i]]=1; } } a.tppx(); b.tppx(); ans=sum[col[1]]; for(int i=1;i<=n;++i) { for(int j=0;j<G[i].size();++j) { int v=G[i][j]; ans=max(ans,a.f[col[v]]+b.f[col[i]]); } } printf("%d\n",ans-sum[col[1]]); return 0; }
时间: 2024-10-13 10:28:39