Link to original topic
In an undirected connected graph, if an edge is deleted, the graph becomes disconnected, and the edge is called a bridge.
You can also use Tajan () to work with DFS to calculate the low and DFN values for all points, and to record the parent node of each point in the DFS process, and then iterate through all the points, looking at their low and DFN, satisfying dfn[FA]<low[i] (0<i<=n, I Father is FA)--the bridge is fa-i. When looking for a bridge, be careful to see if there is a heavy edge, but not a bridge.
In addition, there are no heavy edges in the test instructions and test examples, so there is no need to consider heavy edges.
the puzzle with detailed comments:
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<stack>#include<vector>#defineMAXN 10010using namespacestd;intDFN[MAXN],LOW_LINK[MAXN], FATHER[MAXN];//The Tarjan algorithm's dfn--access number in the DFS process (also known as the start time//low_link[i]--of the Tarjan algorithm from the I node the start time of the earliest node in the DFS process that the node below I can reachintBridgenum, time, N;//The total number of bridges, DFN timestamp, n is the number of vertices,vector<int>G[maxn];//defining adjacency matrix tables for graphsstack<int>St;structnode{intU,v;} BRIDGE[MAXN]; //The storage of the bridge across the graphBOOLCMP (node A,node b) {if(A.U!=B.U)returna.u<b.u; Else returna.v<B.V;}voidinit () {inti; for(i=0; i<=n;i++)//initializing adjacency tablesg[i].clear (); Bridgenum=0; Time=0; memset (DFN,0,sizeof(DFN)); memset (Low_link,0,sizeof(Low_link)); memset (Father,0,sizeof(Father));}voidTarjan (intUintFA) {Low_link[u]=dfn[u]=++Time ; Father[u]=FA;//Record parent Node//st.push (u); for(intI=0;i< (int) g[u].size (); i++){ intv=G[u][i]; if(!Dfn[v]) {Tarjan (v,u); Low_link[u]=min (low_link[u],low_link[v]); } Else if(V!=FA) {//cannot connect to parent node! low_link[u]=min (low_link[u],dfn[v]); } Else{ //This situation is a case of heavy edge! No processing, skip directly! } }}voidsolve () { for(intI=0; i<n;i++){ if(!Dfn[i]) Tarjan (i,-1); } intans=0; for(intI=0; i<n;i++){ intv=Father[i]; if(dfn[v]<low_link[i]&&v!=-1){//if V-i can form a parent nodeBridge[ans].u=v;//two sides of the bridgebridge[ans].v=i; if(bridge[ans].u>bridge[ans].v) Swap (BRIDGE[ANS].U,BRIDGE[ANS].V); Ans++; }} sort (Bridge,bridge+ans,cmp); printf ("%d critical links\n", ans); for(intI=0; i<ans;i++) {printf ("%d-%d\n", BRIDGE[I].U,BRIDGE[I].V); }}intCal_num (Charch[]) { intLen=strlen (CH), s=0; for(intI=1; i<=len-2; i++) {s=s*Ten+ch[i]-'0'; } returns;}intMain () {intt,cas=0; scanf ("%d",&T); while(t--) {init (); Charch[Ten]; intM, u,v;//Number of sidesscanf"%d",&N); for(intI=1; i<=n;i++) {scanf ("%d%s",&u,ch); M=cal_num (CH);//intercept the number of digits deposited into the m--side for(intj=1; j<=m;j++) {scanf ("%d",&v); G[u].push_back (v); //here according to one-way side storage on either side, after all, is no map! g[v].push_back (U); }} printf ("Case %d:\n",++CAs); Solve (); } return 0;}
View Code
Light oj-1026-critical Links (graph theory-tarjan algorithm for the number of bridges without graphs)-with detailed notes