Redundant Paths
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19606 | Accepted: 8105 |
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://poj.org/problem?id=3177
题意:n个牧场,从一个牧场到达另一个牧场,至少有两条路可走,现有m条路,问至少建多少条路?
两条路中不能有重复的边
边连通分量:去掉任意一条边,这个图中任意两点可相互到达。
将图中是边连通分量的子图缩点,(新图中度为1的点+1)/2 就是答案。
因为最优的连法就是取一个度不为1的点作为根结点,优先考虑以根节点为最近公共祖先的两点连一条线是最优的。
#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
const int N=5000+10;
struct edge
{
int from,to,next;
}e[N*N];
int head[N],in[N];
int dfn[N],low[N],cnt,bcc[N],index,vis[N*N],bccnum,ff[N*N];
stack<int>que;
void add_edge(int u,int v)
{
e[cnt].from=u;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int root)
{
dfn[root]=low[root]=++index;//更新时间戳
que.push(root);//入栈
for(int i=head[root];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(vis[i]) continue;
vis[i]=vis[i^1]=1;/*无向图前向星两点之间是两条边,
所以要都标记已走过
这里边从0开始,0,1是代表着同一条边,2,3代表着同一条边
0^1==1,1^1=0; 2^1=3,3^1=2
*/
if(!dfn[v])
{
tarjan(v);
low[root]=min(low[root],low[v]);
}
else low[root]=min(low[root],dfn[v]);//无向图无须判断bcc
/*if(dfn[root]<low[v]
{
//该边是桥
e[i].桥=1;
}*/
}
if(low[root]==dfn[root])//发现一个bcc
{
bccnum++;
int x;
do
{
x=que.top();
que.pop();
bcc[x]=bccnum;//相当于缩点
}while(x!=root);
}
}
int main()
{
memset(head,-1,sizeof(head));
int n,m;
cin>>n>>m;
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=e[j].next)
{
if(ff[j]) continue;
ff[j]=ff[j^1]=1;
int v=e[j].to;
if(bcc[i]!=bcc[v])
{
in[bcc[i]]++;
in[bcc[v]]++;
}
}
}
int ans=0;
for(int i=1;i<=bccnum;i++)
{
if(in[i]==1)
ans++;
}
printf("%d\n",(ans+1)/2);
return 0;
}