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

标签: c++
264人阅读 评论(0) 收藏 举报
分类:

E - Road Construction
Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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


思路: 

  tarjan->缩点(割边)   

  (num(出度为1节点)+1)/2


#include <cstring>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int num;
int cnt[1005];
int deep[1005];
int now[1005];
vector<int> node[1005];
void tarjan(int u,int father)
{
    now[u]=deep[u]=++num;
    for(int i=0; i<node[u].size(); ++i)
    {
        int p=node[u][i];
        if( p!=father && deep[p]<deep[u])
        {
            if(!deep[p])
            {
                tarjan(p,u);
                now[u]=min(now[p],now[u]);
            }
            else now[u]=min(now[u],deep[p]);
        }
    }
}
int main()
{
    int n,m,i,j,k,a,b;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0; i<=n; ++i)
            node[i].clear();
        while(m--)
        {
            scanf("%d%d",&a,&b);
            node[a].push_back(b);
            node[b].push_back(a);
        }
        memset(deep,0,sizeof(deep));
        memset(now,0,sizeof(now));
        memset(cnt,0,sizeof(cnt));
        num=0;
        tarjan(1,1);
        for(i=1; i<=n; ++i)
            for(j=0; j<node[i].size(); ++j)
            if(now[i]!=now[ node[i][j] ])
                ++cnt[ now[i] ];
        int sum=0;
        for(i=1; i<=n; ++i)
            if(cnt[i]==1)++sum;
        printf("%d\n",(sum+1)/2);
    }
    return 0;
}


查看评论

[双连通分量]poj1679 Road Construction

题意给出一个连通的无向图,求至少增加多少条边让这个图变成边双连通图思路其实题目意思很清晰很明白,但是怎么操作才是重点。 那么首先先想想怎么把整个图简化一下, 先求一次边双连通分量。 因为边双连通分...
  • baidu_35009437
  • baidu_35009437
  • 2017-07-31 22:26:35
  • 78

poj 3352--Road Construction(双连通分量)

题意:给定一无向连通图
  • dingzuoer
  • dingzuoer
  • 2014-10-30 15:46:02
  • 198

poj3352——Road Construction(双连通分量)

DescriptionIt’s almost summer time, and that means that it’s almost summer construction time! This y...
  • blue_skyrim
  • blue_skyrim
  • 2017-04-28 20:19:47
  • 784

POJ3352.Road Construction——边-双连通分量

http://poj.org/problem?id=3352题目描述: 某个企业想把一个热带天堂岛变成旅游胜地,岛上有N个旅游景点,任意2个旅游景点之间有路径连通(注意不一定是直接连通)。而为了给游...
  • u014141559
  • u014141559
  • 2015-04-30 09:26:18
  • 338

poj 3177 poj 3352 (边双连通分量裸题+缩点)

题目:http://poj.org/problem?id=3177 题意: 这两题几乎一样,都是给一个连通图,问最少加几条边可以得到边双连通图。 分析: 先求出边双连通分量(low[i]值相...
  • hjt_fathomless
  • hjt_fathomless
  • 2016-10-02 19:12:00
  • 257

双连通分量知识点讲解

[点连通度与边连通度]       在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合。一个图的点连...
  • huangshuai147
  • huangshuai147
  • 2016-05-09 20:53:58
  • 965

[双连通分量] tarjan算法

在前面说的话其实这个借鉴了网上的一些教程/总结,但是主要还是看lrj的蓝书并有部分引用以及自己的一些理解而成的,仅仅是为了给自己或他人总结用的,而不希望用于任何其他的用途亦或是被说抄袭。引入首先先来看...
  • baidu_35009437
  • baidu_35009437
  • 2017-07-26 20:09:50
  • 646

poj3177Redundant Paths【构造双连通分量:并查集缩点 模板】

这个题就是问加几条边可以构成双连通分量,一开始图样图森破的以为只是求桥的个数就好,然而并非如此…… 构造双连通分量的加边数=(原图的叶节点数+1)/2    因为双连通分量需要成环嘛,原图已经是连着的...
  • zhou_yujia
  • zhou_yujia
  • 2015-11-20 10:12:27
  • 653

[双连通分量]poj 3352 Road Construction

/** [双连通分量]poj 3352 Road Construction 题意,求一个无向图至少要添加几条边才能成为边双连通图。 1,考虑一棵树,至少要添加(d1 + 1) / 2条边 (d1为度为...
  • cscj2010
  • cscj2010
  • 2012-08-16 01:41:50
  • 418

双连通分量模板以及对一些不好理解点的解释

概念: 双连通分量有点双连通分量和边双连通分量两种。若一个无向图中的去掉任意一个节点(一条边)都不会改变此图的连 通性,即不存在割点(桥),则称作点(边)双连通图。一个无向图中的每一个极大点(边)双连...
  • yo_bc
  • yo_bc
  • 2017-06-09 18:32:29
  • 705
    个人资料
    持之以恒
    等级:
    访问量: 12万+
    积分: 5185
    排名: 6647
    最新评论