Description
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.
Source
题意大意:一群牛将被在一个特定路径构成的农场上迁移,每两块农场之间都至少有一条通道,这些牛要求每两块路径至少要有两条通道,求最少需要修建多少条路才能满足要求。
这题的解法与http://www.cppblog.com/mythit/archive/2009/05/29/86082.html完全一样,只是题目中说了图中有可能存在平行边,这里必须判断一下。我还是很偷懒的用了STL里的vector模拟邻接矩阵,并且开了个5001*5001的bool数组判断平行边。结果导致代码的效率和空间消耗都很大,110MS和将近24M的内存空间。如果自己建图的话,效率能提高很多。
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 5001;
vector< vector<int> > adj;
bool hash[MAXN][MAXN];
int cnt,low[MAXN],pre[MAXN],visit[MAXN],degree[MAXN];
void dfs(int u,int v){
visit[u]=1;
pre[u]=cnt++,low[u]=pre[u];
int i,len=adj[u].size();
for(i=0;i<len;i++){
if(adj[u][i]==v) continue;
if(!visit[adj[u][i]]) dfs(adj[u][i],u);
if(low[adj[u][i]]<low[u]) low[u]=low[adj[u][i]];
}
visit[u]=2;
}
int main(){
int i,j,u,v,n,m,len,ans;
while(scanf("%d %d",&n,&m)!=EOF){
adj.assign(n+1,vector<int>());
memset(hash,false,sizeof(hash));
while(m--){
scanf("%d %d",&u,&v);
if(!hash[u][v]){
hash[u][v]=true;
adj[u].push_back(v),adj[v].push_back(u);
}
}
memset(visit,0,sizeof(visit));
cnt=0,dfs(1,1);
memset(degree,0,sizeof(degree));
for(i=1;i<=n;i++){
len=adj[i].size();
for(j=0;j<len;j++)
if(low[i]!=low[adj[i][j]])
degree[low[i]]++;
}
for(ans=i=0;i<=n;i++)
if(degree[i]==1) ans++;
printf("%d\n",(ans+1)/2);
}
return 0;
}