POJ-3352-无向图的割顶和桥-求边-双连通分量

Road Construction

Description

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

Sample Output

Output for Sample Input 1
2

Output for Sample Input 2
0

Source

题意:求至少 要加多少条边,使得整个图变为边-双连通分量

连通分量:互相可达的结点称为一个连通分量

割顶:对于无向图G,如果删除某个点u后,连通分量数目增加,则称u为图的关节点或割顶

点-双连通:对于一个连通图,如果任意两点至少存在两条“点不重复”的路径,则说这个图是点-双连通的(简称双连通),这个要求等价于任意两条边都在同一个简单环中,即内部无割顶。

边-双连通:如果任意两点至少存在两条“边不重复”的路径,我们说这个图是边-双连通的

思路:将各个双连通分量看做缩点,最后将叶子的数目加一除以2就是答案(至于为什么不知道难过,当时看了别人的代码)


#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#define MAX 1005
using namespace std;
vector<int> G[MAX];
int pre[MAX];   //时间戳
int low[MAX];   // 结点u及其后代所能连回的最早的祖先的pre值
int iscut[MAX];  //记录结点是否为割点
int dfs_clock;
int dfs(int u,int fa)   //u在dfs树中的父结点是fa
{
    int lowu=pre[u]=++dfs_clock;
    int child=0;        //子结点数目
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(!pre[v])     //没有访问过v
        {
            child++;
            int lowv=dfs(v,u);      //lowv指v及其后代能连回的最早的祖先的pre值
            lowu=min(lowu,lowv);     //用后代的low函数更新u的low函数
            if(lowv>=pre[u])      //说明u是一个割顶
            {
                iscut[u]=1;
            }
        }
        else if(pre[v]<pre[u]&&v!=fa)       //说明不是树边
        {
            lowu=min(lowu,pre[v]);          //用反向边更新u的low函数
        }
    }
    if(fa<0&&child==1)        //判断树根是不是割顶
        iscut[u]=0;
    low[u]=lowu;
    return lowu;
}
void start(int n)
{
    int bcc_cnt=0;
    dfs_clock=0;
    memset(pre,0,sizeof(pre));
    memset(low,0,sizeof(low));
    memset(iscut,0,sizeof(iscut));
}
int main()
{
    int n,r,i,a,b,ans,j,deg[MAX];
    while(~scanf("%d%d",&n,&r))
    {
        for(i=1;i<=n;i++)      //注意要清空
            G[i].clear();
        ans=0;
        memset(deg,0,sizeof(deg));
        for(i=1; i<=r; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        start(n);
        dfs(1,-1);      //初始化树根的fa为-1
        for(i=1; i<=n; i++)
        {
            for(j=0; j<G[i].size(); j++)
            {
                if(low[i]!=low[G[i][j]])
                    deg[low[i]]++;       //记录缩点的出度
            }
        }
        for(i=1; i<=n; i++)
            if(deg[i]==1)       //出度为一说明是叶子结点
                ans++;
        printf("%d\n",(ans+1)/2);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值