is to find out the number of bridges in the original diagram, add a new edge to each query, and add the number of bridges remaining in the current edge graph.
The number of bridges in the original diagram is calculated, and then the number of bridges between the two ends of the new join edge is subtracted, which is the number of remaining bridges.
To put together (that is, a bridge between two points) that belongs to the same set.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<algorithm>#include<vector>#defineMem (A, B) memset (A, B, sizeof (a))using namespacestd;Const intMAXN =400100, INF =0x7fffffff;intPRE[MAXN], F[MAXN], PA[MAXN];intDfs_clock;intN, M, Ret;vector<int>G[MAXN];intFindintx) { returnF[x]==x?x: (f[x]=find (F[x]));}intUnion (intUintv) { intR =find (U); intL =Find (v); if(r = = L)return 0; F[R]=l; return 1;}intDfsintUintFA) {Pa[u]=FA; intLowu = Pre[u] = + +Dfs_clock; for(intI=0; I<g[u].size (); i++) { intv =G[u][i]; if(!Pre[v]) { //Pa[v] = u; intLOWV =Dfs (v, u); Lowu=min (Lowu, LOWV); if(Lowv >Pre[u]) ret++; ElseUnion (U, v); //If there is no bridge between U and V, then you and V belong to a set } Else if(Pre[v] < Pre[u] && v! =FA) Lowu=min (Lowu, pre[v]); } returnLowu;}intLcaintUintv) { intR =find (U); intL =Find (v); if(r = =l)returnret; if(Pre[u] >Pre[v]) Swap (U, v); while(Pre[u] <Pre[v]) { if(Union (pa[v], v)) RET--; V=Pa[v]; } while(U! = V)//v after the previous while either U or the nearest public ancestor of U and v { if(Union (U, Pa[u]) ret--; U=Pa[u]; } returnret;}voidinit () {mem (Pre,0); Mem (PA,0); for(intI=0; i<=n; i++) F[i] =i; Dfs_clock=0; RET=0;}intMain () {intKase =0; while(cin>> n >> m && n+m) {init (); for(intI=0; i<m; i++) { intu, v; CIN>> u >>v; G[u].push_back (v); G[v].push_back (U); } DFS (1, -1); intQ; CIN>>Q; printf ("Case %d:\n",++Kase); for(intI=0; i<q; i++) { intu, v; CIN>> u >>v; cout<< LCA (U, v) <<Endl; } } return 0;}
Network POJ-3694 (LCA and check set + connected graph for bridge)