[kuangbin带你飞]专题九 连通图 F - Warm up HDU - 4612 缩点+树的直径

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

题意,添加一条边,最多还有几个桥?

注意:有重边

方法:先缩点建树,找到树的直径,用bridge-树的直径。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=200000+100;
const int maxm=1000100;
struct node
{
	int v,next;
}edge[maxm*2],Edge[maxm*2];
struct nodee
{
	int u,v;
}ab[maxm*2];
int head[maxn],dfn[maxn],low[maxn],stack[maxn],instack[maxn];
int top,index,block,bridge,belong[maxn];
int vis[maxm*2],head1[maxn],cnt1,dis[maxn];
int maxx,num;
int cnt=0;
void init()
{
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(stack,0,sizeof(stack));
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	memset(belong,0,sizeof(belong));
	cnt=0;top=index=0;
	block=bridge=0;
}
void add_edge(int u,int v)
{
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void init1()
{
	memset(head1,-1,sizeof(head1));
	cnt1=0;
}
void add_Edge(int u,int v)
{
	Edge[cnt1].v=v;
	Edge[cnt1].next=head1[u];
	head1[u]=cnt1++;
}
void tarjan(int u)
{
	low[u]=dfn[u]=++index;
	stack[top++]=u;
	instack[u]=1;
	int v;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].v;
		if(vis[i])
		{
			continue;
		}
		vis[i]=vis[i^1]=1;
		if(!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v])
		{
			low[u]=min(low[u],dfn[v]);
		}
	}
	if(low[u]==dfn[u])
	{
		int xx;
		block++;
		do
		{
			xx=stack[--top];
			instack[xx]=0;
			belong[xx]=block;
		}while(xx!=u);
	}
}
void bfs(int u)
{
	memset(vis,0,sizeof(vis));
	memset(dis,0x3f,sizeof(dis));
	vis[u]=1;
	dis[u]=0;
	queue<int>q;
	q.push(u);
	maxx=0;num=u;
	while(!q.empty())
	{
		int now=q.front();
		q.pop();
		for(int i=head1[now];i!=-1;i=Edge[i].next)
		{
			int v=Edge[i].v;
			if(vis[v])
			{
				continue;
			}
			vis[v]=1;
			dis[v]=dis[now]+1;
			if(dis[v]>maxx)
			{
				maxx=dis[v];
				num=v;
			}
			q.push(v);	
		}
	}
}
int main ()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		if(m==0&&n==0)
		{
			break;
		}
		init();
		for(int i=1;i<=m;i++)
		{
			int xx,yy;
			scanf("%d%d",&xx,&yy);
			ab[i].u=xx;
			ab[i].v=yy;
			add_edge(xx,yy);
			add_edge(yy,xx);
		}
		tarjan(1);
		memset(vis,0,sizeof(vis));
		init1();
		for(int i=1;i<=m;i++)
		{
			int xx,yy;
			xx=ab[i].u,yy=ab[i].v;
			if(belong[xx]!=belong[yy])
			{
				bridge++;
				add_Edge(belong[xx],belong[yy]);
				add_Edge(belong[yy],belong[xx]);
			}
		}
		bfs(belong[1]);
		bfs(num);
		printf("%d\n",bridge-maxx);
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值