poj3177Redundant Paths

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17763 Accepted: 7380

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

题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。
思路:其实在树中至少添加多少条边能使图变为双连通图。先进行缩点,建立有向无环图,然后算入度为1的个数,要新建的边等于(入度为1的个数+1)/2。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int N=5000+50;
const int M=10000+50;
int dfn[N],head[N],low[N],belong[N];
bool instack[N];
int in[N],out[N];
struct edge
{
    int v,next;
}e[M*2];
int n,m;
int cnt,num,index;
stack<int>s;
void addedge(int u,int v)
{
    e[num].v=v;
    e[num].next=head[u];
    head[u]=num++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(belong,0,sizeof(belong));
    memset(instack,false,sizeof(instack));
    while(!s.empty())
        s.pop();
    cnt=index=num=0;
}
void tarjan(int u,int pre)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    int v;
    instack[u]=true;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].v;
        if(i==(pre^1))
        {
            continue;
        }
        if(!dfn[v])
        {
            tarjan(v,i);
            low[u]=min(low[v],low[u]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        do{
            v=s.top();
            s.pop();
            belong[v]=cnt;
            instack[v]=false;
        }while(u!=v);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        int i,j;
        int a,b;
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            addedge(a,b);
            addedge(b,a);
        }
        for(i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(1,-1);
        }
        for(i=1;i<=n;i++)
        {
            for(j=head[i];j!=-1;j=e[j].next)
            {
                int v=e[j].v;
                if(belong[i]!=belong[v])
                {
                    in[belong[v]]++;
                }
            }
        }
        int ans=0;
        for(i=1;i<=cnt;i++)
        {
            if(in[i]==1)ans++;
        }
        printf("%d\n",(ans+1)/2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值