HDU2460(Network)

Network

Problem 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

思路

给定一张无向图,求出当前图中所有的桥(割边)。然后有Q次查询,每次新加入一个对点,问加入该对点之后桥的数目是多少。

自己的思路:开始想法挺好又是缩点又是统计度数的。大概就是先跑一遍tarjan统计桥的数目,然后缩点最后统计缩点的度。开始Q次查询操作,每次输入两个点,因为缩完点了桥肯定是缩点图中的边。然后对输入的两个点进行判断是不是属于同一个连通分量,如果在同一个连通分量或者当前图中桥数量为0,这条边加不加入都无所谓,如果不属于同一个连通分量就加入边重新跑tarjan计算。我算了一下复杂度大概差也差不多。最坏情况下勉勉强强,但是一直WA没找到错误。

Tarjan + LCA
题解思路:跑一次Tarjan,统计桥边数目。然后对接下来的Q次操作做LCA,想想觉得好聪明,但是我想不到网上的人一个比一个聪明。为什么做LCA?

  1. Tarjan跑完之后是一颗生成树,树中每个节点都有一个访问时间序以及一个父节点,桥边做标记。
  2. 可以想象加入对两个叶节点x,y建立一条无向边,那么这两个叶节点到他们最近的公共祖先 f 中所有点就成环了,消除了f - x 以及 f - y这些路上所有的桥边。而且对于任意加入的两个节点都可以得到想要的结果。
  3. 由于两个节点的时间序(深度)可能不同,先让两个节点统一到一个深度在让他们同时上跳,一路跳一路把桥边标记取消,桥边数目一直减1。当他们跳到同一个节点(最近公共祖先)结束。
  4. 桥边虽然是u – v。但是为了方便处理以及查询用cut[v]标记桥,用u标记不好处理。

最后注意一下细节就可以了~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#pragma comment(linker,"/STACK:10240000,10240000")
using namespace std;
const int maxn = 1e5+5;
struct edge{
	int to;
	int next;
}e[maxn*4];
int head[maxn];
int dfn[maxn];
bool cut[maxn];
int low[maxn];
int f[maxn];
int tot,cnt,ans;
void clear_set()
{
	cnt = ans = tot = 0;
    memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
    memset(cut,false,sizeof(cut));
	memset(low,0,sizeof(low));
    for(int i = 0;i < maxn;i++){
        f[i] = i;
    }
}
void addedge(int x,int y)
{
	e[tot].to = y;
	e[tot].next = head[x];
	head[x] = tot++;
}
void tarjan(int x,int fx)
{
	dfn[x] = low[x] = ++cnt;
	int k = 0;
	for(int i = head[x];~i;i = e[i].next){
		int y = e[i].to;
		if(fx == y && !k){          //重边,跳过一次
			k++;
			continue;
		}
		if(!dfn[y]){
            f[y] = x;				//记录父节点
			tarjan(y,x);
			low[x] = min(low[x],low[y]);
			if(low[y] > dfn[x]){
                cut[y] = true;
				ans++;
			}
		}
		else if(dfn[y] < dfn[x]){
			low[x] = min(low[x],dfn[y]);
		}
	}
}
void LCA(int x,int y)
{
	//树根为深度1,保持x的深度 大于 y的深度。
    if(dfn[x] < dfn[y])     swap(x,y);
    while(dfn[x] > dfn[y]){			//跳到同一个深度
        if(cut[x]){
            cut[x] = false;
            ans--;
        }
        x = f[x];
    }
    while(x != y){					//x和y齐头并进
        if(cut[x]){ cut[x] = false;ans--;}
        if(cut[y]){ cut[y] = false;ans--;}
        x = f[x];y = f[y];
    }
}
int main()
{
	int n,m,k = 1,q;
	while(~scanf("%d%d",&n,&m)){
		if(n == 0 && m == 0)	break;
		clear_set();
		int x,y;
		for(int i = 0;i < m;i++){
			scanf("%d%d",&x,&y);
			addedge(x,y);	addedge(y,x);
		}
        tarjan(1,-1);
		printf("Case %d:\n",k++);
		scanf("%d",&q);
		while(q--){
			scanf("%d%d",&x,&y);
			LCA(x,y);
			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、付费专栏及课程。

余额充值