POJ 3177 Redundant Paths

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.


【题目分析】
显然是一道求双连通分量的题目。因为他要求两两之间可以有两条路相互到达。所以求出双连通分量后,缩点成一棵树,一次最多可以把两个度为1的点连起来,如果有剩余,就连到根上,这样就可以了。注意会有重边,这时候需要记录从父节点走的边,不能逆向回去(^1),如果有重边的话,就不可以原路返回,只能从另外一条上去了


【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#define M(a) memset(a,-1,sizeof a)
int h[20001],ne[20001],fr[20001],to[20001],en=0;
int bel[10001],low[10001],dfn[10001],idx=0,dcnt=0;
int sta[10001],tp=0,dg[10001];
inline int min(int a,int b)
{return a>b?b:a;}
inline void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++idx;
    sta[++tp]=u;
    for (int i=h[u];i>=0;i=ne[i])
    if ((fa<0)||(i!=(fa^1))){
        int v=to[i];
        if (!dfn[v]){
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
        }
        else low[u]=min(low[u],dfn[v]);
    }
    if (dfn[u]==low[u])
    {
        int v;
        dcnt++;
        do{
            v=sta[tp--];
            bel[v]=dcnt;
        }while (u!=v);
    }
}
inline void add(int a,int b)
{fr[en]=a;to[en]=b;ne[en]=h[a];h[a]=en++;}
int main()
{
    M(h);M(ne);M(fr);M(to);
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i)
    {int a,b;scanf("%d%d",&a,&b),add(a,b),add(b,a);}
    tarjan(1,-1);
    for (int i=0;i<m*2;i+=2)if (bel[fr[i]]!=bel[to[i]])
        dg[bel[fr[i]]]++,dg[bel[to[i]]]++;
    int sum=0;
    for (int i=1;i<=n;++i)
      if (dg[i]==1) sum++;
    printf("%d\n",(sum+1)/2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值