poj3352--E - Road Construction(无向图加边成双连通)

E - Road Construction

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status

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

无向图加最少的边,形成双连通,先求解割边,割边将图分为几个双连通分量,缩点,找出叶子节点的个数n,最小边数 = (n+1)/2;

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define maxn 1200
struct node
{
    int u , v ;
    int next ;
} edge[maxn<<2] , p[maxn];
int head[maxn] , cnt , vis[maxn<<2] ;
int dnf[maxn] , low[maxn] , time ;
int degree[maxn] , belong[maxn] , num ;
stack <int> sta;
void init()
{
    while( !sta.empty() ) sta.pop() ;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(dnf,0,sizeof(dnf));
    memset(low,0,sizeof(low));
    memset(degree,0,sizeof(degree));
    memset(belong,0,sizeof(belong));
    cnt = time = num = 0 ;
}
void add(int u,int v)
{
    edge[cnt].u = u ;
    edge[cnt].v = v ;
    edge[cnt].next = head[u] ;
    head[u] = cnt++ ;
    edge[cnt].u = v ;
    edge[cnt].v = u ;
    edge[cnt].next = head[v] ;
    head[v] = cnt++ ;
}
void tarjan(int u)
{
    dnf[u] = low[u] = ++time ;
    int i , v , j ;
    for(i = head[u] ; i != -1 ; i = edge[i].next)
    {
        if( vis[i] ) continue ;
        vis[i] = vis[i^1] = 1 ;
        v = edge[i].v ;
        if( !dnf[v] )
        {
            sta.push(i) ;
            tarjan(v) ;
            low[u] = min( low[u],low[v] );
            if( low[v] > dnf[u] )
            {
                num++ ;
                while(1)
                {
                    j = sta.top();
                    sta.pop();
                    belong[ edge[j].v ] = num ;
                    if(edge[j].u == u)
                        break;
                    belong[ edge[j].v ] = num ;
                }
            }
        }
        else if(dnf[v] < dnf[u])
        {
            sta.push(i);
            low[u] = min( low[u],dnf[v] );
        }
    }
}
int main()
{
    int n , m , i , j , u , v , ans ;
    while( scanf("%d %d", &n, &m)!=EOF )
    {
        init();
        for(i = 0 ; i < m ; i++)
        {
            scanf("%d %d", &p[i].u, &p[i].v);
            add(p[i].u,p[i].v);
        }
        tarjan(1) ;
        if(belong[1] == 0)
            belong[1] = ++num ;
        for(i = 0 ; i < m ; i++)
        {
                u = belong[ p[i].u ];
                v = belong[ p[i].v ];
                if(u != v)
                {
                    degree[u]++ ;
                    degree[v]++ ;
                }
        }
        ans = 0 ;
        for(i = 1 ; i <= num ; i++)
            if(degree[i] == 1)
                ans++ ;
        printf("%d\n", (ans+1)/2);
    }
    return 0;
}

时间: 2024-10-09 15:21:48

poj3352--E - Road Construction(无向图加边成双连通)的相关文章

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

Road Construction POJ - 3352 题意:一个无向图(无重边),问至少还要加多少边使得去掉任意一条边后任意两点仍可互达. 无向图的边双连通分量(无重边) 先用一次dfs标记出割边,然后dfs标记出各联通分量 再根据割边,缩点重新建图,生成一颗树 则答案就是(叶子树+1)/2. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring

如何将无向图变为点/边双连通,如何将有向图变为强连通图

将无向图变为点-双连通的图 定义:点-双连通指的是任意两个之间存在至少两条点不重复的路径 分为两种情况, 一种是连通图,一种是非连通图 ①连通图 首先,找出图中的所有点-双连通分量,然后将该分支缩成一个点, 因为双连通分量内部肯定不用考虑的. 只需要考虑双连通分量与外部的其它结点,如何加边才能形成点-双连通分量. 上图的结点2,3,4,5构成了一个点-双连通分支,我们进行缩点,得到下面的图 可以知道,缩点之后的图是一棵树(因为环都被缩成一个点了嘛) 那么问题就转化为了,如果将一棵树变为点-双连通

【POJ3352】Road Construction tarjan求边-双连通分量,裸题模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42671851 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 裸题只给模板. tarjan可以实现. 太水不发题解. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1010 #define M 202

POJ 3352 Road Construction&amp;&amp; POJ 3177 Redundant Paths 双联通分量

大意:给定n点,和m条边的关系图中的一些边随时可能施工导致不能够通过,所以至少加多少条边才能够使得途中任意两条边联通? 思路:很明显只要图中的任意两点都是两条边来链接即可.那么我们可以先缩点构建新图,然后统计出度为1的点的个数ans,那么需要加的边数就是(ans+1)/2条; (PS;因为建图是双向的图所以,在Tarjan缩点的时候就需要遇到临边便越过,并且判断是不是同一个联通分支用num比较!) #include<map> #include<queue> #include<

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【边双连通求最少加多少条边使图双连通&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

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 Remo

POJ3352 Road Construction Tarjan+边双连通

题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去掉任何一条边都是其他边仍然是连通的,也就是说边双连通图中没有割边. 算法设计是:运用tarjan+缩点.对于每一个边双连通分量,我们都可以把它视作一个点,因为low值相同的点处在同一个边双连通分量中,可以简单地思考一下,(u,v)之间有两条可达的路径,dfs一定可以从一条路开始搜索并且从另一条路回去

POJ3352 Road Construction【边双联通分量】【Tarjan】

题目链接: http://poj.org/problem?id=3352 题目大意: 一个热带天堂岛上有N个旅游景点,任意2个旅游景点之间都有路径(并不一定直接相连).为了使游客 往返更便捷,该旅游公司要求增加一些道路.在施工的时候,每次只能选择一条道路施工,在施工完 毕之前,除了该道路意外,其他道路依旧能够通行.因为施工道路禁止通行,这就导致了在施工期间 游客可能无法到达一些经典. 该公司为了保证在施工期间所有的旅游景点都能够向游客开放,该公司决定搭建一些临时桥梁,使得 无论在哪条道路施工,游