HDU - 4612 Warm up the tree reduced diameter point + DCC

First, the content

 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

Second, the idea

  • DCC build a tree after shrinking point, all the edges of the tree are the bridge. Any number of bridges connecting one side so that minimum. So the tree is to find the longest path - the diameter of the tree.
  • Ans = number of bridges - the diameter of the tree

Third, the code

#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;
}
Published 446 original articles · won praise 455 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_41280600/article/details/104435634