标签:pre can tle following bsp get stack sep script
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
Case 1: 2 0 1 Case 2: 2 0 1 2
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #include<queue> using namespace std; #define Max 5005 int head[Max],head2[Max],j,k,bcnt,dindex,dfn[Max],low[Max],belong[Max],sup[Max]; bool visit[Max],vis[Max];; stack<int> s; struct { int s; int e; int next; }edge[Max*6],edge2[Max*6]; void add(int s,int e) { edge[k].s=s; edge[k].e=e; edge[k].next=head[s]; head[s]=k++; } void add2(int s,int e) { edge2[j].s=s; edge2[j].e=e; edge2[j].next=head2[s]; head2[s]=j++; } void tarjan(int i) { int ed; dfn[i]=low[i]=++dindex; visit[i]=true; s.push(i); for(int t=head[i];t!=-1;t=edge[t].next) { ed=edge[t].e; if(!dfn[ed]) { tarjan(ed); if(low[i]>low[ed]) low[i]=low[ed]; } else if(visit[ed]&&low[i]>dfn[ed]) low[i]=dfn[ed]; } if(dfn[i]==low[i]) { bcnt++; do { ed=s.top(); s.pop(); visit[ed]=false; belong[ed]=bcnt; }while(i!=ed); } } void solve(int n) { int i; bcnt=dindex=0; memset(visit,false,sizeof(visit)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); memset(dfn,0,sizeof(dfn)); while(!s.empty()) s.pop(); for(i=0;i<n;i++) if(!dfn[i]) tarjan(i); } int bfs(int rt) { int i,st,ed,sum=0; vis[rt]=true; queue<int> q; q.push(rt); while(!q.empty()) { st=q.front(); q.pop(); for(i=head2[st];i!=-1;i=edge2[i].next) { ed=edge2[i].e; if(!vis[ed]) { sum+=sup[ed]+1; q.push(ed); vis[ed]=true; } } } return sum; } int main() { int i,t,m,n,a,b,ans[Max],sum[Max],flag,cnt,p[Max],ncase=1; scanf("%d",&t); while(t--) { flag=-1; j=k=cnt=0; memset(p,0,sizeof(p)); memset(ans,0,sizeof(ans)); memset(sum,0,sizeof(sum)); memset(head,-1,sizeof(head)); memset(head2,-1,sizeof(head2)); memset(sup,0,sizeof(sup)); scanf("%d%d",&n,&m); while(m--) { scanf("%d%d",&a,&b); add(a,b); } solve(n); for(i=0;i<k;i++) { int st=edge[i].s; int ed=edge[i].e; if(belong[st]!=belong[ed]) add2(belong[ed],belong[st]); } for(i=0;i<n;i++) sup[belong[i]]++; for(i=1;i<=bcnt;i++) sup[i]--; for(i=1;i<=bcnt;i++) { memset(vis,false,sizeof(vis)); sum[i]=bfs(i)+sup[i]; } for(i=0;i<n;i++) { ans[i]=sum[belong[i]]; flag=max(flag,ans[i]); } for(i=0;i<n;i++) { if(ans[i]==flag) p[cnt++]=i; //cout<<ans[i]<<endl; } printf("Case %d: %d\n",ncase++,flag); for(i=0;i<cnt;i++) printf("%d%c",p[i],i==cnt-1?‘\n‘:‘ ‘); } return 0; }
标签:pre can tle following bsp get stack sep script
原文地址:http://www.cnblogs.com/tlnshuju/p/6922550.html