您的位置:首页 > 其它

POJ3177--Redundant Paths

2013-07-23 21:19 183 查看
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

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 30080
int first[maxn],nxt[maxn],vv[maxn],low[maxn],dfn[maxn],rudu[maxn];
int vis[maxn];
int e,cnt;
inline int min(int a,int b)
{
return a>b?b:a;
}
void addEdge(int u,int v)
{
vv[e] = v;	nxt[e] = first[u];	first[u] = e++;
vv[e] = u;	nxt[e] = first[v];		first[v] = e++;
}
void Tarjan(int u,int fa)
{
vis[u] = 1;
dfn[u] = low[u] = ++cnt;
for(int i = first[u];i != -1;i = nxt[i])
{
int v = vv[i];
if(vis[v] == 1 && v != fa)
{
low[u] = min(low[u],dfn[v]);
}
if(vis[v] == 0)
{
Tarjan(v,u);
low[u] = min(low[u],low[v]);
}
}
vis[u] = 2;
}
int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
e = cnt = 0;
memset(vis,0,sizeof(vis));
memset(first,-1,sizeof(first));
memset(dfn,0,sizeof(dfn));
memset(rudu,0,sizeof(rudu));
for(int i = 1;i <= m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
bool flag = true;
for(int j = first[u];j != -1;j = nxt[j])
{
if(vv[j] == v)
{
flag = false;
break;
}
}
if(flag)		addEdge(u,v);
}
Tarjan(1,-1);
for(int i = 1;i <= n;i++)
{
for(int j = first[i];j != -1;j = nxt[j])
{
int v = vv[j];
if(low[i] != low[v])
{
rudu[low[v]]++;rudu[low[i]]++;
}
}
}
for(int i = 1;i <= n;i++)	rudu[i]/=2;
int ans = 0;
for(int i = 1;i <= n;i++)
{
if(rudu[i] == 1)	ans++;
}
printf("%d\n",(ans+1)/2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ3177