For a given connected graph, how many edges can be added to the edge double connected graph.
If a connected graph with a bridge is to become a double connected graph, the double connected sub-graph is shrunk to a point, forming a tree. The side to be added is (leaf+1)/2 (leaf is the number of leaf nodes).
For this problem, there are heavy edges but heavy edges are not added to the calculation.
If you have a heavy edge, you can either start removing it or use a bridge to calculate the degree of penetration.
Because the bridge does not belong to any one-side double-connected branch, the rest of the edges and each vertex belong to and belong to only one edge double connected branch. For heavy edges, only a pair of edges are marked as bridges, and for all heavy edges, belong "U" and Belong "V" are different, so if you use belong "U"! =belong "V" as an added-in condition, there is no doubt that it will be extra.
In that case, if the contraction point is re-built, use a bridge to build a better one so that the new map will not have a heavy edge.
Code (calculated with bridge):
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <queue> #include <vector> #include <cstring> #include < Algorithm> #define REP (i,a,b) for (int i= (a);i< (b); i++) #define Rev. (I,A,B) for (int i= (a); i>= (b); i--) #define CLR (a,x) memset (a,x,sizeof a) #define INF 0x3f3f3f3ftypedef Long long ll;using namespace std;const int Maxn=200005;const int m axm=2000005;struct node{int u,v; BOOL operator< (const node &b) const {if (B.U!=U) return u<b.u; Return v<b.v; }}da[maxm/2];int First[maxn],ecnt,u[maxm],v[maxm],nex[maxm];int Low[maxn],dfn[maxn],stck[maxn],belong[maxn];int Indx,top,block,bridge;bool ins[maxn],ecut[maxm];int n,m;int du[maxn];void tarjan (int s,int pre) {Low[s]=dfn[s]=++indx ; Stck[top++]=s; Ins[s]=1; for (int e=first[s];~e;e=nex[e]) {if (v[e]==pre) continue; if (!dfn[v[e]]) {Tarjan (v[e],s); Low[s]=min (Low[s],low[v[e]); if (Low[v[e]]>dfn[s]) {bridge++; Ecut[e]=ecut[e^1]=1; }} else if (Ins[v[e]]) low[s]=min (Low[s],dfn[v[e]); } if (Low[s]==dfn[s]) {int V; block++; do {v=stck[--top]; Ins[v]=false; Belong[v]=block; }while (v!=s); }}void solve (int n) {clr (dfn,0); CLR (ins,0); Indx=block=top=0; for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (1,0);} void add_ (int a,int b) {u[ecnt]=a; V[ecnt]=b; ecut[ecnt]=0; Nex[ecnt]=first[a]; first[a]=ecnt++;} int main () {int a A, B; while (~SCANF ("%d%d", &n,&m)) {ecnt=0; CLR (first,-1); for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); Add_ (A, b); Add_ (B,a); } solve (n); CLR (du,0); for (int i=0;i<ecnt;i++) if (Ecut[i]) { du[belong[u[i]]]++; } int sum=0; for (int i=1;i<=block;i++) if (du[i]==1) sum++; printf ("%d\n", (sum+1)/2); } return 0;}
Code two:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <queue> #include <vector> #include <cstring> #include < Algorithm> #define REP (i,a,b) for (int i= (a);i< (b); i++) #define Rev. (I,A,B) for (int i= (a); i>= (b); i--) #define CLR (a,x) memset (a,x,sizeof a) #define INF 0x3f3f3f3ftypedef Long long ll;using namespace std;const int Maxn=200005;const int m axm=2000005;struct node{int u,v; BOOL operator< (const node &b) const {if (B.U!=U) return u<b.u; Return v<b.v; }}da[maxm/2];int First[maxn],ecnt,u[maxm],v[maxm],nex[maxm];bool G[5005][5005];int LOW[MAXN],DFN[MAXN],STCK[MAXN] , Belong[maxn];int indx,top,block,bridge;bool ins[maxn],ecut[maxm];int n,m;int du[maxn];void tarjan (int s,int pre) {low [S]=dfn[s]=++indx; Stck[top++]=s; Ins[s]=1; for (int e=first[s];~e;e=nex[e]) {if (v[e]==pre) continue; if (!dfn[v[e]]) { Tarjan (v[e],s); Low[s]=min (Low[s],low[v[e]); if (Low[v[e]]>dfn[s]) {bridge++; Ecut[e]=ecut[e^1]=1; }} else if (Ins[v[e]]) low[s]=min (Low[s],dfn[v[e]); } if (Low[s]==dfn[s]) {int V; block++; do {v=stck[--top]; Ins[v]=false; Belong[v]=block; }while (v!=s); }}void solve (int n) {clr (dfn,0); CLR (ins,0); Indx=block=top=0; for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (1,0);} void add_ (int a,int b) {u[ecnt]=a; V[ecnt]=b; ecut[ecnt]=0; Nex[ecnt]=first[a]; first[a]=ecnt++;} int main () {int a A, B; while (~SCANF ("%d%d", &n,&m)) {ecnt=0;clr (g,false); CLR (first,-1); for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b); if (A!=b&&!g[a][b]) g[a][b]=g[b][a]=1,add_ (A, B), add_ (B,a); } solve (n); CLR (du,0); For(int i=0;i<ecnt;i++) if (Belong[u[i]]!=belong[v[i]]) {du[belong[u[i]]]++; } int sum=0; for (int i=1;i<=block;i++) if (du[i]==1) sum++; printf ("%d\n", (sum+1)/2); } return 0;}
POJ3177 redundant Paths (double-link shrinking point)