题干:

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, vand w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

Sample Output

Output for Sample Input 1
2

Output for Sample Input 2
0

题目大意:

某个企业想把一个热带天堂岛变成旅游胜地,岛上有N个旅游景点,任意2个旅游景点之间有路径连通(注意不一定是直接连通)。而为了给游客提供更方便的服务,该企业要求道路部门在某些道路增加一些设施。

道路部门每次只会选择一条道路施工,在该条道路施工完毕前,其他道路依然可以通行。然而有道路部门正在施工的道路,在施工完毕前是禁止游客通行的。这就导致了在施工期间游客可能无法到达一些景点。

为了在施工期间所有旅游景点依然能够正常对游客开放,该企业决定搭建一些临时桥梁,使得不管道路部门选在哪条路进行施工,游客都能够到达所有旅游景点。给出当下允许通行的R条道路,问该企业至少再搭建几条临时桥梁,才能使得游客无视道路部门的存在到达所有旅游景点?

一句话题意:给一个无向连通图,至少添加几条边使得去掉图中任意一条边不改变图的连通性(即使得它变为边双连通图)。

解题报告:(链接

首先建立模型:

       给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。

       模型很简单,正在施工的道路我们可以认为那条边被删除了。那么一个图G能够在删除任意一条边后,仍然是连通的,当且仅当图G至少为双连通的。

       PS:不要问我为什么不是3-连通、4-连通...人家题目问“至少添加几条边”好不...

       显然,当图G存在桥(割边)的时候,它必定不是双连通的。桥的两个端点必定分别属于图G的两个【边双连通分量】(注意不是点双连通分量),一旦删除了桥,这两个【边双连通分量】必定断开,图G就不连通了。但是如果在两个【边双连通分量】之间再添加一条边,桥就不再是桥了,这两个【边双连通分量】之间也就是双连通了。

       那么如果图G有多个【边双连通分量】呢?至少应该添加多少条边,才能使得任意两个【边双连通分量】之间都是双连通(也就是图G是双连通的)?

       这个问题就是本题的问题。要解决这个问题:

1、  首先要找出图G的所有【边双连通分量】。

Tarjan算法用来寻找图G的所有【边双连通分量】是最简单有效的方法,因为Tarjan算法在DFS过程中会对图G所有的结点都生成一个Low值,而由于题目已表明任意两个结点之间不会出现重边,因此Low值相同的两个结点必定在同一个【边双连通分量】中!  (如果是有重边的话,那么不同的low值是可能是属于同一个边双连通分量的,这个时候就要通过其他方法去求解边双连通分量。不过这不是本题要讨论的)

2、  把每一个【边双连通分量】都看做一个点(即【缩点】)

也有人称【缩点】为【块】,都是一样的。其实缩点不是真的缩点,只要利用Low值对图G的点分类处理,就已经缩点了。

以样例1为例,样例1得到的图G为上左图,

其中Low[4]=Low[9]=Low[10]

       Low[3]=Low[7]=Low[8]

       Low[2]=Low[5]=Low[6]

       Low[1]独自为政....

把Low值相同的点划分为一类,每一类就是一个【边双连通分量】,也就是【缩点】了,不难发现,连接【缩点】之间的边,都是图G的桥,那么我们就得到了上右图以缩点为结点,已桥为树边所构造成的树。

3、  问题再次被转化为“至少在缩点树上增加多少条树边,使得这棵树变为一个双连通图”。

首先知道一条等式:

若要使得任意一棵树,在增加若干条边后,变成一个双连通图,那么

至少增加的边数 =( 这棵树总度数为1的结点数 + 1 )/ 2

具体做法如下:

统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

那么我们只需要 求缩点树中中度数为1的结点数(即叶子数)有多少就可以了。

4、  求出所有缩点的度数的方法

两两枚举图G的直接连通的点,只要这两个点不在同一个【缩点】中,那么它们各自所在的【缩点】的度数都+1。注意由于图G时无向图,这样做会使得所有【缩点】的度数都是真实度数的2倍,必须除2后再判断叶子。


 但是其实这个题解包括下面这个代码都是错误的,因为不能通过看low值是否相等来判断是否在一个bcc中。具体代码应该看POJ - 3177,里面解释的比较详细了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int n,m;
int dfn[MAX],low[MAX],out[MAX];
int stk[MAX],index,clk,scc;
void init() {
	for(int i = 1; i<=n; i++) {
		vv[i].clear();
		dfn[i]=low[i]=out[i]=0;
	}
	clk = index = scc = 0;
}
void tarjan(int x,int rt) {
	dfn[x] = low[x] = ++clk;
	int up = vv[x].size();
	for(int i = 0; i<up; i++) {
		int v = vv[x][i];
		if(v == rt) continue;
		if(dfn[v] == 0) {
			tarjan(v,x);
			low[x] = min(low[x],low[v]);
		}
		else low[x] = min(low[x],dfn[v]);
	}
}
int main()
{
	while(~scanf("%d",&n)) {
		scanf("%d",&m);
		init();
		for(int u,v,i = 1; i<=m; i++) {
			scanf("%d%d",&u,&v);
			vv[u].pb(v);vv[v].pb(u);
		}
		for(int i = 1; i<=n; i++) {
			if(dfn[i] == 0) tarjan(i,-1);//因为题目保证是连通图所以其实可以直接tarjan(1,-1)的 
		}
		for(int up,u = 1; u<=n; u++) {
			up = vv[u].size();
			for(int v,j = 0; j<up; j++) {
				v = vv[u][j];//图G中Low值相同的两个点必定在同一个边双连通分量(即同一个缩点)中
				if(low[u] == low[v]) continue;//检查i、j是否不在同一个缩点中
				out[low[u]]++;out[low[v]]++;//注意是对缩点操作,不是对原图的点
			}
		}
		int ans = 0;//记录总度数=1(叶子)的缩点
		for(int i = 1; i<=n; i++) {
			if(out[i] == 2) ans++;//由于是无向图,因此每个缩点的度都重复计算了2次,因此度数==2才是叶子结点
		}
		printf("%d\n",(ans+1)/2);//将一棵树连成一个边双连通分量至少需要添加的边数=(叶子节点数+1)/2
	}
	return 0 ;
}

 

这里注意理解一下LOW的意思,因为讨论的都是无向图,所以下面我们说的含义也都是在无向图的基础上讨论的,定义上说,LOW[cur]代表:cur点可以向上回溯到的最早的一个点的DFN值。但是我想说,严格来讲,应该代表从另一条路径上可以向上回溯到的最早的一个点的DFN值,这里另一条路径就是代表不是dfs来的这条路径,而是其他路径。