题目链接 :http://poj.org/problem?id=3177
Description In order to get from one of the F ( <= F <= ,) grazing fields (which are numbered ..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- <= R <= ,) 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 : Two space-separated integers: F and R Lines ..R+: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output Line : A single integer that is the number of new paths that must be built.
Sample Input Sample Output Hint Explanation of the sample: One visualization of the paths is: +---+---+
| |
| |
+---+---+
/
/
/
+
Building new paths from to and from to satisfies the conditions. +---+---+
: | |
: | |
+---+---+
/ :
/ :
/ :
+ - - - -
Check some of the routes:
– : –> and –> –> –>
– : –> –> –> and –> –> –>
– : –> –> and –> –> –>
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.
题意大意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。
现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。
分析:利用tarjan算法进行缩点,算出每个缩点的出入度,所要增加的边为 添加边数=(树中度为1的节点数+1)/2;
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stack>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define mod 2147493647
#define N 30100
struct node
{
int id,to,next;
}s[N];
int a[N],deg[N];
int low[N],dfn[N],st[N];
int belong[N],vis[N];
int t,k,l,num;
void init()
{
met(vis,);
met(dfn,);
met(a,-);
met(deg,);
t=k=l=num=;
}
void add(int u,int v,int f)
{
s[l].id=f;
s[l].to=v;
s[l].next=a[u];
a[u]=l++;
}
void tarjan(int u,int f)
{
low[u]=dfn[u]=++t;
st[k++]=u;
vis[u]=;
for(int i=a[u];i!=-;i=s[i].next)
{
int v=s[i].to;
if(f==s[i].id)
continue;
if(!dfn[v])
{
tarjan(v,s[i].id);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
{
low[u]=min(low[u],dfn[v]);
}
}
int v;
if(dfn[u]==low[u])
{
++num;
do{
v=st[--k];
vis[v]=;
belong[v]=num;///每个点属于的圈
}while(v!=u);
}
}
void solve(int n)
{
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-);
}
for(int u=;u<=n;u++)
{
for(int j=a[u];j!=-;j=s[j].next)
{
int v=s[j].to;
if(belong[v]==belong[u])
continue;
deg[belong[v]]++;///统计每个缩点里有几个点
//deg[belong[u]]++;
}
}
int ans=;
for(int i=;i<=num;i++)
{
if(deg[i]==)///添加边数=(树中度为1的节点数+1)/2
ans++;
}
printf("%d\n",(ans+)/);
}
int main()
{
int n,m,e,f;
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
for(int i=;i<=m;i++)
{
scanf("%d %d",&e,&f);
add(e,f,i);
add(f,e,i);
}
solve(n);
}
return ;
}