poj 3694 Network

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9123 Accepted: 3390

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

提示

题意:

一个网络管理者管理一个庞大的网络,网络包括n(1<=n<=100000)台电脑,m(1<=m<=200000)条网线,这m条网线是双向的。有时会因为某些因素导致网线损坏,如果有一条重要网线(如果该网线不存在则会导致网络不能让每台电脑相互传递信息)损坏会使得管理员很困扰,所以他将在该网络上增加q(1<=q<=1000)条网线,请你计算出每增加一条网线就输出所谓重要网线的数量。

思路:

这题同样对图的综合性要求很高,LCA(最近公共祖先)+tarjan算法求割边(桥)。

割边:该边保证了整个图的连通性,没有这条边会导致整个图不连通。

这题点少还可以直接上tarjan算法就行了,所以用LCA来简化步骤。

先给出我所参考学长代码吧:

子祥队长:http://blog.csdn.net/challengerrumble/article/details/50737162

标神:http://blog.csdn.net/huayunhualuo/article/details/50557689

用并查集可以优化时间复杂度,但要用并查集还需要压缩路径。

不过感觉自己对tarjan理解还不是很深刻,有错还请多多包涵。

示例程序

Source Code

Problem: 3694		Code Length: 2347B
Memory: 10536K		Time: 516MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int v,next;
}w[400000];
int h[100001],numw,num,dfn[100001],low[100001],vis[100001],p[100001],father[100001];
int min(int x,int y)
{
    if(x<y)
    {
        return x;
    }
    else
    {
        return y;
    }
}
void insert(int u,int v)
{
    w[numw].v=v;
    w[numw].next=h[u];
    h[u]=numw;
    numw++;
    w[numw].v=u;
    w[numw].next=h[v];
    h[v]=numw;
    numw++;
}
int find(int x)
{
    if(father[x]==-1)
    {
        return x;
    }
    else
    {
        father[x]=find(father[x]);				//并查集路径压缩否则会TLE
        return father[x];
    }
}
void Union(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)
    {
        father[x]=y;
    }
}
void tarjan(int s,int t,int deep)
{
    int i;
    vis[t]=0;
    p[t]=s;
    dfn[t]=deep;
    low[t]=deep;
    for(i=h[t];i!=-1;i=w[i].next)
    {
        if(w[i].v!=s&&vis[w[i].v]==0)
        {
            low[t]=min(low[t],dfn[w[i].v]);
        }
        if(vis[w[i].v]==-1)
        {
            tarjan(t,w[i].v,deep+1);
            low[t]=min(low[t],low[w[i].v]);
            if(low[w[i].v]>dfn[t])			//该边为桥
            {
                num++;
            }
            else					//缩点
            {
                Union(w[i].v,t);
            }
        }
    }
    vis[t]=1;
}
void judge(int x)
{
    int y;
    y=find(p[x]);
    x=find(x);
    if(x!=y)
    {
        father[x]=y;
        num--;
    }
}
void lca(int x,int y)
{
    while(dfn[x]>dfn[y])
    {
        judge(x);
        x=p[x];
    }
    while(dfn[x]<dfn[y])
    {
        judge(y);
        y=p[y];
    }
    while(x!=y)
    {
        judge(x);
        judge(y);
        x=p[x];
        y=p[y];
    }
}
int main()
{
    int n,m,i,i1,u,v,q;
    scanf("%d %d",&n,&m);
    for(i=1;n!=0||m!=0;i++)
    {
        numw=0;
        num=0;
        memset(h,-1,sizeof(h));
        memset(father,-1,sizeof(father));		//并查集数组以-1做初始化,要以数组下标做初始化也可以
        memset(vis,-1,sizeof(vis));
        for(i1=1;m>=i1;i1++)
        {
            scanf("%d %d",&u,&v);
            insert(u,v);
        }
        tarjan(0,1,1);					//tarjan预处理
        scanf("%d",&q);
        printf("Case %d:\n",i);
        for(i1=1;q>=i1;i1++)
        {
            scanf("%d %d",&u,&v);
            if(find(u)!=find(v))				//最近公共祖先
            {
                lca(u,v);
            }
            printf("%d\n",num);
        }
        printf("\n");
        scanf("%d %d",&n,&m);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值