标签:can ons front clu const == turn end div
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; stack<int>s; vector<int>vv[N]; vector<int>v2[N]; bool vis[N]; int n,m,tim,num,cnt,in_[N];int ans[N]; int dfn[N],low[N],bel[N],x,y,size[N],max_;queue<int>q; void tarjan(int u) { dfn[u]=low[u]=++tim; vis[u]=1;s.push(u); for(int i=0;i<vv[u].size();i++) { int v=vv[u][i]; if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]); else if(vis[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { int v;num++; do { v=s.top(),s.pop(); vis[v]=0,bel[v]=num; size[num]++; }while(u!=v); } } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);vv[x].push_back(y);} for(int i=1;i<=n;i++){ sort(vv[i].begin(),vv[i].end()); unique(vv[i].begin(),vv[i].end()); } for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); for(int i=1;i<=n;i++) for(int j=0;j<vv[i].size();j++){ int u=bel[i],v=bel[vv[i][j]]; if(u!=v) v2[u].push_back(v); } for(int i=1;i<=num;i++){ sort(v2[i].begin(),v2[i].end()); unique(v2[i].begin(),v2[i].end()); } for(int i=1;i<=num;i++) for(int j=0;j<v2[i].size();j++){ int u=i,v=v2[i][j];in_[v]++; } for(int i=1;i<=num;i++) if(in_[i]==0) q.push(i),ans[i]=size[i]; while(!q.empty()){ int u=q.front();q.pop(); for(int i=0;i<v2[u].size();i++){ int v=v2[u][i]; ans[v]=max(ans[v],ans[u]+size[v]);in_[v]--; if(in_[v]==0) q.push(v); } } for(int i=1;i<=num;i++) max_=max(max_,ans[i]); printf("%d\n",max_); return 0; }
标签:can ons front clu const == turn end div
原文地址:https://www.cnblogs.com/vainglory/p/8886372.html