POJ3694-Network(LCA+边联通分量+桥)

63 篇文章 0 订阅
19 篇文章 0 订阅

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9714 Accepted: 3603

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

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

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

Source


题意:一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(注意要考虑之前连了的边,每次回答是在上一次的基础之上)

解题思路:边连通分量+LCA。首先运行一次tarjan,求出桥和缩点,那么无向图将缩点为一棵树,树边正好是原来的桥。每次连接两点,看看这两点是不是在同一个缩点内,如果是,那么缩点后的树没任何变化,如果两点属于不同的缩点,那么连接起来,然后找这两个缩点的LCA,因为从点u到LCA再到点v再到点u,将形成环,里面的树边都会变成不是桥。计数的时候注意


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

const int N=101000;

struct Node
{
    int v,id,nt,flag;
}edge[400020];
int s[N],cnt,dep;
int n,m;
int f[N],p[N];
int dfn[N],low[N],ans[N];//ans[N]用来记录桥的编号,或者可以用brideg[N][2]来记录桥的两个端点
bool vis[N];
int nbridge;

int Find(int x)
{
    return x==f[x]?x:(f[x]=Find(f[x]));
}

void Union(int a,int b)
{
    int x=Find(a),y=Find(b);
    if(x!=y) f[x]=y;
}

void AddEdge(int u,int v,int id)
{
    for(int i=s[u];~i;i=edge[i].nt)
    {
        if(edge[i].v==v)
        {
            edge[i].flag++;
            return ;
        }
    }
    edge[cnt].v=v;
    edge[cnt].nt=s[u];
    edge[cnt].flag=0;
    edge[cnt].id=id;
    s[u]=cnt++;
}

void tarjan(int u,int pre)
{
    vis[u]=true;
    dfn[u]=low[u]=dep++;
    for(int i=s[u];~i;i=edge[i].nt)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        if(!vis[v])
        {
            p[v]=u;
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]>=low[v]) Union(u,v);
            if(dfn[u]<low[v]&&!edge[i].flag)
                ans[nbridge++]=edge[i].id;
        }
        else low[u]=min(low[u],dfn[v]);
    }
}

void GetConnection()
{
    for(int i=0;i<=n;i++) f[i]=i;
    nbridge=0,dep=1;
    memset(vis,0,sizeof vis);
    tarjan(1,0);
}

int lca(int x,int y)
{
    if(dfn[x]<dfn[y]) swap(x,y);
    while(dfn[x]>dfn[y])
    {
        if(Find(x)!=Find(p[x]))
          nbridge--,f[Find(x)]=Find(p[x]);
        x=p[x];
    }
    while(y!=x)
    {
        if(Find(y)!=Find(p[y]))
          nbridge--,f[Find(y)]=Find(p[y]);
        y=p[y];
    }
    return nbridge;
}

int main()
{
    int cas=1;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        printf("Case %d:\n",cas++);
        memset(s,-1,sizeof s);
        cnt=0;
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            AddEdge(u,v,i),AddEdge(v,u,i);
        }
        GetConnection();
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            printf("%d\n",lca(u,v));
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值