[HDU2460] Network [2008 Asia Hefei Regional Contest Online E]

来源:互联网 发布:大数据 veracity 编辑:程序博客网 时间:2024/05/06 09:34

题意

给出一个无向图以及Q次询问,每次询问增加一条无向边,要求输出增加这条边后剩余的桥的数目。

题解

先做一次dfs求出所有的桥,并且记录这棵dfs树,当一次询问加入一条边(a,b)之后,会在dfs上形成一个环,在这个环上的桥都变为非桥,这个环肯定经过a和b的LCA,此时我们只需在求LCA的过程中把经过的为桥的树边标记为非桥,同时cnt_bridge–再输出即可。

代码

/****************************************\* Author : ztx* Title  : E - Network* ALG    :* CMT    : g++  I64d* Time   :\****************************************/#include <cstdio>#define Rep(i,l,r) for(i=(l);i<=(r);i++)#define rep(i,l,r) for(i=(l);i< (r);i++)#define Rev(i,r,l) for(i=(r);i>=(l);i--)#define rev(i,r,l) for(i=(r);i> (l);i--)typedef long long ll ;typedef double lf ;int CH , NEG ;template <typename TP>inline void read(TP& ret) {    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;    if (CH == '-') NEG = true , CH = getchar() ;    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;    if (NEG) ret = -ret ;}template <typename TP>inline void readc(TP& ret) {    while (ret=getchar() , ret<'!') ;    while (CH=getchar() , CH>'!') ;}template <typename TP>inline void reads(TP *ret) {    ret[0]=0;while (CH=getchar() , CH<'!') ;    while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;    ret[ret[0]+1]=0;}#include <cstring>#define  maxn  100010LL#define  maxm  200010LLstruct FST { int to,next;bool vis; } e [maxm<<1] ;int star[maxn] , tote=1 ;void AddEdge(int u,int v) {    e[++tote].to=v;e[tote].next=star[u];star[u]=tote;e[tote].vis=false;    e[++tote].to=u;e[tote].next=star[v];star[v]=tote;e[tote].vis=false;}int n , m , kiss = 0 , cnt_bcc = 0 ;int dfn[maxn] , low[maxn] , par[maxn] , idx ;bool vis[maxn] , istop[maxn] ;void BCC(int u) {// build a treeint v,p;    vis[u] = true ;    dfn[u] = low[u] = ++idx ;    for (p=star[u],v=e[p].to;p;p=e[p].next,v=e[p].to) if (!e[p].vis) {        e[p].vis = e[p^1].vis = true ;        if (!vis[v]) {            par[v] = u ;            BCC(v) ;            if (low[v] < low[u]) low[u] = low[v] ;            if (dfn[u] < low[v]) {                // a new bcc                cnt_bcc++;                istop[v]=1;            }        } else if (dfn[v] < low[u]) low[u] = dfn[v] ;    }}inline void Union(int u,int v) {    // two conditions    // one chain or have LCA    if (low[u] == low[v]) return ;    while (u != v) {        if (dfn[u] < dfn[v]) u^=v,v^=u,u^=v ;        if (istop[u]) cnt_bcc -- , istop[u] = false ;        u = par[u] ;    }}int main() {int i , u , v , q ;//    #define READ    #ifdef  READ        freopen(".in" ,"r",stdin ) ;        freopen(".out","w",stdout) ;    #endif    while (++kiss) {        read(n) , read(m) ;        if (n+m == 0) break ;        memset(star,0,sizeof star) ; tote = 1 ;        memset(dfn,0,sizeof dfn) ;        memset(low,0,sizeof low) ;        memset(vis,0,sizeof vis) ;        memset(istop,0,sizeof istop) ;        idx = 0 ;        cnt_bcc = 0 ;        Rep (i,1,n) par[i] = i ;        Rep (i,1,m)            read(u) , read(v) , AddEdge(u,v) ;        BCC(1) ;        printf("Case %d:\n",kiss) ;        read(q) ;        while (q -- ) {            read(u) , read(v) ;            Union(u,v) ;            printf("%d\n", cnt_bcc) ;        }        puts("") ;    }    #ifdef  READ        fclose(stdin) ; fclose(stdout) ;    #else        getchar() ; getchar() ;    #endif    return 0 ;}
0 0
原创粉丝点击