点击打开链接
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 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
CCC 2007
参考博客:
https://blog.csdn.net/u013480600/article/details/31004741
http://blog.csdn.net/lyy289065406/article/details/6762370
总结题他们的过程:
求边双连通分量,缩点,求度数为1的节点个数cnt,ans=(cnt+1)/2(对于一棵无向树,我们要使得其变成边双连通图,需要添加的边数 == (树中度数为1的点的个数+1)/2)
其实,可以伪缩点:
而由于题目已表明任意两个结点之间不会出现重边,因此Low值相同的两个结点必定在同一个【边双连通分量】中! (如果是有重边的话,那么不同的low值是可能是属于同一个边双连通分量的,这个时候就要通过其他方法去求解边双连通分量。不过这不是本题要讨论的)
所以觉得真缩点好一点
///tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int SIZE = 100010;
int head[SIZE], ver[SIZE * 2], Next[SIZE * 2];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE], c[SIZE];
///dfn表示时间戳
///low表示追溯值,c[x]表示结点x属于边连通分量的编号
int n, m, tot, num, dcc, tc;
bool bridge[SIZE * 2]; ///是否是桥
int hc[SIZE], vc[SIZE * 2], nc[SIZE * 2];
///与上面未缩点表示含义一样,只不过把e——dcc看成一个结点
void add(int x, int y) {ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}void add_c(int x, int y) {vc[++tc] = y, nc[tc] = hc[x], hc[x] = tc;
}void tarjan(int x, int in_edge) {dfn[x] = low[x] = ++num;for (int i = head[x]; i; i = Next[i]) {///遍历每一个结点int y = ver[i];if (!dfn[y]) {tarjan(y, i);low[x] = min(low[x], low[y]);if (low[y] > dfn[x]) ///找到桥bridge[i] = bridge[i ^ 1] = true;}else if (i != (in_edge ^ 1)) ///利用异或性质解决重边///防止x结点到父亲结点low[x] = min(low[x], dfn[y]);}
}void dfs(int x) {c[x] = dcc;for (int i = head[x]; i; i = Next[i]) {int y = ver[i];if (c[y] || bridge[i]) continue;dfs(y);}
}
int d[SIZE];
int main() {cin >> n >> m;tot = 1;for (int i = 1; i <= m; i++) {int x, y;scanf("%d%d", &x, &y);add(x, y), add(y, x);}for (int i = 1; i <= n; i++)if (!dfn[i])tarjan(i, 0);for (int i = 1; i <= n; i++)if (!c[i]) {++dcc;dfs(i);}///缩点tc = 1;for (int i = 2; i <= tot; i++) {int x = ver[i ^ 1], y = ver[i];if (c[x] == c[y]) continue;add_c(c[x], c[y]);}for (int i = 2; i < tc; i += 2){d[vc[i^1]]++;d[vc[i]]++;//printf("%d %d\n", vc[i ^ 1], vc[i]);}int cnt=0;for(int i=1;i<=n;i++)if(d[i]==1)cnt++;printf("%d\n",(cnt+1)/2 );return 0;
}