The main idea: to give an no-map and Q-query, each time asked to add a non-edge, the output to increase the number of remaining bridge after the edge.
Algorithm: Similar to the method of cutting points, first do a DFS to find all the bridge, and maintain the Dfs tree, when a query to join an edge (A, A, b), will be a ring on the DFS, the bridge on this ring into a non-bridge, this ring must go through a and B LCA, At this point we simply mark the tree edge of the bridge as a non-bridge in the process of evaluating the LCA, and cnt_bridge--the output.
It is important to note that the tree edge number is represented by the number of the node pointed to by the tree edge, such as the tree edge <u,v> denoted by the number v.
There is also the addition of a #pragma pre-processing instructions to prevent the explosion stack.
1#include <iostream>2#include <stdio.h>3#include <cstring>4#include <vector>5 using namespacestd;6 #pragmaComment (linker, "/stack:10240000,10240000")7 8 Const intMAXN =100000+Ten;9 intLOW[MAXN],PRE[MAXN], ISCUT[MAXN], dfs_clock=0;Ten BOOLISBRIDGE[MAXN]; Onevector<int>G[MAXN]; A intCnt_bridge; - intFATHER[MAXN]; - the intDfsintUintFA) - { -father[u]= Day - intLowu = Pre[u] = + +Dfs_clock; + intChild =0; - for(inti =0; I < g[u].size (); i++) + { A intv =G[u][i]; at if(!pre[v])//No access to V - { -child++; - intLOWV =Dfs (v, u); -Lowu = min (Lowu, LOWV);//update yourself with the low function of the descendant - if(Lowv > Pre[u])//determine if the Edge (U,V) is a bridge in { -isbridge[v]=true; tocnt_bridge++; + } - } the Else if(Pre[v] < Pre[u] && v! =FA) * { $Lowu = min (Lowu, pre[v]);//update yourself with a reverse edgePanax Notoginseng } - } the if(FA <0&& Child = =1) +Iscut[u] =0; A returnlow[u]=Lowu; the } + - voidInitintN) $ { $memset (Isbridge,false,sizeofIsbridge); -memset (PRE,0,sizeofpre); -cnt_bridge=dfs_clock=0; the for(intI=0; i<n; i++) - {Wuyi g[i].clear (); the } - } Wu - voidLCA (intAintb) About { $ while(pre[a]>Pre[b]) - { - if(Isbridge[a]) - { Aisbridge[a]=false; +cnt_bridge--; the } -A=Father[a]; $ } the while(pre[b]>Pre[a]) the { the if(Isbridge[b]) the { -isbridge[b]=false; incnt_bridge--; the } theb=Father[b]; About } the if(a!=b) the { the while(pre[a]>Pre[b]) + { - if(Isbridge[a]) the {Bayiisbridge[a]=false; thecnt_bridge--; the } -A=Father[a]; - } the while(pre[b]>Pre[a]) the { the if(Isbridge[b]) the { -isbridge[b]=false; thecnt_bridge--; the } theb=Father[b];94 } the } the } the 98 intMain () About { - #ifndef Online_judge101Freopen ("In.txt","R", stdin);102 #endif103 104 intN,m,case=1; the while(SCANF ("%d%d", &n,&m),! (n==0&& m==0))106 {107 init (n);108 for(intI=0; i<m; i++)109 { the intb;111scanf"%d%d",&a,&b); thea--;113b--; the G[a].push_back (b); the G[b].push_back (a); the }117 118 //The first time DFS finds all the bridges119Dfs0,-1); - intQ;121Cin>>Q;122printf"Case %d:\n", case++);123 while(q--)124 { the intb;126scanf"%d%d",&a,&b);127a--; -b--;129 LCA (A, b); theprintf"%d\n", Cnt_bridge);131 if(q==0) theprintf"\ n");133 }134 }135 136 return 0;137}
Hdu2460-network: The double connected component of the Edge