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

题目大意:给出一张无向图,问添加多少边才能使得这张无向图变成边双连通分量

解题思路:先求出所有的边双连通分量,再将边双连通缩成一个点,通过桥连接起来,这样就形成了一棵无根树了

现在的问题是,将这颗无根树变成边双连通分量

网上的解释是:统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

附上大神的详解

和相关的连通分量的概念

#include <cstdio>
#include <cstring>

#define N 1010
#define min(a,b) ((a)<(b) ?(a):(b))

struct Edge{
    int to, next;
}E[N*2];

int n, m, tot, dfs_clock, bcc_cnt, top, bnum;;
int head[N], pre[N], belong[N], degree[N], stack[N], bridge[N][2];

void Addegreedge(int u, int v) {
    E[tot].to = v; E[tot].next = head[u]; head[u] = tot++;
    u = u ^ v; v = v ^ u; u = u ^ v;
    E[tot].to = v; E[tot].next = head[u]; head[u] = tot++;
}

void init() {
    memset(head, -1, sizeof(head));
    tot = 0;

    int u, v;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        Addegreedge(u, v);
    }
}

int dfs(int u, int fa) {
    int lowu = pre[u]  = ++dfs_clock;
    stack[++top] = u;
    for (int i = head[u]; i != -1; i = E[i].next) {
        int v = E[i].to;

        if (!pre[v]) {
            int lowv = dfs(v, u);
            lowu = min(lowv, lowu);
            if (lowv > pre[u]) {
                bridge[bnum][0] = u;
                bridge[bnum++][1] = v;

                bcc_cnt++;
                while (1) {
                    int x = stack[top--];
                    belong[x] = bcc_cnt;
                    if (x == v) break;
                }
            }
        }else if (pre[v] < pre[u] && v != fa) {
            lowu = min(lowu, pre[v]);
        }
    }
    return lowu;
}

void solve() {
    memset(degree, 0, sizeof(degree));
    memset(pre, 0, sizeof(pre));
    dfs_clock = bcc_cnt = top = bnum = 0;
    dfs(1, -1);

    if (top) {
        bcc_cnt++;
        while (1) {
            int x = stack[top--];
            belong[x] = bcc_cnt;
            if (x == 1)
                break;
        }
    }

    for (int i = 0; i < bnum; i++) {
        int u = bridge[i][0];
        int v = bridge[i][1];
        degree[belong[u]]++;
        degree[belong[v]]++;
    }

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

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 14:25:52

POJ - 3352 Road Construction(边双连通分量)的相关文章

POJ 3352 Road Construction(边双连通分量,桥,tarjan)

题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业想把一个热带天堂岛变成旅游胜地,岛上有N个旅游景点,保证任意2个旅游景点之间有路径连通的(可间接连通).而为了给游客提供更方便的服务,该企业要求道路部门在某些道路增加一些设施. 道路部门每次只会选择一条道路施工,在该条道路施工完毕前,其他道路依然可以通行.然而有道路部门正在施工的道路,在施工完毕前是

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 使得无向图边变双连通图

点击打开链接 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 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(图论-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 &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(边—双连通分量)

http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相等,说明属于同一个双连通分量. 接下来把连通分量缩点,然后把这些点连边. 对于一棵无向树,我们要使得其变成边双连通图,需要添加的边数 == (树中度数为1的点的个数+1)/2. 1 #include<iostream> 2 #include<algorithm> 3 #include&

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(无向连通图)

题目地址:POJ 3352 这题就是求最少加多少条边可以使得图为无向双连通图.方法是找度数为1的连通分量,可以只用low来找.然后根据公式(叶子结点数+1)/2即可得到答案.原因是在图中将每两个度数为1的都连起来,度数为2的显然已经可以形成双联通了,因为是无向边,只要加一条,就相当于加了两条有向边,很显然,结果数就是那个公式. 代码如下: #include <iostream> #include <cstdio> #include <string> #include &