In undirected graphs, u is the parent of v
The value of low(v) represents the timestamp of the largest ancestor node that V considers accessible through u
Low (v) >= DFN (u) means that V must access ancestor nodes through v-u (excluding u), and there is no other way. (cut point)
Low (v) > DFN (u) indicates that V must access ancestor nodes through v-u (including u). (cutting edge)
low(v)==dfn(u) means that V can go back to u without u, i.e. from the side of v-u to u, but it must access the ancestor node from U.
Low (v) < DFN (u) means that V can access ancestor nodes (including u) without u.
const int N=1e5+10; struct ac{ int u,v; }edge[N]; int tot,now; int dfn[N],low[N]; vector<int> g[N]; void tarjan(int u,int fa){ dfn[u]=low[u]=++now; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(dfn[v]!=0&&v!=fa){ low[u]=min(low[u],dfn[v]); } else if(dfn[v]==0){ tarjan(v,u); low[u]=min(low[u],low[v]); if(dfn[u]<low[v]){ if(u<v){ edge[tot].u=u; edge[tot++].v=v; } else { edge[tot].u=v; edge[tot++].v=u; } } } } }
struct ac{ int u,v; ac(int _u,int _v){ u=_u,v=_v; } }; vector<ac> edges; int tot,now; int dfn[N],low[N]; vector<int> g[N]; void tarjan(int u,int fa){ dfn[u]=low[u]=++now; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(dfn[v]!=0&&v!=fa){ low[u]=min(low[u],dfn[v]); } else if(dfn[v]==0){ tarjan(v,u); low[u]=min(low[u],low[v]); if(dfn[u]<low[v]){ if(u<v){ edges.push_back(ac(u,v)); } else { edges.push_back(ac(v,u)); } } } } }
Example
#include<iostream> #include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int N=1e5+10; struct ac{ int u,v; }edge[N]; int tot,now; int dfn[N],low[N]; vector<int> g[N]; void tarjan(int u,int fa){ dfn[u]=low[u]=++now; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; if(dfn[v]!=0&&v!=fa){ low[u]=min(low[u],dfn[v]); } else if(dfn[v]==0){ tarjan(v,u); low[u]=min(low[u],low[v]); if(dfn[u]<low[v]){ if(u<v){ edge[tot].u=u; edge[tot++].v=v; } else { edge[tot].u=v; edge[tot++].v=u; } } } } } void init(){ memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); tot=now=0; for(int i=0;i<N;i++){ edge[i].u=edge[i].v=0; g[i].clear(); } } bool cmp(ac a1,ac a2){ if(a1.u==a2.u) return a1.v<a2.v; else return a1.u<a2.u; } int main(){ int n,m,mn,t; while(scanf("%d",&n)!=EOF){ init(); for(int i=0;i<n;i++){ scanf("%d (%d)",&m,&mn); for(int j=0;j<mn;j++){ scanf("%d",&t); g[m].push_back(t); } } for(int i=0;i<n;i++) if(dfn[i]==0) tarjan(i,-1); sort(edge,edge+tot,cmp); printf("%d critical links\n",tot); for(int i=0;i<tot;i++){ printf("%d - %d\n",edge[i].u,edge[i].v); } cout<<endl; } }