There are many ways to find strong unicom components. "C + + Informatics Olympiad a pass" said a Dfs to find strong unicom components of the algorithm Kosdaraju, in order to cheat words I will be simple to say. However, the subject of our article is Tarjan, so I'm sure I'll pay tribute to Tarjan Dafa before I finish.
First of all, let's talk about the strong Unicom component
The strong Unicom component refers to a sub-graph of the graph. In this sub-diagram, any two nodes can reach each other. From the definition we can see that there is a direction graph, because any one of the non-map conforms to the definition.
The standard definition is: The largest sub-graph connected to any two points in the graph.
Cough, first celebrate the ha-my blog's first picture. The drawing lasted 3 minutes.
In our example, we can see that 1, 2, 3, 5 through the side can reach each other, they calculate a strong unicom component, but 4 is isolated from them. As you can see, you cannot reach any point from 4 pips. So it is a single node is also a strong unicom component. So there are two strong unicom components in the graph: one is 1-2-3-5, and the other is 4.
OK read the strong Unicom component is what we will talk about Kosaraju.
The idea of this algorithm is to DFS the diagram and record the exit order for each point. Re-construct the inverse graph (that is, the direction of the opposite side is all in turn), according to the exit order of the reverse Dfs inverse graph, the resulting points are dyed is the strong unicom component.
Finish the idea and start the simulation. With beginning 1 as starting point the traversal sequence is as follows:
[1 2 3 5 4 5 3 2 4 4 1 ]
The underlined part of the bold italic is the exit order of this diagram.
So we get an array: [5 3 2 4 1]. The inverse of this array is followed by a reverse graph:
[5 3 2 1 Exit 4 exit]
The two strong Unicom components that are required.
Also two times Dfs, the trouble ratio. Look at my big Tarjan. DFS will be able to find out the strong Unicom component
First we need to make clear the Tarjan two arrays to use: dfn[] and low[]
DFN refers to the order in which the point is accessed during the DFS process. Starting from 1 DFS full-view, then 1 of the DFN value is the DFN value is 2,5 the DFN value is the DFN value is 5. The rest of the analogy
What about low? Low refers to the point at which the node is first traversed by the inverse of the DFS sequence.
For example, in 2, 3, 5, 4 are the first from 1 came, so their low value is 1
The algorithms for DFN and low are posted below
Per DFS (point u) {
Dfn[u] = number of times the DFS () function was entered (self-defining a timestamp record such as Timee)
Enumerates the points adjacent to them v{
If there is no access to the dot v {(which is the tree edge on the Dfs tree)
DFS (v);
If v can be traced back to the earlier point than "U traced to the earliest point";
Then you can go back to that point by V;
Low[u]=min (Low[u],low[v]);
}
If you have access to the Point v && v in the stack
Low[u]=min (Low[u],dfn[v]);
}
Pinch point
}
The pseudo-code above is brought from the great Generalliu, first%%%.
Then suppose we go to a node I and find that I cannot continue to expand, that is, Dfn[i]==low[i]
So we started to go back. As we go back, we dye the nodes of a component and give them a uniform mark. Finally count how many different tags are the number of strong unicom components
Luogu a title burning disc is very good and can be used for practiced hand.
Put code
#include <iostream>#include<cstring>using namespacestd;inthead[10000],num;structedge{intNext,to;} edge[100000];intstack[10000],top;intcolor[10000],cnt;intdfn[10000],low[10000];intID;BOOLIdx10000];intvis[10000];inlinevoidAddint from,intTo ) {edge[++num]={head[ from],to}; head[ from]=num;}voidTarjan (intx) {Dfn[x]=++ID; LOW[X]=ID; JD[X]=1; stack[++top]=x; for(intI=head[x];i;i=Edge[i].next) { intto=edge[i].to; if(!Dfn[to]) {Tarjan (to); LOW[X]=min (low[x],low[to]); } Else if(Jd[to]) low[x]=min (low[x],dfn[to]); } if(dfn[x]==Low[x]) {Jd[x]=0; COLOR[X]=++CNT; while(stack[top]!=x) {Color[stack[top--]]=CNT; Jd[stack[top+1]]=0; Color[stack[top+1]]=CNT; } Top--; } }intMain () {intN; CIN>>N; intx; for(intI=1; i<=n;++i) { while(cin>>x&&x!=0) {Add (i,x); } } for(intI=1; i<=n;++i) { if(!Dfn[i]) Tarjan (i); } memset (JD,0,sizeof(JD)); for(intI=1; i<=n;++i) { for(intj=head[i];j;j=Edge[j].next) { if(color[i]!=Color[edge[j].to]) {Jd[color[edge[j].to]]=1; } } } intans=0; for(intI=1; i<=cnt;++i)if(!jd[i]) ans++; cout<<ans<<Endl; return 0;}
The strong unicom component of Tarjan