【HDU461】【tarjan】【桥】【树的直径】

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4971    Accepted Submission(s): 1105


Problem Description
  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
 

Author
SYSU
 

Source
 

Recommend
zhuyuanchen520




#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <assert.h>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back

int n,m;
const int N = 200100;
const int M = 3000010;
struct Edge
{
	int to,next,id;
}edge[M];
Edge edge2[M];
int H[N],H2[N];
int myblock[N];
int vis[N];
int low[N];
int myst[N];
int inst[N];
int dist[N];
int ok[N];
int times;
int len;
int len2;
int top;
int sccnum;
int maxs;
int end_p;
int brinum;

void addedge(int u,int v,int id)
{
	edge[len].to = v;
	edge[len].id = id;
	edge[len].next = H[u];
	H[u] = len++;
}

void addedge2(int u,int v)
{
	edge2[len2].to = v;
	edge2[len2].next = H2[u];
	H2[u] = len2++;
}
void init()
{
	assert(sizeof(H) >= 5);

	len = 0;
	len2 = 0;
	times = 0;
	top = 0;
	sccnum = 0;
	maxs = 0;
	brinum = 0;

	memset(inst,0,sizeof(inst));
	memset(myst,0,sizeof(myst));
	memset(H,-1,sizeof(H));
	memset(H2,-1,sizeof(H2));
	memset(vis,-1,sizeof(vis));
	memset(low,0,sizeof(low));
	memset(ok,0,sizeof(ok));
	memset(myblock,0,sizeof(myblock));
}


void tarjan(int u,int ei)
{
	vis[u] = low[u] = ++times;
	inst[u] = true;
	myst[top++] = u;
	for(int i=H[u];~i;i=edge[i].next)
	{
		int v = edge[i].to;
		if(ei == edge[i].id) continue;
		if(vis[v] == -1)
		{
			tarjan(v,edge[i].id);
			low[u] = min(low[u],low[v]);
			if(low[v] > vis[u]) brinum ++;
		}
		else if(inst[v])
		{
			low[u] = min(low[u],vis[v]);
		}
	}
	int t;
	if(low[u] == vis[u])
	{
		sccnum ++;
		do
		{
			t = myst[--top];
			inst[t] = false;
			myblock[t] = sccnum;
		} while(t != u);
	}

}

void bfs(int st)
{
	queue<int> q;
	while(!q.empty()) q.pop();
	memset(ok,0,sizeof(ok));
	dist[st] = 0;
	ok[st] = true;
	q.push(st);
	maxs = 0;
	while(!q.empty())
	{
		int now = q.front();
		q.pop();
		for(int ei=H2[now];~ei;ei=edge2[ei].next)
		{
			int v = edge2[ei].to;
			if(ok[v]) continue;
			ok[v] = true;
			q.push(v);
			dist[v] = dist[now] + 1;
			if(dist[v] > maxs)
			{
				maxs = dist[v];
				end_p = v;
			}
		}
	}

}
void solve(int n)
{
	
	for(int i=1;i<=n;i++)
	{
		if(vis[i] == -1)
			tarjan(i,-1);
	}

	for(int u=1;u<=n;u++)
	{
		for(int i=H[u];~i;i=edge[i].next)
		{
			int v = edge[i].to;
			if(myblock[u] != myblock[v])
			{
				addedge2(myblock[u],myblock[v]); // 之前加了双向边的.
			}
		}
	}
	bfs(1);
	bfs(end_p);
	printf("%d\n",brinum - maxs);
}
int main()
{
	while(scanf("%d%d",&n,&m) )
	{
		if(n == 0 && m == 0) break;
		init();
		for(int i=1;i<=m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addedge(u,v,i);
			addedge(v,u,i);
		}
		solve(n);
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值