Redundant Paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17387 | Accepted: 7233 |
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.
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.
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: 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.
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
题目大意:
一个联通的无向图,问最少添加几条边,能满足去掉任意一条边,整个图仍联通。
解析:
其实就是问最少加几条边能是使得整个图成为边-双连通图。
具体的解题步骤就是先求出原图中的边-双连通分量,然后对其进行缩点,于是原图就变成了一个新的无向图。这里有个结论:对于一棵无向树,我们要使得其变成边-双连通图,需要添加的边数 == (树中度数为1的点的个数+1)/2,所以就相当于求一下叶子结点的数目即为答案。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <ctime>
#include <queue>
using namespace std;
const int Max=100100;
int n,m,ans,size=1,Index,sum;
int num[Max],low[Max];
int first[Max],to[Max];
int way[Max],father[Max];
int map[5005][5005];
struct shu{int to,next;};
shu bian[Max];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void build(int x,int y)
{
size++;
bian[size].next=first[x];
first[x]=size;
bian[size].to=y;
}
inline void tarjan(int point,int fa)
{
low[point]=num[point]=++Index;
for(int u=first[point];u;u=bian[u].next)
{
if(!num[bian[u].to])
{
tarjan(bian[u].to,u);
low[point]=min(low[point],low[bian[u].to]);
if(low[bian[u].to]>num[point]) way[u]=way[u^1]=1;
}
else if(fa!=(u^1)) low[point]=min(low[point],num[bian[u].to]);
}
}
inline void dfs(int point) //缩点
{
father[point]=sum;
for(int u=first[point];u;u=bian[u].next)
{
if(way[u]||father[bian[u].to]) continue;
dfs(bian[u].to);
}
}
int main()
{
//freopen("lx.in","r",stdin);
//freopen("lx.out","w",stdout);
n=get_int();
m=get_int();
for(int i=1;i<=m;i++)
{
int x=get_int(),y=get_int();
if(x==y) continue;
build(x,y);
build(y,x);
}
tarjan(1,0);
for(int i=1;i<=n;i++) if(!father[i]) {sum++;dfs(i);}
for(int i=1;i<=n;i++)
for(int u=first[i];u;u=bian[u].next)
if(father[i]!=father[bian[u].to]) to[father[bian[u].to]]++;
for(int i=1;i<=sum;i++) if(to[i]==1) ans++; //求度数为1的叶子节点个数
cout<<(ans+1)/2<<"\n";
return 0;
}