POJ 3177 Redundant Paths 有重边的图求桥

本文介绍了一种算法,用于确定在给定图中至少需要添加多少条边才能使其成为边双连通图。通过使用Tarjan算法找到双连通分量,并基于此计算所需的最小边数。

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

点击打开链接

 

 

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7951 Accepted: 3456

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

给定一张图,求最少加入几条边可以使得整个图成为一个边双连通图。很明显这里不是要我们求出这里面的割点或者割边。
其他的就比较简单了,按照Low和Dfn的定义,如果某个点拓展完后Low=Dfn,那么栈中从栈顶到这个店为止的部分都属于同一个双联通分量。这个由Dfs树的性质决定的。这样求出来之后,只需要对这些点进行染色,不需要真的缩点,只要再去重新枚举原图中的边,向新的颜色不同的顶点之间加边即可。又由于双联通分量的定义,这样新的图一定是一个树,如果不是一个树,那有环的部分按照定义是应该在同一个双联通分量中的。然后求求度数,计算输出即可。
添加边数=(树中度为1的节点数+1)/2。
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define min(a,b) (a<b?a:b)
#define M 5005
using namespace std;
int low[M],dfn[M],head[M];
bool g[M][M];
int n,num,cnt;
struct E
{
    int v,next;
} edg[M*2];



void addedge(int a,int b)
{
    edg[cnt].v=b;
    edg[cnt].next=head[a];
    head[a]=cnt++;
}

void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++num;
    for(int i=head[u]; i!=-1; i=edg[i].next)
    {
        int v=edg[i].v;
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(v!=fa)
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
}

void solve()
{
    int count=0;
    int cut[M];
    memset(cut,0,sizeof(cut));
    for(int i=1; i<=n; i++)
    {
        for(int j=head[i]; j!=-1; j=edg[j].next)
        {
            int v=edg[j].v;
            if(low[v]!=low[i])
                cut[low[i]]++;
        }
    }
    for(int i=0; i<=n; i++)
    {
        if(cut[i]==1)
            count++;
    }
    printf("%d\n",(count+1)/2);
}

int main()
{
    int m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dfn,0,sizeof(dfn));
        memset(g,0,sizeof(g));
        memset(head,-1,sizeof(head));
        num=cnt=0;
        int a,b;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            if(!g[a][b])
            {
                addedge(a,b);
                addedge(b,a);
                g[a][b]=g[b][a]=1;
            }
        }
        tarjan(1,1);
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值