题干:

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R 

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample: 

One visualization of the paths is: 

   1   2   3
   +---+---+  
       |   |
       |   |
 6 +---+---+ 4
      / 5
     / 
    / 
 7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 

   1   2   3
   +---+---+  
   :   |   |
   :   |   |
 6 +---+---+ 4
      / 5  :
     /     :
    /      :
 7 + - - - - 

Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes. 

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

题目大意:

有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。

解题报告:

在POJ - 3352的基础上,加个去重边可以过。

但是对于这组样例就过不了

2 2
1 2
2 1

按照题意答案应该是0,但是AC代码1的答案输出是1.

所以再次证明了不能只看low数组是否相同来判断是否属于一个边双连通分量

AC代码1:(但是其实是错误代码只不过是数据若)

#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;
bool mm[5555][5555];
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;
		for(int j = 1; j<=n; j++) mm[i][j]=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);
			if(mm[u][v]==1) continue;
			mm[u][v]=mm[v][u]=1;
			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] == 1) ans++;//由于是无向图,因此每个缩点的度都重复计算了2次,因此度数==2才是叶子结点
		}
		printf("%d\n",(ans+1)/2);//将一棵树连成一个边双连通分量至少需要添加的边数=(叶子节点数+1)/2
	}
	return 0 ;
}

AC代码2:(注意对于重边的处理,这里采用kuangbin的写法,当然如果你是邻接表的写法也可以用下面附的那部分代码)

#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],col[MAX];
int stk[MAX],index,clk,bcc;
void init() {
	for(int i = 1; i<=n; i++) {
		vv[i].clear();
		dfn[i]=low[i]=out[i]=0;
	}
	clk = index = bcc = 0;
}
void tarjan(int x,int rt) {
	dfn[x] = low[x] = ++clk;
	stk[++index] = x;
	int up = vv[x].size(),cntrt=0;
	for(int i = 0; i<up; i++) {
		int v = vv[x][i];
		if(v == rt && cntrt == 0) {
			cntrt++;continue;
		}
		if(dfn[v] == 0) {
			tarjan(v,x);
			low[x] = min(low[x],low[v]);
		}
		else low[x] = min(low[x],dfn[v]);
	}
	if(dfn[x] == low[x]) {
		bcc++;
		while(1) {
			int tmp = stk[index];index--;
			col[tmp] = bcc;
			if(tmp == x) break;
		}
	}
}
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(col[u] == col[v]) continue;//检查i、j是否不在同一个缩点中
				out[col[u]]++;//out[low[v]]++;//注意是对缩点操作,不是对原图的点
			}
		}
		int ans = 0;//记录总度数=1(叶子)的缩点
		for(int i = 1; i<=n; i++) {
			if(out[i] == 1) ans++;//由于是无向图,因此每个缩点的度都重复计算了2次,因此度数==2才是叶子结点
		}
		printf("%d\n",(ans+1)/2);//将一棵树连成一个边双连通分量至少需要添加的边数=(叶子节点数+1)/2
	}
	return 0 ;
}

附代码: 

void tarjan(int x,int rti) {
	dfn[x] = low[x] = ++clk;
	stk[++index] = x;
	for(int i = head[x]; ~i; i=e[i].ne) {
		int v = e[i].to;
		if(i == rti^1) continue;
		if(dfn[v] == 0) {
			tarjan(v,i);
			low[x] = min(low[x],low[v]);
		}
		else low[x] = min(low[x],dfn[v]);
	}
	if(dfn[x] == low[x]) {
		bcc++;
		while(1) {
			int tmp = stk[index];index--;
			col[tmp] = bcc;
			if(tmp == x) break;
		}
	}
}