Warm up HDU - 4612 Tarjan,树的直径

  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

题解:非常水的一道题

1.强连通分量缩点,化成树,然后统计桥的数量。

2.求出树的直径的长度

3.答案即是桥的数量-树的直径的长度


题解:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set> 
#include <queue> 
using namespace std;
const int MAXN = 200007;
int head[MAXN];
int DFN[MAXN];
int LOW[MAXN];
int visit[MAXN];
int visit2[MAXN];
int p[MAXN];
int stack[MAXN];
int belong[MAXN];
int d[MAXN];
int tot;
int sp;
int cnt;
int real_cnt;
int bridges;
int sccnum;
set<int> st[MAXN];
struct Es{ 
    int u,v,id; 
    int next; 
    int cost; 
}Es[2000007];  
void init(){ 
    sccnum = sp = bridges = real_cnt = tot = cnt = 0; 
    memset(head,-1,sizeof(head)); 
    memset(p,-1,sizeof(p));
    memset(visit,0,sizeof(visit));
    memset(DFN,0,sizeof(DFN));
    memset(LOW,0,sizeof(LOW));
    memset(belong,0,sizeof(belong));
}
inline void add_edge(int i,int j,int id){  
	Es[cnt].u = i; 
    Es[cnt].v = j; 
    Es[cnt].next = head[i];
	Es[cnt].id = id; 
    head[i] = cnt++; 
}   
void tarjan(int x)
{
	visit[x] = 1;
	stack[sp++] = x;
	DFN[x]=LOW[x]=++tot;
	for(int i=head[x];i!=-1;i=Es[i].next)
	{
		int v = Es[i].v;
		if(!visit[v]){
			p[v] = Es[i].id;
			tarjan(v);
			LOW[x] = min(LOW[x],LOW[v]);
		}
		else if(Es[i].id != p[x]){
			LOW[x] = min(LOW[x],DFN[v]);
		}
	}
	if(LOW[x] == DFN[x]){
		sccnum++;
		if(p[x] != -1) 
			bridges ++;
		int top;
		do{
			top = stack[--sp];
			belong[top] = sccnum;
		} while(top != x);
	} 
    return ;
}
int N,M;
void bfs(int x){
	memset(visit2,0,sizeof(visit2));
	memset(d,0,sizeof(d));
	queue<int> Q;
	Q.push(x);
	visit2[x] = 1;
	d[x] = 0;
	while(!Q.empty()){
		int u = Q.front();Q.pop();
		visit2[u] = 1;
		set<int>::iterator it = st[u].begin();
		for(;it != st[u].end();++it){
			int v = *it;
			if(!visit2[v]){
				d[v] = d[u] + 1; 
				Q.push(v);
			}
		}
	} 
}
int main(){
	while(scanf("%d%d",&N,&M) && M+N){
		for(int i = 0;i < MAXN;i++) st[i].clear(); 
		init();
		for(int i = 0;i < M;i++){
			int a,b;
			scanf("%d%d",&a,&b);
			add_edge(a,b,real_cnt);add_edge(b,a,real_cnt++);
		}
		for(int i = 1;i <= N;i++){
			if(!DFN[i]){
				tarjan(i);
			} 
		}
		for(int i = 1;i <= N;i++){
			for(int e = head[i];e != -1;e = Es[e].next){
				int v = Es[e].v;
				if(belong[v] == belong[i]) continue;
				st[belong[i]].insert(belong[v]);
				st[belong[v]].insert(belong[i]);
			}
		}
		bfs(1);
		int maxi = 0,vertex;
		for(int i = 1;i <= sccnum;i++){
			if(d[i] > maxi){
				maxi = d[i];
				vertex = i;
			} 
		} 
		bfs(vertex);
		int ans = 0;
		for(int i = 1;i <= sccnum;i++){
			ans = max(ans,d[i]);
		}
		printf("%d\n",bridges - ans);
	}
	
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值