Test instructions: A non-graphic can have a heavy edge, the following Q operation, each time between two points connected to a forward edge, each connection after the entire graph left how many bridges (note is to consider the previous connected side, each answer is on the previous basis)
/*Tarjan+lca first with Tarjan, then this graph will become a tree, when we are not in the same node, it is equivalent to the tree two nodes together, the two nodes with their common ancestor will form a new indentation, the number of each statistic bridge. */#include<iostream>#include<cstdio>#include<cstring>#defineM 200010using namespacestd;intn,m,num,head[m],low[m],dfn[m],topt;intS[m],top,f[m],sum,belong[m],cnt,cas;intNum,head[m],fa[m],c[m],vis[m];structnode{intV,pre;} e[m*2];structnode{intV,pre;} e[m*2];voidADD (int from,intTo ) {E[NUM].V=to ; E[num].pre=head[ from]; head[ from]=num++;}voidAddint from,intTo ) {E[NUM].V=to ; E[num].pre=head[ from]; head[ from]=num++;}voidTarjan (intXintFA) {Low[x]=dfn[x]=++topt; s[++top]=x;f[x]=1; for(inti=head[x];i!=-1; i=e[i].pre) { intv=e[i].v; if(i== (fa^1))Continue; if(dfn[v]==0) {Tarjan (v,i); Low[x]=min (low[x],low[v]); } Else if(F[v]) low[x]=min (low[x],dfn[v]); } if(low[x]==Dfn[x]) {Sum++; while(x!=S[top]) {F[s[top]]=0; belong[s[top]]=sum;top--; } F[s[top]]=0; belong[s[top]]=sum;top--; }}voidDfs (intNowint from,intDEP) {Fa[now]= from; c[now]=DEP; for(inti=head[now];i!=-1; i=e[i].pre)if(e[i].v!= from) Dfs (E[I].V,NOW,DEP+1);}voidLCA (intAintb) { if(c[a]<c[b]) swap (A, b); intt=c[a]-C[b]; for(intI=1; i<=t;i++) { if(vis[a]==0) cnt--; Vis[a]=1; a=Fa[a]; } while(a!=b) {if(vis[a]==0) cnt--; Vis[a]=1; a=Fa[a]; if(vis[b]==0) cnt--; VIS[B]=1; b=Fa[b]; }}intMain () { while(1) {scanf ("%d%d",&n,&m); if(n==0&&m==0) Break; memset (Head,-1,sizeof(head)); memset (Head,-1,sizeof(Head)); memset (Low,0,sizeof(low)); memset (DFN,0,sizeof(DFN)); Memset (F,0,sizeof(f)); Memset (Belong,0,sizeof(belong)); memset (Vis,0,sizeof(VIS)); Memset (c,0,sizeof(c)); memset (FA,0,sizeof(FA)); memset (s),0,sizeof(s)); Num=num=topt=sum=0; intu,v; for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); ADD (U,V); ADD (V,u); } for(intI=1; i<=n;i++) if(dfn[i]==0) Tarjan (i,-1); for(intu=1; u<=n;u++) for(inti=head[u];i!=-1; i=e[i].pre)if(belong[u]!=BELONG[E[I].V]) Add (BELONG[U],BELONG[E[I].V]); Dfs (1,1,0); cnt=sum-1; scanf ("%d",&m); printf ("Case %d:\n",++CAs); for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); intu=Belong[u]; intv=Belong[v]; if(u==V) {printf ("%d\n", CNT);Continue; } LCA (U,V); printf ("%d\n", CNT); } } return 0;}
View Code
Network (POJ 3694)