HDU - 4612 Warm up 树的直径+DCC缩点

一、内容

 N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.

Input

  The input contains multiple cases.
  Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
  (2<=N<=200000, 1<=M<=1000000)
  Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two integers '0' terminates the input.

Output

  For each case, output the minimal number of bridges after building a new channel in a line.

Sample Input

4 4
1 2
1 3
1 4
2 3
0 0 

Sample Output

0

二、思路

  • DCC缩点后建立一棵树, 树中的所有边都是桥。 任意连接一条边使桥的数量最少。 那么就是要找出树的最长路径–树的直径。
  • ans = 桥的数量 - 树的直径

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e5 + 5, M = 4e6 + 6;
struct E {int v, next;} e[M];
int n, m, u, v, len, ans, dh[N], h[N], dcc_cnt, id[N], num, dfn[N], low[N], d[N];
bool brid[M], vis[N]; 
void add(int h[], int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
void tarjan(int u, int in_edge) {
	dfn[u] = low[u] = ++num;
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (!dfn[v]) {
			tarjan(v, j);
			low[u] = min(low[u], low[v]);
			if (dfn[u] < low[v]) brid[j] = brid[j ^ 1] = true;
		} else if ((j ^ 1) != in_edge) low[u] = min(low[u], dfn[v]);
	} 
}
void dfs(int u) {
	id[u] = dcc_cnt;
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (id[v] || brid[j]) continue;
		dfs(v);
	} 
}
void dp(int u) {
	vis[u] = true;
	for (int j = dh[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (vis[v]) continue;
		dp(v);
		ans = max(ans, d[u] + d[v] + 1);
		d[u] = max(d[u], d[v] + 1);
	}
}
int main() {
	while (scanf("%d%d", &n, &m), n) {
		memset(h, 0, sizeof(h)); len = num = 1;
		memset(dh, 0, sizeof(dh));
		memset(id, 0, sizeof(id));
		memset(dfn, 0, sizeof(dfn));
		memset(d, 0, sizeof(d));
		memset(brid, false, sizeof(brid));
		memset(vis, false, sizeof(vis));
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &u, &v);
			add(h, u, v); add(h, v, u);
		}
		tarjan(1, 0); dcc_cnt = 0; 
		//缩点 建树
		for (int i = 1; i <= n; i++) {
			if (!id[i]) {	
			  	dcc_cnt++;
			 	dfs(i);
			 }
		} 
		int tlen = len;  
		for (int j = 2; j <= tlen; j += 2) { 
			u = id[e[j].v], v = id[e[j ^ 1].v];
			if (u == v) continue;
			add(dh, u, v); add(dh, v, u);  
		} 
		ans = 0;
		dp(1); //求出直径 
		printf("%d\n", dcc_cnt - 1 - ans);
	}	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值