POJ 3177 Redundant Paths(边双联通分量)

题目描述:
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

翻译:

F个草场,R条道路。
每对草场之间已经有至少一条路径,给出所有R条双向道路,每条路连接了两个不同的草场,计算最少需要新建道路的数量使得每一对草场之间都会至少有两条相互分离的路径。

分析:

求任意两点间至少有两条没有公共边的路,也就是说所要求的图是一张边双联通图。

边双联通图求法:
求出所有的桥以后,把桥边删除,原图变成了多个连通块,则每个连通块就是一个边双联通分量。
桥不属于任何一个边双联通分量,其余的边和每个顶点都属于且只属于一个边双联通分量 。

一个有桥的连通图,如何把它通过加边变成双连通图?
首先求出所有的桥,然后删除这些桥边,剩下的每一个连通块都是一个双连通图。把每个双连通图子图收缩为一个顶点,再把桥边加回来,最后的这个图一定是一棵树,边连通度为1。

统计出树中度为1的结点的个数,即叶子节点的个数,记为leaf,则至少在树上添加(leaf+1)/2条边,就能使树达到边双联通。

1.求出所有的桥:

void tarjan(int u)
{
    dfn[u]=low[u]=++dfstime;
    for(int i=fir[u]; i; i=edge[i].nex)
    {
        if(!book[i])///不能沿着dfs过来的那条边回去
        {
            int v=edge[i].v;
            book[i]=book[i^1]=true;///边的标号是从2开始的 x=2,x^1=3 x=3,x^1=2
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else
                low[u]=min(low[u],dfn[v]);
            if(low[u]<low[v])///桥的满足条件
            {
                cut[i]=cut[i^1]=1;///双向边都进行标记
                bridge[++res].x=u,bridge[res].y=v;
            }
        }
    }
}
tarjan(1);

2.删除桥之后缩点

void dfs(int x)
{
    co[x]=col;
    for(int i=fir[x]; i; i=edge[i].nex)
    {
        if(!cut[i])//不是桥
        {
            int v=edge[i].v;
            if(co[v])
                continue;
            dfs(v);
        }
    }
}
    for(int i=1; i<=n; i++) ///删除桥之后,缩点
    {
        if(!co[i])
            ++col,dfs(i);
    }

3.把桥边加回来统计度数

   for(int i=1; i<=res; i++)
    {
        int x=bridge[i].x;
        int y=bridge[i].y;
        if(co[x]!=co[y])///桥的两个端点不属于同一个连通分量
            degree[co[x]]++,degree[co[y]]++;
    }

完整代码:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e4+10;
struct node
{
    int v,nex;
} edge[N>>1];
struct node1  ///记录桥两边的端点
{
    int x,y;
} bridge[N>>1];

int res;
int cut[N*2];///标记桥
int fir[N],tot;
int n,m;
int low[N],dfn[N],dfstime;
int degree[N];
bool book[N>>1];
int co[N],col;
void init()
{
    res=0;
    tot=1;
    col=0;
    memset(degree,0,sizeof(degree));
    memset(co,0,sizeof(co));
    memset(cut,0,sizeof(cut));
    memset(book,false,sizeof(book));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
}
void Ins(int x,int y)
{
    edge[++tot].v=y;
    edge[tot].nex=fir[x];
    fir[x]=tot;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++dfstime;
    for(int i=fir[u]; i; i=edge[i].nex)
    {
        if(!book[i])///不能沿着dfs过来的那条边回去
        {
            int v=edge[i].v;
            book[i]=book[i^1]=true;///边的标号是从2开始的 x=2,x^1=3 x=3,x^1=2
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else
                low[u]=min(low[u],dfn[v]);
            if(low[u]<low[v])///桥的满足条件
            {
                cut[i]=cut[i^1]=1;
                bridge[++res].x=u,bridge[res].y=v;
            }
        }
    }
}
void dfs(int x)
{
    co[x]=col;
    for(int i=fir[x]; i; i=edge[i].nex)
    {
        if(!cut[i])//不是桥
        {
            int v=edge[i].v;
            if(co[v])
                continue;
            dfs(v);
        }
    }
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        Ins(x,y);
        Ins(y,x);
    }
    tarjan(1);

    for(int i=1; i<=n; i++) ///删除桥之后,缩点
    {
        if(!co[i])
            ++col,dfs(i);
    }
    for(int i=1; i<=res; i++)
    {
        int x=bridge[i].x;
        int y=bridge[i].y;
        if(co[x]!=co[y])///桥的两个端点不属于同一个连通分量
            degree[co[x]]++,degree[co[y]]++;
    }
    int sum=0;
    for(int i=1; i<=col; i++)
        if(degree[i]==1)
            sum++;
    printf("%d\n",(sum+1)>>1);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值