【POJ3694】Network

9 篇文章 0 订阅
8 篇文章 0 订阅

Network


  • Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can’t be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.
You are to help the administrator by reporting the number of bridges in the network after each new link is added.

  • Input Format

The input consists of multiple test cases. Each test case starts with a line containing two integers N(1≤N≤100,000) and M(N-1≤M≤200,000).
Each of the following M lines contains two integers A and B (1≤A≠B≤N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q (1≤Q≤1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1≤A≠B≤N), which is the i-th added new link connecting computer A and B.
The last test case is followed by a line containing two zeros.

  • Output Format

For each test case, print a line containing the test case number(beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

  • Sample Input

3 2
1 2
2 3
2
1 2
1 3
4 4
1 2
2 1
2 3
1 4
2
1 2
3 4
0 0

  • Sample Output

Case 1:
1
0

Case 2:
2
0

  • Hint

【题目大意】
先给你一张图,然后再给你若干条边,问每加入一条边后这张图剩余的桥有多少?


  • 分析

首先我们用先用tarjan把图中的强连通分量缩环成点,构成一张新图,新图中的边就都是桥了,而且还是一颗树。然后对于每次询问的两个点(u,v),从u到v的路径上所有的桥都不是桥了,所以我们把从u到v的路径上所有点都染成同样颜色,每染掉一个不同颜色的点就相当于去掉一个桥。这个地方需要用到树上倍增和并查集,因为N的范围很大。
还有就是多组数据所以要注意清空操作。


#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MaxN 100001
#define MaxM 200002
#define MaxQ 1001
using namespace std;
int n,m,q,u,v,tot,Tot,Case=1,Tarjan_Time,Color_Num,Low[MaxN],Dfn[MaxN],last[MaxN],Last[MaxN],Deep[MaxN],Father[MaxN],Colored[MaxN],IsBridge[2*MaxM],ColorIs[2*MaxN];
queue <int> Q;
struct Data1{int from,to,next,back;}E[2*MaxM];
struct Data2{int to,next;}G[2*MaxN];
void Addline1(int u,int v){
    E[++tot].from=u;E[tot].to=v;E[tot].next=last[u];E[tot].back=tot+1;last[u]=tot;
    E[++tot].from=v;E[tot].to=u;E[tot].next=last[v];E[tot].back=tot-1;last[v]=tot;
}
void Addline2(int u,int v){
    G[++Tot].to=v;G[Tot].next=Last[u];Last[u]=Tot;
    G[++Tot].to=u;G[Tot].next=Last[v];Last[v]=Tot;
}
void Clear(){
    memset(Low,0,sizeof(Low));
    memset(Dfn,0,sizeof(Dfn));
    memset(last,0,sizeof(last));
    memset(Last,0,sizeof(Last));
    memset(Deep,0,sizeof(Deep));
    memset(Father,0,sizeof(Father));
    memset(Colored,0,sizeof(Colored));
    memset(IsBridge,0,sizeof(IsBridge));
    for (int i=1;i<=n;i++) ColorIs[i]=i;
    tot=Tot=Tarjan_Time=Color_Num=0;
}
int Tarjan(int fa,int u){
    Dfn[u]=Low[u]=++Tarjan_Time;
    for (int i=last[u];i;i=E[i].next){
        int v=E[i].to;
        if (!Dfn[v]){
            Low[u]=min(Low[u],Low[v]=Tarjan(u,v));
            if (Low[v]>Dfn[u]) IsBridge[i]=IsBridge[E[i].back]=1;
        }
        else if (Dfn[v]<Dfn[u] && v!=fa) Low[u]=min(Low[u],Dfn[v]);
    }
    return Low[u];
}
void Color(int u,int C){
    for (Q.push(u),Colored[u]=C;!Q.empty();Q.pop()){
        u=Q.front();
        for (int i=last[u];i;i=E[i].next){
            if (Colored[E[i].to] || IsBridge[i]) continue;
            Q.push(E[i].to); Colored[E[i].to]=C;
        }
    }
}
void GetDeep(int fa,int u){
    Deep[u]=Deep[fa]+1; Father[u]=fa;
    for (int i=Last[u];i;i=G[i].next){
        if (fa==G[i].to) continue;
        GetDeep(u,G[i].to);
    }
}
int GetColor(int x){return ColorIs[x]==x?x:ColorIs[x]=GetColor(ColorIs[x]);}
void Solve(int u,int v){
    if (Deep[v]>Deep[u]) swap(u,v);
    for (int fu=Father[u];Deep[u]>Deep[v];u=fu,fu=Father[u]){
        int C_fu=GetColor(fu),C_u=GetColor(u);
        if (C_fu!=C_u) ColorIs[C_fu]=C_u,Color_Num--;
    }
    for (int fu=Father[u],fv=Father[v];u!=v;u=fu,fu=Father[u],v=fv,fv=Father[v]){
        int C_fu=GetColor(fu),C_u=GetColor(u);
        if (C_fu!=C_u) ColorIs[C_fu]=C_u,Color_Num--;
        int C_fv=GetColor(fv),C_v=GetColor(v);
        if (C_fv!=C_v) ColorIs[C_fv]=C_v,Color_Num--;       
    }
    printf("%d\n",Color_Num-1);
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    for (scanf("%d%d",&n,&m);n || m;scanf("%d%d",&n,&m)){
        printf("Case %d:\n",Case++);
        Clear();
        for (int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            Addline1(u,v);
        }
        Tarjan(0,1);
        for (int i=1;i<=n;i++) if (!Colored[i]) Color(i,++Color_Num);
        for (int i=1;i<=tot;i+=2)
            if (IsBridge[i]) Addline2(Colored[E[i].from],Colored[E[i].to]);
        GetDeep(0,1);
        for (scanf("%d",&q);q;q--){
            scanf("%d%d",&u,&v);
            Solve(Colored[u],Colored[v]);
        }
        printf("\n");
    }
    fclose(stdin); fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值