Test Instructions:
N Villas and a main building, from each villa there are many different ways to walk to the main building, wherein the definition is (each side can go multiple times, if the order of the walk side is a different is called two ways different).
Ask the most different ways, and how many villas there are so many ways to output the villa number in sequence.
If the maximum number of different ways exceeds 36500 then it is regarded as Zawsze
parsing:
It is easy to think of the reverse side, the problem into the plan from the main building to each point of the program number.
For a strongly connected component, obviously we can think of it as a point, so we'll start by narrowing the point.
After shrinking point
We set F[i] Represents the number of scenarios that go to the first point.
Obviously F [I]=∑F [J](Savein theNe w e d g e(J,I))
It is also particularly important to note that, initially, the f[belong[main building is]]=1.
Second, any one of the siz[i]>1 points, if f[i]>0, then obviously there are countless ways to go, directly set to Maxn+1.
Then we observe that this is actually the process of doing DP on a topological map.
Direct topology map on DP.
Finally sweep over the statistical solution.
Complexity: O (constant *n);
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1000100#define MAXN 36500using namespace STD;intn,m,rev_cnt;intRev_head[n];structline{intx, y;} L[n];structnode{intFrom,to,next;} Rev_edge[n];voidInit () {memset(rev_head,-1,sizeof(Rev_head)); Rev_cnt=1;}voidEdgeadd (intFromintTo,node *edge,int*head,int&CNT) {Edge[cnt].from=from,edge[cnt].to=to,edge[cnt].next=head[from]; head[from]=cnt++;}intDeep[n],low[n],sta[n],ins[n],belong[n],top,cnt_block,tot;voidTarjan (intNow) {Deep[now]=low[now]=++tot; sta[++top]=now,ins[now]=1; for(inti=rev_head[now];i!=-1; i=rev_edge[i].next) {intto=rev_edge[i].to;if(!deep[to]) {Tarjan (to); Low[now]=min (Low[now],low[to]); }Else if(Ins[to]) low[now]=min (Low[now],deep[to]); }if(Deep[now]==low[now]) {cnt_block++;intt=-1; Do{t=sta[top--]; ins[t]=0; Belong[t]=cnt_block; } while(T!=now); }}BOOLTag[n];intIn[n];voidRe_build () { for(intI=1; i<=m;i++) {intX=L[I].X,Y=L[I].Y;if(Belong[x]==belong[y]) {tag[belong[x]]=1;Continue;} Edgeadd (BELONG[X],BELONG[Y],REV_EDGE,REV_HEAD,REV_CNT); in[belong[y]]++; }}intF[n];voidTopsort () { Queue<int>Q for(intI=1; i<=cnt_block;i++)if(!in[i]) Q.push (i); f[belong[n]]=1;if(Tag[belong[n]]) F[BELONG[N]]+=MAXN; while(!q.empty ()) {intU=q.front (); Q.pop (); for(inti=rev_head[u];i!=-1; i=rev_edge[i].next) {intto=rev_edge[i].to; F[to]+=f[u];if(F[TO]>MAXN) f[to]=maxn+1; in[to]--;if(!in[to]) {if(tag[to]&&f[to]>0) f[to]=maxn+1; Q.push (to); } } }}voidCalc () {intans=-1; top=0; for(intI=1; i<n;i++)if(F[belong[i]]>ans) ans=f[belong[i]]; for(intI=1; i<n;i++)if(F[belong[i]]==ans) sta[++top]=i;printf(ans==maxn+1?"zawsze\n":"%d\n", ans);printf("%d\n", top); for(intI=1; i<=top;i++)printf("%d", Sta[i]);puts("");}intMain () {#ifndef Online_judgeFreopen ("1512.in","R", stdin); Freopen ("1512.out","W", stdout);#endifInit ();scanf("%d%d", &n,&m); n++; for(intI=1; i<=m;i++) {intx, y;scanf("%d%d", &x,&y); Swap (x, y); Edgeadd (X,Y,REV_EDGE,REV_HEAD,REV_CNT); L[i].x=x,l[i].y=y; } for(intI=1; i<=n;i++)if(!deep[i]) Tarjan (i); Init (); Re_build (); Topsort (); Calc ();#ifndef Online_judgeFclose (stdin); fclose (stdout);#endif return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1512 [poi2006]pro-professor szu Tarjan indent + topology DP