poj 3177 Redundant Paths

题目链接

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9438 Accepted: 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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值