POJ 3352 Road Construction 双连通分量+缩点

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


   

    题目大意:给定一个双向连通的公路网,当某些公路路段检修的时候可能会由于该段公路不通,可能会使某些旅游点之间无法通行,求至少新建多少条公路,使得任意对一段公路进行检修的时候,所有的旅游景点之间仍然畅通;

    分析:检修某一路段导致公路网不畅通的原因必然是该段公路在图中是桥(割边),因此完全畅通的方法就是,加最若干条边,使图中不存在桥。先找出图中所有的双连通分量,将双连通分量进行缩点,得到一个树形图,求出这个树形图中度为1的点的个数,新加边的条数即是(度为1的点数目+1)/2,考虑到题目只要求求度为1的点数目,因此可以部分缩点,利用并查集,保存每个割边的顶点,统计每个顶点在并查集中的代表元的度数即可。
    Sample 1中存在4个双连通分量:{1},{2,5,6},{3,7,8},{4,9,10},进行缩点之后,求得一个4个节点的树形图,其中一个点的度数为3,其余3个点的度数为1,得到需要加的边的数目为(3+1)/2=2。

#include <iostream>
#include 
<vector>
using namespace std;

const int MAXN = 5001;
vector
< vector<int> > adj;
int cnt,low[MAXN],pre[MAXN],visit[MAXN],degree[MAXN];

void dfs(int u,int v){
    visit[u]
=1;
    pre[u]
=cnt++,low[u]=pre[u];
    
int i,len=adj[u].size();
    
for(i=0;i<len;i++){
        
if(adj[u][i]==v) continue;
        
if(!visit[adj[u][i]]) dfs(adj[u][i],u);
        
if(low[adj[u][i]]<low[u]) low[u]=low[adj[u][i]];
    }

    visit[u]
=2;
}

int main(){
    
int i,j,u,v,n,m,len,ans;
    scanf(
"%d %d",&n,&m);
    adj.assign(n
+1,vector<int>());
    
while(m--){
        scanf(
"%d %d",&u,&v);
        adj[u].push_back(v),adj[v].push_back(u);
    }

    memset(visit,
0,sizeof(visit));
    cnt
=0,dfs(1,1);
    memset(degree,
0,sizeof(degree));
    
for(i=1;i<=n;i++){
        len
=adj[i].size();
        
for(j=0;j<len;j++)
            
if(low[i]!=low[adj[i][j]])
                degree[low[i]]
++;
    }

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

posted on 2009-05-29 18:53 极限定律 阅读(1542) 评论(3)  编辑 收藏 引用 所属分类: ACM/ICPC

评论

# re: POJ 3352 Road Construction 双连通分量+缩点 2009-08-03 10:47 路人

11 14
1 2
1 3
1 4
2 5
6 11
2 6
5 6
5 11
3 7
3 8
7 8
4 9
4 10
9 10

你的程序,这组数据时错误的。。  回复  更多评论   

# re: POJ 3352 Road Construction 双连通分量+缩点 2009-08-04 09:46 mythit

@路人
多谢指正  回复  更多评论   

# re: POJ 3352 Road Construction 双连通分量+缩点 2011-07-25 19:14 过客

void dfs(int u,int v){
visit[u]=1;
pre[u]=cnt++,low[u]=pre[u];
int i,len=adj[u].size();
for(i=0;i<len;i++){
if(adj[u][i]==v) continue;
if(!visit[adj[u][i]]) dfs(adj[u][i],u);
if(low[adj[u][i]]<low[u]) low[u]=low[adj[u][i]];
}
visit[u]=2;
}
最后一句 visit[u]=2;有什么用啊,求解释  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(10)

随笔分类

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