Topic Links:
Poj3694
Test instructions
A connected graph of n(1≤ n ≤100,000) points and m(n -1≤ m ≤200,000) is given.
have q (1≤ Q ≤1,000) Ask each query to add an edge (accumulate)
The number of bridges remaining after each additional edge is output
Exercises
10W Point Plus 1000 queries every time you ask for a tarjin algorithm, you'll definitely time out.
Consider the essence of a-B at a time of adding one edge:
All cut edges in the path from A to B will disappear
So how do you record this path?
Build a tree by the edge of the Tarjan algorithm during recursion
Then each query starts from a B until the edges of their nearest public ancestor disappear.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #define MAXN 100050using namespace std;struct node{int to,next;} Edge[maxn*4];int head[maxn];int s;int dfn[maxn],low[maxn],num;int level[maxn],fa[maxn];int flag[maxn];int n,ans;void Init () {memset (head,-1,sizeof (head)); memset (dfn,0,sizeof (DFN)); memset (flag,0,sizeof (flag)); s=ans=num=0; level[0]=0; for (int i=1;i<=n;i++) fa[i]=i;} void Addedge (int a,int b) {Edge[s]={b,head[a]}; head[a]=s++;} void LCA (int a,int b) {while (Level[b]>level[a]) {if (Flag[b]) ans--, flag[b]=0; B=FA[B]; } while (Level[a]>level[b]) {if (Flag[a]) ans--, flag[a]=0; A=fa[a]; } while (a!=b) {if (Flag[a]) ans--, flag[a]=0; if (Flag[b]) ans--, flag[b]=0; A=FA[A],B=FA[B]; }}void Tarjan (int u,int pre) {dfn[u]=low[u]=++num; level[u]=level[pre]+1; Achievements for (inT i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to; if (!dfn[v]) {fa[v]=u; Tarjan (V,u); Low[u]=min (Low[u],low[v]); if (Dfn[u]<low[v])//Mark cutting edge flag[v]=1,ans++; } if (V!=pre) low[u]=min (Low[u],dfn[v]); }}int Main () {int m,a,b; int case=1; while (scanf ("%d%d", &n,&m) && (n+m)) {init (); while (m--) {scanf ("%d%d", &a,&b); Addedge (A, b); Addedge (B,a); } Tarjan (1,0); scanf ("%d", &m); printf ("Case%d:\n", case++); while (m--) {scanf ("%d%d", &a,&b); if (ans!=0) LCA (A, b); printf ("%d\n", ans); } printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ3694 Network Cutting Edge +lca