【連通圖|邊雙連通+縮點】POJ-3352 Road Construction

Road Construction
Time Limit: 2000MS Memory Limit: 65536K

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
CCC 2007


題意: 給出一張無向圖,問至少添加多少條邊可以使得該圖變成邊雙連通圖。
思路: 一開始我還以爲是要求邊連通度,但是邊連通度的意義並不是這樣。邊連通度指的是至少刪除多少條邊可以使得原來的連通圖變得不連通。後來發現原來和這道題是一模一樣的:參考POJ-3177

統計出樹中度爲1的節點的個數,即爲葉節點的個數,記爲leaf。則至少在樹上添加(leaf+1)/2條邊,就能使樹達到邊雙連通,所以至少添加的邊數就是(leaf+1)/2

代碼如下:

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 1e3 + 5, M = 2e3 + 5;
struct Edge {
    int v, next, idx;
    Edge(){}
    Edge(int _v, int _next, int _idx):
        v(_v), next(_next), idx(_idx){}
}e[M];
int n, m, deep, tot, bcc_cnt;
int dfn[N], head[N], bcc_id[N], deg[N], line[N][2];
bool isbri[N];

void init()
{
    bcc_cnt = tot = deep = 0;
    memset(head, -1, sizeof(head));
    memset(isbri, 0, sizeof(isbri));
    memset(deg, 0, sizeof(deg));
    memset(dfn, 0, sizeof(dfn));
    memset(bcc_id, 0, sizeof(bcc_id));
}

void add(int u, int v, int idx)
{
    e[tot] = Edge(v, head[u],idx);
    head[u] = tot++;
}

int dfs(int u, int fa)
{
    int lowu = dfn[u] = ++deep;
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if(lowv > dfn[u]) isbri[e[i].idx] = true;
        }
        else if(dfn[v] < dfn[u] && v != fa) lowu = min(lowu, dfn[v]);
    }
    return lowu;
}

void flood(int u)
{
    bcc_id[u] = bcc_cnt;
    for(int i = head[u]; ~i; i = e[i].next) {
        if(isbri[e[i].idx]) continue;
        int v = e[i].v;
        if(!bcc_id[v]) flood(v);
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    scanf("%d%d", &n, &m);
    init();
    int u, v, ID = 0;
    for(int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        u--; v--;
        line[i][0] = u; line[i][1] = v;
        add(u, v, ID);
        add(v, u, ID++);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++) {
        if(!bcc_id[i]) {
            bcc_cnt++;
            flood(i);
        }
    }
    for(int i = 0; i < m; i++) {
        int u = bcc_id[line[i][0]], v = bcc_id[line[i][1]];
        if(u != v) {
            deg[u]++; deg[v]++;
        }
    }
    int ans = 0;
    for(int i = 1; i <= bcc_cnt; i++) {
        if(deg[i] == 1) ans++;
    }
    printf("%d\n", (ans+1) / 2);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章