POJ3177-Redundant Paths

本文介绍了一种解决所有顶点间拥有两条不共边路径的问题算法。通过将相同双连通分量内的点进行收缩,并确保任意两点间至少存在两条独立路径的方法,文章详细解析了如何最小化新建路径的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14268 Accepted: 6065

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

Hint

Explanation of the sample: 

One visualization of the paths is: 
   1   2   3
   +---+---+  
       |   |
       |   |
 6 +---+---+ 4
      / 5
     / 
    / 
 7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
   1   2   3
   +---+---+  
   :   |   |
   :   |   |
 6 +---+---+ 4
      / 5  :
     /     :
    /      :
 7 + - - - - 
Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
 
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.

Source


题意:使所有的顶点间都有两条路可达, 路径可共顶点但不可共边
解题思路:将同一个边双连通分量的所有点收缩成一个点. 新构成的顶点形成一棵树。此时的任意两个顶点间都需要有至少两条相互"独立边", 则可以让成对的叶子节点连边。总方案数 = (叶子节点数量+1)/2,    单个叶子节点也要连到其它节点去


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

const int N=1010;

struct Node
{
    int v,nt;
}edge[N*100];
int s[N],cnt,dep;
int n,m;
int f[N];
int belong[N],dfn[N],low[N];
bool vis[N];
int bridge[N*10][2],nbridge,x[N];

int Find(int x)
{
    return x==f[x]?x:(f[x]=Find(f[x]));
}

void Union(int a,int b)
{
    int x=Find(a),y=Find(b);
    if(x!=y) f[x]=y;
}

void AddEdge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].nt=s[u];
    s[u]=cnt++;
}

void tarjan(int u,int pre)
{
    vis[u]=true;
    dfn[u]=low[u]=dep++;
    for(int i=s[u];~i;i=edge[i].nt)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        if(!vis[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]>=low[v]) Union(u,v);
            if(dfn[u]<low[v])
            {
                bridge[nbridge][0]=u;
                bridge[nbridge++][1]=v;
            }
        }
        else low[u]=min(low[u],dfn[v]);
    }
}

int GetConnection()
{
    for(int i=0;i<=n;i++) f[i]=i;
    nbridge=0,dep=1;
    memset(vis,0,sizeof vis);
    memset(belong,-1,sizeof belong);
    tarjan(1,0);
    int res=0;
    for(int i=1;i<=n;i++)
    {
        int k=Find(i);
        if(belong[k]==-1) belong[k]=res++;
        belong[i]=belong[k];
    }
    return res;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(s,-1,sizeof s);
        cnt=0;
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            AddEdge(u,v),AddEdge(v,u);
        }
        int res=GetConnection();
        memset(x,0,sizeof x);
        for(int i=0;i<nbridge;i++)
        {
            int u=bridge[i][0],v=bridge[i][1];
            x[belong[u]]++,x[belong[v]]++;
        }
        int sum=0;
        for(int i=0;i<res;i++)
            if(x[i]==1) sum++;
        printf("%d\n",(sum+1)/2);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值