graph theory Algorithm-tarjan template "shrinking point; cutting top; Double connecting component"
Tarjan Three algorithms for the small partners
Tarjan Indent (strong connected component)
int N;int low[100010],dfn[100010];bool Ins[100010];int col[100010];//Record each point belongs to the strong connected component (i.e. dyeing) vector<int> map[100010] ;stack<int> st;int tot;//Timestamp int colnum;//records the number of strongly connected components void Tarjan (int u) {low[u]=dfn[u]=++tot; St.push (U); Ins[u]=true; for (int j=0;j<map[u].size (); j + +) {int v=map[u][j]; if (!dfn[v]) {Tarjan (v); Low[u]=min (Low[u],low[v]); } else if (Ins[v]) low[u]=min (Low[u],dfn[v]); } if (Low[u]==dfn[u]) {///to which a new strong connected component colnum++ is found; int temp; int cont=0; do {temp=st.top (); St.pop (); Ins[temp]=false; The point of the same strong connected component is dyed to represent a col[temp]=colnum of the indentation point; It is also possible to perform some other operations on the strongly connected component, such as saving the original node contained in the Indent (temp!=u). }}void init () {memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); memset (col,0,sizeof (col)); memset (ins,false,sizeof (INS)); tot=colnum=0;} void Solve () {init (); for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i); }}
Tarjan Cutting Point (cutting top)
int n;vector<int> map[100010];int low[100010],dfn[100010];bool cut[1000010];//记录割点int tot;void tarjan(int u,int fa){ low[u]=dfn[u]=++tot; int child=0; for(int j=0;j<map[u].size();j++) { int v=map[u][j]; if(!dfn[v]) { child++; tarjan(v,u); low[u]=min(low[u],low[v]); if(low[v]>=dfn[u]) cut[u]=true; } else if(dfn[v]<dfn[u]&&v!=fa) low[u]=min(low[u],dfn[v]); } if(fa<0&&child==1) cut[u]=false;}void init(){ memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(cut,false,sizeof(cut)); tot=0;}void solve(){ init(); for(int i=1;i<=n;i++) { if(!dfn[i]) tarjan(i,-1); //**高亮**这里的第二个参数一定要设为负数 } //cut[i]==true即表示i为割点}
Tarjan-Two connected components
int n;vector<int> map[100010];int low[100010],dfn[100010];bool cut[1000010];int bcc[1000010];int tot;int bcc_ cont//records the number of two connected components; struct Edge{int u,v};stack<edge> e;void tarjan (int u,int fa) {low[u]=dfn[u]=++tot; int child=0; for (int j=0;j<map[u].size (); j + +) {int v=map[u][j]; Edge e= (Edge) {u,v}; if (!dfn[v]) {E.push (E); child++; Tarjan (V,u); Low[u]=min (Low[u],low[v]); if (Low[v]>=dfn[u]) {//To here a new double-connected component cut[u]=true is found; bcc_cont++: while (1) {Edge temp=e.top (); E.pop (); if (Bccno[temp.u]!=bcc_cont) Bccno[temp.u]=bcc_cont; if (Bccno[temp.v]!=bcc_cont) Bccno[temp.v]=bcc_cont; if (Temp.u==u& &temp.v==v) break; }}} and else if (DFN[V]<DFN[U]&&V!=FA) {E.push (E); Low[u]=min (Low[u],dfn[v]); }} if (Fa<0&&child==1) Cut[u]=false;} void Init () {memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); memset (bccno,0,sizeof (BCCNO)); tot=bcc_cont=0;} void Solve () {init (); for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i,-1);//** Highlight * * The second argument here must be set to a negative number}}
In fact, three algorithms are basically the same way of thinking
After all, it's the same person who raised it.
Graph theory Algorithm-tarjan template "shrinking point; cutting top; Double connecting component"