In the words of yesterday's practice, there is a problem that requires the pretreatment of strong connected components. However, I still do not know what the Tarjan algorithm, and then paste out the following algorithm to find strong connected components. (Time complexity O (N*LOGN))
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn=10010;4 structedge{5 intla,b;6}con[n<<1];7 intTot,fir[n];8 voidAddintUintv)9 {TenCon[++tot].la=Fir[u]; Onecon[tot].b=v; Afir[u]=tot; - } - intdeep[n],bel[n],n,m; the BOOLPass[n],vis[n]; - intGet_bel (intPOS) - { - if(Bel[pos]!=pos) bel[pos]=Get_bel (Bel[pos]); + returnBel[pos]; - } + intDfsintPosintFA) A { atvis[pos]=1;p ass[pos]=1; -deep[pos]=deep[fa]+1; -bel[pos]=Pos; - inttmp; - for(intI=fir[pos];i;i=con[i].la) - { in if(vis[con[i].b]&&!pass[con[i].b]) - { to if(Pass[get_bel (con[i].b)]) + { - if(Deep[get_bel (con[i].b)]<Deep[get_bel (POS)]) thebel[pos]=Get_bel (con[i].b); * } $ Continue;Panax Notoginseng } - if(!pass[con[i].b]) the { +tmp=DFS (con[i].b,pos); A if(Pass[tmp]&&deep[tmp]<deep[get_bel (POS)]) bel[pos]=tmp; the } + Else - { $ if(Deep[con[i].b]<deep[get_bel (POS)]) bel[pos]=con[i].b; $ } - } -pass[pos]=0; the returnGet_bel (POS); - }Wuyi voidSolve () the { -Dfs1,0); Wu for(intI=1; i<=n;i++) Cout<<get_bel (i) <<Endl; - } About intMain () $ { - int from, to; -scanf"%d%d",&n,&m); - for(intI=1; i<=m;i++) A { +scanf"%d%d",& from,&to ); theAdd from, to); - } $ solve (); the return 0; the}
So with this algorithm wrote the problem, incredibly AC. (The problem also requires single source shortest, card not off my O (n*logn) pretreatment 2333)
(Note: This spicy chicken algorithm is to use and check each point connected to its strong link to the minimum depth of the point, and then to the tree side, the back edge, the cross-side are discussed once (forward edge when the cross-border processing))
Then I learned the Tarjan algorithm, and then wrote the following program.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn=10010;4 intlow[n],dfn[n],n,m;5 structedge{6 intla,b;7}con[n<<1];8 intTot,fir[n];9 voidAddint from,intto )Ten { Onecon[++tot].la=fir[ from]; Acon[tot].b=to ; -fir[ from]=tot; - } thestack<int>s; - intbel[n],cnt; - voidTarjan (intPOS) - { + S.push (POS); -low[pos]=dfn[pos]=++CNT; + for(intI=fir[pos];i;i=con[i].la) A { at if(bel[con[i].b])Continue; - if(!dfn[con[i].b]) Tarjan (con[i].b); -low[pos]=min (low[pos],low[con[i].b]); - } - if(dfn[pos]==Low[pos]) - { in intpo=s.top (); S.pop (); - while(po!=POS) to { +bel[po]=Pos; -po=s.top (); S.pop (); the } *bel[pos]=Pos; $ }Panax Notoginseng } - voidSolve () the { + while(!s.empty ()) S.pop (); ATarjan (1); the for(intI=1; i<=n;i++) cout<<bel[i]<<Endl; + } - intMain () $ { $ int from, to; -scanf"%d%d",&n,&m); - for(intI=1; i<=m;i++) the { -scanf"%d%d",& from,&to );WuyiAdd from, to); the } - solve (); Wu return 0; -}
A lot shorter.
And then there's no more. (Parsing of the Tarjan algorithm?) There are many online. )
Summary: Big guy and Konjac Konjac gap is reflected in that log.
"Learning" Tarjan algorithm