Road Construction+求双联通分量、割点、桥+POJ

文章讨论了在热带海岛度假胜地远程岛上的道路建设问题,包括道路布局、维修升级及新道路建设,以确保在任意道路维修时仍能实现游客景点间的通行。通过求解双连通分量和构建树形结构,文章提供了最小新增道路数量的计算方法,确保道路网络的连通性不受施工影响。

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

Road Construction
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8652 Accepted: 4323

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
解决方案:可求双连通分量,把连通分量压缩成一个点,之后就成了一颗树,求出叶子节点的个数k,则要添加的边为(k+1)/2条,而桥就刚好是形成的树的边。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define MMAX 1003
using namespace std;
int n,r,index,root,k;
vector<int>Map[MMAX];
int low[MMAX],dfn[MMAX],d[MMAX];
bool jud[MMAX];
void init()
{
    index=0,k=0;
    for(int i=0; i<=n; i++)
    {
        Map[i].clear();
        low[i]=0;
        dfn[i]=0;
        jud[i]=false;
        d[i]=0;
    }
}
void tarjan(int u,int fa)
{
    low[u]=dfn[u]=++index;
    int len=Map[u].size();
    for(int i=0; i<len; i++)
    {
        int v=Map[u][i];
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(fa!=v)///防止搜回上一个节点
            low[u]=min(low[u],dfn[v]);
    }


}
int main()
{
    while(~scanf("%d%d",&n,&r))
    {
        init();
        for(int i=0; i<r; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            Map[a].push_back(b);
            Map[b].push_back(a);
        }
        tarjan(1,-1);
        for(int i=1; i<=n; i++)
        {
            int len=Map[i].size();
            for(int j=0; j<len; j++)
            {
                int v=Map[i][j];
                if(low[v]!=low[i])
                {
                    d[low[v]]++,d[low[i]]++;
                }
            }
        }

        for(int i=1; i<=n; i++)
        {
            if(d[i]==2) k++;
        }
        printf("%d\n",(k+1)/2);

    }
    return 0;
}
其实上面的方法也不太安全,在同一个边双连通分量中的low值未必是相等的,所以下面是比较谨慎的代码,先标记桥,然后进行一次dfs。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define MMAX 5005
using namespace std;
int F,R;
vector<int>Map[MMAX];
int dfn[MMAX],low[MMAX],index,belong[MMAX],degree[MMAX],cc;
bool m[MMAX][MMAX];
void init()
{
    index=1;
    memset(m,false,sizeof(m));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(degree,0,sizeof(degree));
    memset(belong,0,sizeof(belong));
    for(int i=0; i<=F; i++)
    {
        Map[i].clear();
    }
}
void tarjan(int u,int fa)
{

    dfn[u]=low[u]=index++;
    int len=Map[u].size();
    for(int i=0; i<len; i++)
    {
        int v=Map[u][i];
        if(v!=fa)
        {
            if(!dfn[v])
            {
                tarjan(v,u);
                low[u]=min(low[u],low[v]);
                if(low[v]>dfn[u])
                {
                    m[u][v]=m[v][u]=true;
                }
            }
            else
            {
                low[u]=min(low[u],dfn[v]);
            }
        }

    }

}
void dfs(int u)
{

    int len=Map[u].size();
    for(int i=0; i<len; i++)
    {
        int v=Map[u][i];
        if(!m[u][v]&&!belong[v])
        {
            belong[v]=cc;
            dfs(v);
        }
    }

}
int main()
{
    while(~scanf("%d%d",&F,&R))
    {
        init();
        for(int i=0; i<R; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            Map[a].push_back(b);
            Map[b].push_back(a);
        }
        tarjan(1,-1);
        cc=0;
        for(int i=1; i<=F; i++)
        {
            if(!belong[i])
            {
                cc++;
                belong[i]=cc;
                dfs(i);
            }
        }
        for(int i=1; i<=F; i++)
        {
            int len=Map[i].size();
            for(int j=0; j<len; j++)
            {
                int v=Map[i][j];
                if(belong[i]!=belong[v])
                {
                    degree[belong[i]]++,degree[belong[v]]++;
                }

            }
        }
        int cnt=0;
        for(int i=1; i<=cc; i++)
        {
            if(degree[i]==2)cnt++;
        }
        printf("%d\n",(cnt+1)/2);


    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值