Test instructions: Given an absence graph, the number of edges that are not in any ring and the amount of edges in two or more rings are calculated respectively.
Solution: The side of the bridge is not in any ring. And if the number of edges in a two-connected component is more than the number of points, then all the sides of the two-connected component are in two or more rings at the same time (this can be imagined by adding a different edge to a simple ring, so that the simple ring is divided into two small simple rings, Any edge in a large ring will be in one of the small rings at the same time.
In the Tarjan algorithm, when lowv>pre[u] (U is the starting point, V is the next point of the edge), The Edge (U,V) is a bridge. The number of edges of the point double connected component can be counted by the number of POPs in the stack.
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string> #include <cmath> #include <set> #include <climits> #include <queue> #include < vector> #include <stack> #include <map> #include <climits>using namespace std;const int maxn=10005; struct Edge{int u,v; Edge (int uu,int vv): U (UU), V (vv) {}edge () {}};int Pre[maxn],bccno[maxn],dfs_clock,bcc_cnt;bool iscut[maxn];std:: vector<int> g[maxn],bcc[maxn];int ans,ans1;stack<edge>s;int dfs (int u,int fa) {int lowu=pre[u]=++dfs_ Clock;int child=0;for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; Edge E=edge (u,v), if (!pre[v]) {S.push (e); Child++;int Lowv=dfs (V,u); Lowu=min (LOWU,LOWV); if (Lowv>=pre[u])//conditions must be " =, if only write = =, then the side of the bridge will not be ejected from the stack, resulting in the subsequent double-connected component of the number of side calculation error {if (Lowv>pre[u]) ans1++;//(U,V) is a bridge iscut[u]=true;bcc_cnt++;bcc[bcc_ Cnt].clear (); int cnt=0;for (;;) {cnt++; Edge x=s.top (); S.pop (); if (bccno[x.u]!=bcc_cnt) {bcc[bcc_cnt].push_back (x.u); bccno[x.u]=bcc_cnt;} if (bccno[x.v]!=BCC_CNT) {bcc[bcc_cnt].push_back (X.V); bccno[x.v]=bcc_cnt;} if (x.u==u&&x.v==v) break;} if (Cnt>bcc[bcc_cnt].size ()) ans+=cnt;//when the number of edges in a point double connected component is greater than the number of points}}else if (PRE[V]<PRE[U]&&V!=FA) {S.push (E) ; Lowu=min (Lowu,pre[v]);}} if (fa<0&&child==1) Iscut[u]=false;return Lowu;} void find_bcc (int n) {memset (pre,0,sizeof (PRE)), memset (bccno,0,sizeof (BCCNO)), memset (iscut,0,sizeof (iscut));d Fs_ clock=bcc_cnt=0;for (int i=0;i<n;i++) {if (!pre[i]) DFS (I,-1);}} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&m==0) break;int a,b;for (int i=0;i<n; i++) G[i].clear (); for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); G[a].push_back (b); G[b].push_back (a);} ANS=0,ANS1=0;FIND_BCC (n);p rintf ("%d%d\n", Ans1,ans);}}
HDU 3394 Railway (application of point dual connected components)