Divide the modification operation according to time , set up $solve(l,r,n,m)$ For the sake of time $[l,r]$ Modification operation of , The scope of action is $n$ A little bit ,$m$ A graph of bars and edges .

if $l=r$, Then violence Tarjan Count the number of bridge sides .

Otherwise, extract $[l,r]$ All the edges involved in the modification $E$, And mark the endpoint as $V$, obviously $|V|=O(|E|)$.

Join in $m$ Except for $E$ All the edges outside , Obviously, no matter how it's modified , These edges will exist .

Tarjan Find the edge biconnected component , Double shrink the edge , You can get a forest , Then non tree side can never be a bridge , Just delete it .

It can also be noted that , The endpoint of an edge that has not been joined can only be in $V$ in , So we can find out $V$ The virtual tree of China , The edge not on the virtual tree is always a bridge , Add the answer and delete it .

And on the edge of the virtual tree , Each compressed edge represents a chain , Additional records are required. This tree edge represents several bridge edges $w$, When this tree becomes a bridge , The answer should be to add $w$.

After the above steps ,$n$ and $m$ All are reduced to $O(|V|)=O(|E|)=O(r-l)$.

take $mid=\lfloor\frac{l+r}{2}\rfloor$, recursive $solve(l,mid,n',m')$, Then violence deals with all $[l,mid]$ Modification of , Re recursion $solve(mid+1,r,n',m')$ that will do .

Time complexity $T(n)=2T(\frac{n}{2})+O(n)=O(n\log n)$.

