Network poj 3694(tarjan 求割边 + lca(朴素的))

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9462 Accepted: 3517

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

题意:

给出一个N(1 ≤ N ≤ 100,000)个点 和 M(N - 1 ≤ M ≤ 200,000)的连通图.

有Q ( 1 ≤ Q ≤ 1,000)个询问 每次询问增加一条边(累加下去)

输出每增加一条边后剩下的桥的数量

题解:

10W点加1000次询问 每次询问都用Tarjin算法求一次肯定会超时的

考虑  每次加一条边a-b的实质:

从a到b的路径中的所有割边都将消失

那如何记录这条路径呢

在Tarjan算法递归的过程中通过树边建立一棵树  

然后每次询问分别从a  b开始 一直到它们的最近公共祖先 出现的割边全部消失即可

#include <stdio.h>
#include <string.h>

#define min(a,b) a>b?b:a
#define Max 100050
struct Edge
{
    int to;
    int next;
}edge[Max*4];

int head[Max],tol;
void add( int u,int v)//邻接表
{
    edge[tol].to = v;
    edge[tol].next = head[u];
    head[u] = tol++;
}
int dfn[Max],low[Max],vis[Max],bcnt,time;

int flag[Max];//存割边
int deep[Max];//存深度
int pre[Max];//存祖先节点
int ans;//计数割边

void tarjan(int u,int fa)//求割边
{
    dfn[u] = low[u] = ++time;
    deep[u] = deep[fa]+1;


    for( int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(!dfn[v])
        {
            pre[v] = u;
            tarjan(v,u);
            low[u] = min(low[u], low[v]);
            if(dfn[u] < low[v])
            {
                flag[v] = 1;//统计割边,并计数
                ans++;
            }
        }
        else if( v != fa)
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
}
void lca(int a,int b)
{
    while(deep[a] > deep[b])//如果深度a,比b的深度深,则a向上返回,直到与b同级
    {
        if(flag[a])
        {
            flag[a] = 0;
            ans--;
        }
        a = pre[a];
    }
    while(deep[a] < deep[b])//b同a
    {
        if(flag[b])
        {
            flag[b] = 0;
            ans--;
        }
        b = pre[b];
    }
    while(a != b)//如果a,b同级则找他们的公共祖先,找到公共祖先的过程中,去掉所有割边
    {
        if(flag[a])
        {
            flag[a]=0;
            ans--;

        }
        a= pre[a];
        if(flag[b])
        {
            flag[b] = 0;
            ans--;
        }
        b=pre[b];
    }
}

int main()
{
    int m,n,u,v;
    int cnt = 1;
    while(~scanf("%d%d",&n,&m))
    {
        if(!n && !m) break;
        memset(head, -1, sizeof(head));
        tol = 0;
        for( int i = 0; i < m; i++)
        {
            scanf("%d%d",&u,&v);
            add(u, v);
            add(v, u);
        }
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(deep,0, sizeof(deep));
        memset(flag,0,sizeof(flag));
        for( int i = 1; i <= n; i++)
            pre[i] = i;
        ans = 0;
        time = 0;
        tarjan(1,0);
        //for( int i = 1; i <= n; i++)
         //   printf("%d  :  %d\n",i,pre[i]);
        int q;
        scanf("%d",&q);
        printf("Case %d:\n",cnt++);
        while(q--)
        {
            scanf("%d%d",&u,&v);
            if(ans != 0)
            lca(u,v);
            printf("%d\n",ans);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值