Road Construction POJ - 3352 (边双连通分量)题解

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.


题目大意:一个度假胜地,岛上的路没有交叉,每一条路连着两端的景点,现在要维修道路,但是道路维修的时候不能通行,如果一个景点只由这条路连接的话就无法参观,问至少要多修几条路使得修任何一条路的时候任何一个旅游景点都不会无法到达。

图删去一条边之后不影响连通性,就是双连通图的概念了,这题就是要让你添加最少的边使图变成双连通图,做法就是先找出所有边双联通块(同一个双连通块内的点的low值是相等的),把连通块看成一个点,整张图就变成一颗树,问题转化成如何把这棵树变成双连通图,扫一遍统计所有结点的度数,度数为1的结点就需要加边,加边优先和其他度数为1的点相连,最后需要添加的边就是(度数为1的点的数量+1)/2。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
using namespace std;
#define ll long long
#define N 100005
#define inf 0x3f3f3f3f
int n, m;
vector<int> e[1005];
int pre[1005];
int low[1005];
int deg[1005];
int ans;
int clock;
int dfs(int u, int fa){
    int lowu = pre[u] = ++clock;
    bool k = 1;
    for (int i = 0; i < e[u].size(); i++){
        int v = e[u][i];
        if (!pre[v]){
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
        }
        else if (v != fa){
            lowu = min(lowu, pre[v]);
        }
    }
    low[u] = lowu;
    return lowu;
}

int main(){
    int x, y;
    while (~scanf("%d%d", &n, &m)){
        ans = inf;
        clock = 0;
        memset(pre, 0, sizeof(pre));
        for (int i = 0; i < m; i++){
            scanf("%d%d", &x, &y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        for (int i = 1; i <= n; i++){
            if (!pre[i])dfs(i, -1);
        }
        for (int i = 1; i <= n; i++){
            for (int j = 0; j < e[i].size(); j++){
                if (low[i] != low[e[i][j]]){
                    deg[low[i]]++;
                }
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++){
            if (deg[i] == 1){ ans++; }
        }
        printf("%d\n", (ans + 1) / 2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值