您的位置:首页 > 其它

tarjan深搜,缩点: Road Construction

2012-07-30 15:26 155 查看
H - Road Construction
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
SubmitStatus

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 ton. Each of the followingr lines will consist of two integers,v andw, separated by a space, indicating that a road exists between the attractions labelledv andw. 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个点各种相连,让你增添最少的边数,使得就算缺了一条边还是全部连通

先用tarjan算法处理各个点,使得属于同一层的点low值为相同;

再缩点,所有low值一样的点可以当同一个点来看待;

缩点的方法,一个点若与它周围的点的度数有不同,则它的度数增加;

最后计算所有度为一的点的个数,答案则是这个个数加一再除以二。

题目sample1分析:

0是虚拟节点

1的low是0

2,5,6的low是1

3,7,8的low是4

4,9,10的low是7

0的度是3

1、4、7的度是1,其余的度是0(注意0,1,4,7的概念不是图中点的序号,而是代表四种low,low值相同的点属于同一层,可以缩成一个点)



#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

vector< int > point[2010]; //point[i][j]是与编号为i的点相连的第j个点的编号
bool co[2010][2010];
bool vis[2010];
int low[2010];
int degree[2010];
int cnt;
int n, r;

void init(){
for(int i = 1;i <= n; i ++){
for(int j = 1; j <= n; j ++){
co[i][j] = false;
}
vis[i] = false;
low[i] = 0;
point[i].clear();
degree[i] = 0;
}
cnt = 0;
}

void dfs(int x, int f){ //递归,tarjan
vis[x] = true;
low[x] = cnt ++;
int len = point[x].size();
for(int i = 0; i < len; i ++){
if(point[x][i] == f)
continue;
if(!vis[point[x][i]]){//遍历非父亲的点
dfs(point[x][i], x);
}
if(low[x] > low[point[x][i]])//更新low值,属于同一个强连通分量点的low值一样
low[x] = low[point[x][i]];
}
}

void outDegree(){ //缩点
int len;
for(int i = 1; i <= n; i ++){
len = point[i].size();
for(int j = 0; j < len; j ++)
if(low[point[i][j]] != low[i]){
degree[low[i]] ++;
}
}
}

int solve(){      //计算度为一的点数,计算要添加边数
int ans = 0;
for(int i = 1; i <= n; i ++){
if(degree[i] == 1)
ans ++;
}
return (ans + 1)/2;
}

int main(){
int i, a, b;
while(~scanf("%d %d", &n ,&r)){
init();
for(i = 1; i <= r; i ++){
scanf("%d %d", &a, &b);
if(co[a][b])
continue;
co[a][b] = true;
point[a].push_back(b); //vector妙用
point[b].push_back(a);
}
dfs(1, 1);
outDegree();
printf("%d\n", solve());
}
return 0;
}


dfs的部分可以用另外一种方式写,这里也给出来:

void dfss(int x, int f){ //递归,tarjan
int tot = 0;
vis[x] = true;
cnt ++;
low[x] = cnt;
dfn[x] = cnt;
int len = point[x].size();
for(int i = 0; i < len; i ++){
if(vis[point[x][i]] == f)
continue;
if(!vis[point[x][i]]){
tot ++;
dfs(point[x][i], x);
low[x] = min(low[x], low[point[x][i]]);
}
low[x] = min(low[x], dfn[point[x][i]]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: