Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3321 Accepted Submission(s): 1041
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; const int N=5008; const int M=30008; int head[N],tot,scnt,cnt,cont,sz[N]; int dfn[N],low[N],bl[N],q[N],l; bool instack[N],ru[N]; vector<int>G; vector<int>C[N]; struct node{ int to,next; }e[M]; void add(int u,int v){ e[tot].to=v; e[tot].next=head[u]; head[u]=tot++; } void init(){ tot=scnt=cnt=l=0; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack)); memset(ru,0,sizeof(ru)); G.clear(); for(int i=0;i<N;++i) C[i].clear(); } void Tajan(int u){ dfn[u]=low[u]=++cnt; q[l++]=u; instack[u]=1; for(int i=head[u];i+1;i=e[i].next){ int v=e[i].to; if(!dfn[v]) { Tajan(v); low[u]=min(low[u],low[v]); } else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(dfn[u]==low[u]) { ++scnt; int t; sz[scnt]=0; do{ t=q[--l]; bl[t]=scnt; ++sz[scnt]; C[scnt].push_back(t); instack[t]=0; }while(t!=u); } } bool used[N]; void dfs(int u,int tz){ for(int i=head[u];i+1;i=e[i].next) if(!used[e[i].to]){ used[e[i].to]=1; sz[tz]+=sz[e[i].to]; dfs(e[i].to,tz); } } struct point{ int u,v; }ee[M]; int main(){ int n,m,u,v,T; scanf("%d",&T); for(int tas=1;tas<=T;++tas){ init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;++i){ scanf("%d%d",&u,&v); add(v,u); } for(int i=0;i<n;++i) if(!dfn[i]) Tajan(i); int pp=0; for(int i=0;i<n;++i) for(int j=head[i];j+1;j=e[j].next){ int v=e[j].to; if(bl[i]==bl[v]) continue; else {ee[pp].u=bl[i];ee[pp++].v=bl[v];ru[bl[v]]=1;} } memset(head,-1,sizeof(head)); tot=0; for(int i=0;i<pp;++i) add(ee[i].u,ee[i].v); int maxx=-1; for(int i=1;i<=scnt;++i) if(!ru[i]){ memset(used,0,sizeof(used)); dfs(i,i); if(maxx<sz[i]) {maxx=sz[i];G.clear(); for(int j=0;j<(int)C[i].size();++j) G.push_back(C[i][j]);} else if(maxx==sz[i]) for(int j=0;j<(int)C[i].size();++j) G.push_back(C[i][j]); } sort(G.begin(),G.end()); printf("Case %d: %d\n",tas,maxx-1); for(int i=0;i<(int)G.size()-1;++i) printf("%d ",G[i]); printf("%d\n",G[(int)G.size()-1]); } }
原文:http://www.cnblogs.com/mfys/p/7258886.html