#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int>P;
const int N=100010,M=100010,K=19;
int n,m,i,ans[M];
bool use[M],ex[M],cut[M],vis[N];
int g[N],v[M<<1],w[M<<1],nxt[M<<1],ed,f[N],dfn[N],low[N],num,from[N];
int G[N],V[N<<1],W[N<<1],NXT[N<<1],ED,id[N],sum[N],vip[N];
int O,ce,all,pos[M],q[K][M];
map<P,int>T;
struct E{
int x,y,w;
E(){}
E(int _x,int _y,int _w){x=_x,y=_y,w=_w;}
}e[K][M];
inline int ask(int x,int y){
if(x==y)return 0;
if(x>y)swap(x,y);
int&t=T[P(x,y)];
if(!t)e[0][t=++ce]=E(x,y,0);
return t;
}
inline void add(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;}
inline void ADD(int x,int y,int z){V[++ED]=y;W[ED]=z;NXT[ED]=G[x];G[x]=ED;}
void tarjan(int x){
dfn[x]=low[x]=++num;
for(int i=g[x];i;i=nxt[i])if(!dfn[v[i]]){
f[v[i]]=w[i],tarjan(v[i]);
if(low[x]>low[v[i]])low[x]=low[v[i]];
}else if(f[x]!=w[i]&&low[x]>dfn[v[i]])low[x]=dfn[v[i]];
if(f[x]&&low[x]==dfn[x])cut[f[x]]=1;
}
void dfs(int x,int y){
from[x]=y;
for(int i=g[x];i;i=nxt[i])if(!from[v[i]]&&!cut[w[i]])dfs(v[i],y);
}
inline void newedge(int x,int y,int w){all-=w;e[O][++ce]=E(x,y,w);}
void compress(int x,int y){
int d=0;
vis[x]=1;
for(int i=G[x];i;i=NXT[i]){
int u=V[i];
if(u==y)continue;
sum[u]=sum[x]+W[i];
compress(u,x);
if(!id[u])continue;
d++;
id[x]^=id[u];
}
if(d>1)vip[x]=1;
if(vip[x]){
for(int i=G[x];i;i=NXT[i]){
int u=V[i];
if(u==y)continue;
int t=id[u];
if(t)newedge(x,t,sum[t]-sum[x]);
}
id[x]=x;
}
}
void solve(int o,int l,int r,int n,int m,int pre){
O=o+1;
int i;
if(l==r){
for(i=1;i<=m;i++)cut[i]=0;
for(ed=0,i=1;i<=n;i++)g[i]=f[i]=dfn[i]=low[i]=from[i]=0;
num=0;
e[o][q[o][l]].w^=1;
for(i=1;i<=m;i++)if(e[o][i].w){
int x=e[o][i].x,y=e[o][i].y;
add(x,y,i),add(y,x,i);
}
for(i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(i=1;i<=m;i++)if(cut[i])pre+=e[o][i].w;
e[o][q[o][l]].w^=1;
ans[l]=pre;
return;
}
for(i=1;i<=m;i++)use[i]=cut[i]=pos[i]=0;
for(ed=0,i=1;i<=n;i++)g[i]=f[i]=dfn[i]=low[i]=from[i]=0;
num=0;
int cnt=0;
for(i=l;i<=r;i++)use[q[o][i]]=1;
for(i=1;i<=m;i++)if(!use[i]&&e[o][i].w){
int x=e[o][i].x,y=e[o][i].y;
add(x,y,i),add(y,x,i);
}
for(i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(i=1;i<=n;i++)if(!from[i])dfs(i,++cnt);
for(ED=0,i=1;i<=cnt;i++)vis[i]=vip[i]=G[i]=id[i]=sum[i]=0;
ce=all=0;
for(i=1;i<=m;i++)if(!use[i]&&e[o][i].w){
int x=e[o][i].x,y=e[o][i].y;
x=from[x],y=from[y];
if(x==y)continue;
ADD(x,y,e[o][i].w),ADD(y,x,e[o][i].w);
all+=e[o][i].w;
}
for(i=l;i<=r;i++){
int t=q[o][i];
if(!t)continue;
vip[from[e[o][t].x]]=vip[from[e[o][t].y]]=1;
}
for(i=1;i<=cnt;i++)if(vip[i]&&!vis[i])compress(i,0);
int mid=(l+r)>>1,_ce=ce,cv=0;
for(i=1;i<=cnt;i++)if(vip[i])vip[i]=++cv;
pre+=all;
for(i=1;i<=ce;i++)e[o+1][i].x=vip[e[o+1][i].x],e[o+1][i].y=vip[e[o+1][i].y];
for(i=1;i<=m;i++)if(use[i]){
int x=e[o][i].x,y=e[o][i].y;
e[o+1][++ce]=E(vip[from[x]],vip[from[y]],e[o][i].w);
pos[i]=ce;
}
for(i=l;i<=r;i++)q[o+1][i]=pos[q[o][i]];
solve(o+1,l,mid,cv,ce,pre);
ce=_ce;
for(i=1;i<=m;i++)use[i]=0;
for(i=l;i<=r;i++)use[q[o][i]]=1;
for(i=1;i<=m;i++)if(use[i])ex[i]=e[o][i].w;
for(i=l;i<=mid;i++)ex[q[o][i]]^=1;
for(i=1;i<=m;i++)if(use[i])e[o+1][++ce].w=ex[i];
solve(o+1,mid+1,r,cv,ce,pre);
}
int main(){
freopen("bridges3.in","r",stdin);freopen("bridges3.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
char s[9];
int x,y;
scanf("%s%d%d",s,&x,&y);
q[0][i]=ask(x,y);
}
solve(0,1,m,n,ce,0);
for(i=1;i<=m;i++)printf("%d\n",ans[i]);
}

Bridges: The Final Battle More articles about

  1. hdu 3986 Harry Potter and the Final Battle ( Shortest path )

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  2. hdu3986Harry Potter and the Final Battle

    Here's an undirected graph , And find the shortest path , Remove any edge in the shortest path , Look at the worst case , In the new picture , What is the shortest length from the first point to the end point . Here's what I did : First, find the shortest path , Then record the path , Delete one more side by side , Delete ...

  3. hdu 3986 Harry Potter and the Final Battle

    A water problem WA 了 60 Hair , The array is not big , this OJ No hint RE, Light hint WA...... Ideas : Find the shortest path first , If the deleted edge is not on the shortest path , So there's no impact on the results , To have an impact , Only the edge on the shortest path can be deleted . So enumerate the shortest path ...

  4. [JZOJ6089]【CodeChef 2014 April Challenge】Final Battle of Chef【 data structure 】【 The whole score is two points. 】

    Description \(n,q,V\leq 100000,w_i\leq 10^9\) Solution Another big data structure Because there is a rounding down , As a result, the modified values at different times cannot be simply added together ...

  5. 【Dijstra Heap optimization 】HDU 3986 Harry Potter and the Final Battle

    http://acm.hdu.edu.cn/showproblem.php?pid=3986 [ The question ] Given an undirected graph with multiple edges ,T=20,n<=1000,m<=5000 Delete an edge , bring 1 ...

  6. Final Battle #1 K topic Fire game

    Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...

  7. 【 turn 】 shortest path &amp; Difference constrained problem set

    from :http://blog.csdn.net/shahdza/article/details/7779273 shortest path [HDU] 1548 A strange lift The shortest path of the foundation ( or bfs)*254 ...

  8. 【 Reprint 】 graph theory 500 topic —— Mainly for hdu/poj/zoj

    from ——http://blog.csdn.net/qwe20060514/article/details/8112550 ============================= Here is the minimum spanning tree + and ...

  9. Cheating father CF April Fools Day Contest Answer key

    H - A + B Strikes Back A + B is often used as an example of the easiest problem possible to show som ...

Random recommendation

  1. C++Builder XE8_upd1 Successfully cracked the installation

    1. To download XE8, Installation failed .2. Download after XE8U1(delphicbuilder_xe8_upd1_subscription.iso), install , choice DelPhi.C++builder, Enter the serial number :3. install ...

  2. [SharpDevelop] Menu status update

    Mode one stay Idle Update in method void OnApplicationIdle(object sender, EventArgs e) { // Use the Idle event to update ...

  3. Cocos2d-js Using texture objects to create Sprite object

    In this section, we will introduce texture object creation through an example Sprite Object use , This example is shown in the figure 5-2 Shown , The grass on the ground is placed in the background ( As shown in the figure below ) Medium , The two trees in the scene are shown from the back “ Trees ” The texture is captured from the image , chart 5-5 This is a tree ...

  4. Illustrator In software eps and ai The difference in format

    from Illustrator In software eps and ai The difference in format AI yes ILL Unique format ,EPS Format is often used in the field of typesetting .AI The bitmap image in is stored as a link ,EPS Format contains the bitmap image in the file . For containing phases ...

  5. java Multithreading ( Synchronization and deadlock issues , Producer and consumer issues )

    First, let's look at synchronization and deadlock : The so-called deadlock , Namely A Have banana.B Have apple. A Yes B say : You put apple Give me a . I just put banana Here you are. . B Yes A say : You put banana Give me a , I just put apple Here you are. . But ...

  6. ucenter No two way synchronization setting[allowsynlogin] by 0 Problem solving

    Explore in depth ucenter All kinds of communication failure problems ITWeb Problem description :A,B Two applications ,A Log in to B, and B Can't sync to A, That is, only from A One way synchronization to B,AB There's no two-way synchronization between them , Record this time and check ...

  7. ST-1 It's a mess bug

    One of the mistakes that impressed me most was the garbled code . Do... At the end of school web The final assignment is , I met a lot of garbled problems . The problem of garbled code is not the logic error of the program itself , But it makes the usability of the program very poor . Only when you input English can you judge whether the result is correct or not . And the compiler ...

  8. ES6 Array extension

    In front of the word Arrays are a basic JS object , Over time ,JS The rest of the world has been evolving , And until ES5 The standard introduces some new methods to simplify the use of array objects .ES6 Standards continue to improve arrays , A lot of new features have been added . This article will introduce in detail ES6 Array expansion ...

  9. Compile from the command line Wordcount

    1. edit WordCount.java file , In download hadoop There are... In the installation package WordCount Example http://mirrors.hust.edu.cn/apache/hadoop/common/ha ...

  10. Time range (447392) for take &#39;Take 001&#39; is larger than maximum allowed(100000).

    http://www.cnblogs.com/lopezycj/archive/2012/05/16/unity3d_tuchao.html https://forum.unity3d.com/thr ...