【连通图|边双连通+缩点】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;
}
时间: 2024-11-05 11:32:30

【连通图|边双连通+缩点】POJ-3352 Road Construction的相关文章

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

POJ 3352 Road Construction 使得无向图边变双连通图

点击打开链接 Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8168   Accepted: 4106 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

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

POJ 3352 Road Construction(图论-tarjan)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8647   Accepted: 4318 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 ro

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

题目大意:给出一张无向图,问添加多少边才能使得这张无向图变成边双连通分量 解题思路:先求出所有的边双连通分量,再将边双连通缩成一个点,通过桥连接起来,这样就形成了一棵无根树了 现在的问题是,将这颗无根树变成边双连通分量 网上的解释是:统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf.则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2.具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 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 r

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 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 ro

POJ 3352 Road Construction(添最少边构造边双连通图的结论)

题意:已知无向图,问添加最少的边使之成为边双连通图 思路:显然先缩点成一棵树,添加最少边使一棵树的边双连通 此处有结论:对于一棵树添加(1+leaf)>>1 条无向边就能构造成一个双连通图,构造方法显然(脑补一下 //216K 63MS C++ 1754B #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; c

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       Description 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 T