[POJ]3352 Road Construction 双连通分量缩点

Road Construction
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12499 Accepted: 6284

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


题目大意: 给出一个无向图, 问最少加几条边使任意割掉一条边整个图依然联通(即为双连通图).

题解: 很明显割掉割边后整个图就不联通, 那么就将边双连通分量缩成一个点(当然没有真缩). 然后整个图就成了一棵树, 现在就是使这棵树成为一个双连通图. 那么答案就是(度数为1的点的个数 + 1) / 2.画画图就知道了, 实际上就是这样的点两两连起来即可. 

因为一个边双连通分量所有点的low值相同, 所以把low值作为一个边双连通分量的编号即可. for一遍所有边算度数, 最后统计度数为1的边双连通分量即可.

10.11更新: tarjan算出来的low值相同的一定在一个边双连通分里, 但不相同的也可能是. 因为u的low值指的是dfs树上通过u的子孙能直接连到的最早的祖先. 所以要通过栈来保存整个边双连通分量的点, 然后一起更新low值(也可以是用其他数组...达到染色目的就行了). 差点误会tarjan大大了. 总算对low值有了一个重新的认识. 不代表tarjan算出来的low值是错的, 照样能判断割点割边(本来就是对的..), 只是我一开始以为是指图上的最早祖先. 实质上换个dfs序就不一样了.

可以看看代码尾部的数据. 如果使用链式前向星存的把5, 6这条边放到最后输入再试试. 答案应该是2.

#include<stdio.h>  
#include<cstring>  
#include<algorithm>  
#define clear(a) memset(a, 0, sizeof(a))  
using namespace std;  
const int maxn = 100005;  
int n, m, num, idx, lea;  
int h[maxn], dfn[maxn], low[maxn], d[maxn], sta[maxn], top;  
struct edge{int nxt, v, u;}e[maxn * 10];  
inline void add(int u, int v){  
    e[++num].v = v, e[num].u = u, e[num].nxt = h[u], h[u] = num;  
    e[++num].v = u, e[num].u = v, e[num].nxt = h[v], h[v] = num;  
}  
void dfs(int u, int fa){  
    dfn[u] = low[u] = ++idx;  
    sta[++top] = u;
    for(int i = h[u]; i; i = e[i].nxt){  
        int v = e[i].v;  
        if(!dfn[v]) dfs(v, u), low[u] = min(low[u], low[v]);  
        else if(dfn[v] < dfn[u] && v != fa) low[u] = min(low[u], dfn[v]);  
    }
    if(low[u] == dfn[u]){
    	while(sta[top + 1] != u){
    		low[sta[top]] = u;
    		top--;
		}
	}
}  
int main(){  
    idx = 0;  
    scanf("%d%d", &n, &m);  
    for(int i = 1; i <= m; ++i){  
        int u, v;  
        scanf("%d%d", &u, &v);  
        add(u, v);  
    }  
    dfs(1, -1);
    for(int i = 1; i <= 2 * m; i += 2)  
        if(low[e[i].u] != low[e[i].v]) d[low[e[i].u]]++, d[low[e[i].v]]++;  
    for(int i = 1; i <= n; ++i)  
        if(d[i] == 1) lea++;  
    printf("%d\n", (lea + 1) / 2);  
    return 0;
}
/*
11 14
1 2
2 5
2 6
5 11
6 11
5 6
1 3
3 7
3 8
7 8
1 4
4 9
4 10
9 10
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值