Tarjan algorithm A method of solving the strongly connected component of a forward graph by Robert Tarjan, which can achieve the complexity of linear time. (only once per point)
When it comes to Tarjan, the first thing we have to lose is a graph, because there is no one.
This sentence starts with the definition of the Tarjan algorithm.
We define:
If two vertices can be connected to each other, the two vertices are strongly connected (strongly connected). If there is strong connectivity to every two vertices of the graph G, the G is a strongly connected graph. A strongly connected sub-graph of a graph, called a strongly connected component (strongly connected components).
This sentence is very popular on the internet, but plainly is a unicom block, from each of the two points can reach each other, is a strong connected components
The role of Tarjan is to quickly find out the number of strongly connected components in a given graph, and the points in each strongly connected component.
Exactly how it's achieved.
We record two arrays
DFN and Low, they record the DFS sequence for each point separately, and the minimum DFS sequence at which each point can go back to the point
That may not be clear enough.
Let's Raise a chestnut: (For the sake of convenience, we take the point of walking to the table in red)
By convention, we start at point 1, first to his son, 2, then 3.
At this time dfn[1]=1
Low[1]=1
dfn[2]=2;
low[2]=2;
dfn[3]=3;
low[3]=3;
dfn[4]=0;
low[4]=0;
We found three have no son, that is to say low[3] value can not change, and found Dfn[3]=low[3]
So we know that 3 makes up a strong connected component
and keep going.
We're at the other son of 2.
At this time dfn[1]=1
Low[1]=1
dfn[2]=2;
low[2]=2;
dfn[3]=3;
low[3]=3;
dfn[4]=4;
low[4]=4;
And then there's the most important step.
4 One son is 1, but 1 has passed.
So we can update the value of Low[4] directly
At this time dfn[1]=1
Low[1]=1
dfn[2]=2;
low[2]=2;
dfn[3]=3;
low[3]=3;
dfn[4]=4;
Low[4]=1;
Now that 1 has passed, we don't have to go anymore (otherwise how linear)
Start backtracking
We update the value of low[2]
At this time dfn[1]=1
Low[1]=1
dfn[2]=2;
Low[2]=1;
dfn[3]=3;
low[3]=3;
dfn[4]=4;
Low[4]=1;
And then back to the 1,tarjan algorithm end
The above is the specific operation process of Tarjan
But there are some simple staining operations.
It's not fine. (comments are given in the code)
Here is the code: (Writing the code in the process of the computer suddenly card, the results have just finished writing templates and comments are not sad to deformation qaq)
#include <iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<stack>using namespaceStd;inlineintMinintAintb) {returnA<b?a:b;} InlineintMaxintAintb) {returnA>b?a:b;} InlineintRd () {intx=0, f=1; CharC=GetChar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;} InlinevoidWriteintx) { if(x<0) Putchar ('-'), x=-x; if(x>9) Write (x/Ten); Putchar (x%Ten+'0');}intn,m;inthead[100006];intnxt[100006],to[100006];intTotal=0;intdfn[100006],low[100006];inttot=0;intbook[100006],color[100006];intCnt=0; Stack<int>s;voidAddintXinty) { Total++; To[total]=y; Nxt[total]=Head[x]; HEAD[X]=Total ; return ;}voidTarjan (intx) {Dfn[x]=++tot; LOW[X]=Dfn[x]; BOOK[X]=1; S.push (x); for(intE=head[x];e;e=Nxt[e]) { if(!dfn[to[e]]) {//If you don't walk through it, expand the point.Tarjan (To[e]); LOW[X]=min (low[x],low[to[e]]); } Else if(Book[to[e]]) low[x]=min (Low[x],low[to[e]);//on the stack represents this point passing by, and he can backtrack to the point where his father can also } if(Dfn[x]==low[x]) {//if the DFN of a point is equal to low, the sub-graph that is the root of it is the strongly connected componentcolor[x]=++cnt;//Dyeingbook[x]=0; while(!s.empty () &&s.top ()!=x) {//put a stack of points with X UnicomColor[s.top ()]=CNT; Book[s.top ()]=0; S.pop (); } s.pop (); } return ;}intMain () {n=rd (), m=Rd (); for(intI=1; i<=m;i++){ intX=rd (), y=Rd (); Add (x, y);//directional graphs One-way connecting Edge } for(intI=1; i<=n;i++)if(!dfn[i]) Tarjan (i);//avoid some points not connectedprintf"%d\n", CNT); for(intI=1; i<=cnt;i++){ for(intj=1; j<=n;j++)if(color[j]==i) printf ("%d", j);//can actually be written in the Tarjan process, but will become the reverse of the DFS sequencePuts""); } return 0;}
Talking about Tarjan