码迷,mamicode.com
首页 > 移动开发 > 详细

HDOJ 题目4587 TWO NODES(双联通,割点,枚举)

时间:2015-05-20 22:31:29      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

TWO NODES

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1367    Accepted Submission(s): 410


Problem Description
Suppose that G is an undirected graph, and the value of stab is defined as follows:
技术分享

Among the expression,G-i, -j is the remainder after removing node i, node j and all edges that are directly relevant to the previous two nodes. cntCompent is the number of connected components of X independently.
Thus, given a certain undirected graph G, you are supposed to calculating the value of stab.
 

Input
The input will contain the description of several graphs. For each graph, the description consist of an integer N for the number of nodes, an integer M for the number of edges, and M pairs of integers for edges (3<=N,M<=5000).
Please note that the endpoints of edge is marked in the range of [0,N-1], and input cases ends with EOF.
 

Output
For each graph in the input, you should output the value of stab.
 

Sample Input
4 5 0 1 1 2 2 3 3 0 0 2
 

Sample Output
2
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  5231 5230 5229 5228 5227 
 
题目大意:一个无向图去掉两个点后,最多产生几个联通分量
思路:先去掉一个点,再对去掉点的图进行tarjan求割点
ac代码
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
struct s
{
	int u,v,next;
}edge[10010];
int head[5050],dfn[5050],low[5050],n,m,time,sum,ans,cnt,del,iscnt[5050];
void init()
{
	time=cnt=sum=ans=0;
	memset(head,-1,sizeof(head));
	//	memset(vis,0,sizeof(vis));
	memset(iscnt,0,sizeof(iscnt));
}
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void tarjan(int u,int pre)
{
	low[u]=dfn[u]=time++;
	int i,j;
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(v==del)
			continue;
		if(dfn[v]==-1)
		{
			tarjan(v,u);
			low[u]=min(low[v],low[u]);
			if(low[v]>=dfn[u])
				iscnt[u]++;
		}
		else
		{
			if(dfn[v]<dfn[u]&&v!=u)
			{
				low[u]=min(low[u],dfn[v]);
			}
		}
	}
	if(pre<0)
		iscnt[u]--;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j;
		init();
		for(i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
		}
		for(i=0;i<n;i++)
		{
			del=i;
			time=0;
			memset(low,-1,sizeof(low));
			memset(dfn,-1,sizeof(dfn));
			memset(iscnt,0,sizeof(iscnt));
			sum=0;
			for(j=0;j<n;j++)
			{
				if(j==del)
					continue;
				if(dfn[j]==-1)
				{
					tarjan(j,-1);
					sum++;
				}
			}
			for(j=0;j<n;j++)
			{
				if(j==del)
					continue;
			//	printf("%d %d %d \n",sum,j,iscnt[j]);
				ans=max(ans,sum+iscnt[j]);
			}
		}
		printf("%d\n",ans);
	}
}


HDOJ 题目4587 TWO NODES(双联通,割点,枚举)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/45873831

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!