/******************************************************* title: Redundant Paths (POJ 2177) Link:http://poj.org/problem?id=3177algorithm: Double unicom + shrink point of thought: first find all the double unicom components, the components are shrunk to a point and then find all the points of a point, with these points plus a 2 can be ************************************************* *******/#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<iostream>#include<stack>using namespacestd;Const intmx=5005; Vector<int>G[mx];stack<int>s;intDFN[MX],LOW[MX];intBCC[MX],VS[MX];int inch[MX];intDfs_cut,bcc_cut;voidDfsintUintFA) {Dfn[u]=low[u]=++Dfs_cut; Vs[u]=1; S.push (U); intp=1;///Go heavy for(intI=0; I<g[u].size (); i++) { intv=G[u][i]; if(v==fa&&p) {p=0; Continue; } if(!Vs[v]) {DFS (V,U); Low[u]=min (low[u],low[v]); } Elselow[u]=min (low[u],dfn[v]); } ///Pinch point if(low[u]==Dfn[u]) {Bcc_cut++; intx; while(1) {x=S.top (); S.pop (); BCC[X]=Bcc_cut; if(X==u) Break; } }}intMain () {intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) g[i].clear (); Bcc_cut=dfs_cut=0; while(m--) { intu,v; scanf ("%d%d",&u,&v); G[u].push_back (v); G[v].push_back (U); } DFS (1,-1); for(intu=1; u<=n;u++) { for(intj=0; J<g[u].size (); j + +) { intv=G[u][j]; if(bcc[u]!=Bcc[v]) { inch[bcc[u]]++; inch[bcc[v]]++; } } } intans=0; for(intI=1; i<=bcc_cut;i++) { if(inch[i]==2) ans++; } printf ("%d\n", (ans+1)/2);}
POJ 3177 redundant Paths (double unicom)