您的位置:首页 > 其它

poj 3177 Redundant Paths

2014-11-30 19:40 411 查看
题目链接

Redundant Paths

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9438Accepted: 4043
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


题解:求最少添加多少条边让整个无向联通图不存在桥?

题解:将边双联通分量缩点,形成的图一定树,设叶子节点的个数为x,那么最少添加(x+1)/2条边,就能让整个图没有桥。

代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<string>
#include<string.h>
#include<map>
#define nn 51000
#define inff 0x3fffffff
typedef long long LL;
using namespace std;
int n,m;
struct node
{
int st,en,next;
}E[nn*10];
int p[nn],num;
void init()
{
memset(p,-1,sizeof(p));
num=0;
}
void add(int st,int en)
{
E[num].st=st;
E[num].en=en;
E[num].next=p[st];
p[st]=num++;

E[num].st=en;
E[num].en=st;
E[num].next=p[en];
p[en]=num++;
}
int dfn[nn],low[nn];
int df;
bool qiao[nn*10];
vector<int>ve;
int fa[nn];
void dfs(int id,int pre)
{
dfn[id]=low[id]=++df;
int i,w;
for(i=p[id];i+1;i=E[i].next)
{
w=E[i].en;
if((i^1)==pre)
continue;
if(dfn[w]==-1)
{
dfs(w,i);
low[id]=min(low[id],low[w]);
if(low[w]>dfn[id])
{
qiao[i^1]=true;
qiao[i]=true;
ve.push_back(i);
}
}
else
low[id]=min(low[id],dfn[w]);
}
}
int cnt;
int du[nn];
void go(int id)
{
fa[id]=cnt;
int i,w;
for(i=p[id];i+1;i=E[i].next)
{
if(qiao[i])
continue;
w=E[i].en;
if(fa[w]==-1)
go(w);
}
}
void solve()
{
ve.clear();
memset(du,0,sizeof(du));
memset(dfn,-1,sizeof(dfn));
memset(fa,-1,sizeof(fa));
df=0;
int i;
dfs(1,-1);
cnt=0;
for(i=1;i<=n;i++)
{
if(fa[i]==-1)
{
++cnt;
go(i);
}
}
int ix;
for(i=0;i<(int)ve.size();i++)
{
ix=ve[i];
// if(fa[E[ix].st]!=fa[E[ix].en])
{
du[fa[E[ix].st]]++;
du[fa[E[ix].en]]++;
}
}
int ans=0;
for(i=1;i<=cnt;i++)
{
if(du[i]==1)
{
ans++;
}
}
printf("%d\n",(ans+1)/2);
}
int main()
{
int i,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
memset(qiao,false,sizeof(qiao));
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: