HDU 4587 TWONODES (tarjan) (2013ACM-ICPC南京赛区全国邀请赛)

3 篇文章 0 订阅
3 篇文章 0 订阅

TWO NODES

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


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
 
题意:一个无向图去掉两个点后形成的图最多有多少个独立集合

思路:枚举第一个点,然后用tarjan求出剩下图中联通最大的割点。然后让第一个去掉的点形成的块加上第二个割点所形成的联通分量就是第一个点所能形成的最佳的答案,然后枚举第一个点求出一个最大值就好了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 5500
using namespace std;
vector< int >G[maxn];
int ans,tot,dfs_clock,pre[maxn],low[maxn],iscut[maxn];
int dfs(int u,int f,int d){
	int lowu=pre[u]=++dfs_clock;
	for(int i=0;i<(int)G[u].size();i++){
		int v=G[u][i];
		if(v==d) continue;
		if(!pre[v]){
			int lowv=dfs(v,u,d);
			lowu=min(lowu,lowv);
			if(lowv>=pre[u]) iscut[u]++;
		}else if(pre[v]<pre[u]&&v!=f){
			lowu=min(lowu,pre[v]);
		}
	}
	if(f<0) iscut[u]--;
	low[u]=lowu;
	return lowu;
}
void init(int n){
	dfs_clock=tot=0;
	memset(pre,0,sizeof(pre));
	memset(iscut,0,sizeof(iscut));
}
int main(int argc,char **argv){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		for(int i=0;i<n;i++)
			G[i].clear();
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		ans=0;
		for(int i=0;i<n;i++){
			init(n);
			for(int u=0;u<n;u++){
				if(u==i||pre[u])continue;
				dfs(u,-1,i);tot++;
			}
			int maxx=-1;//这里坑啊!!!!
			//cout<<i<<' '<<tot<<endl;
			for(int u=0;u<n;u++){
				//cout<<iscut[u]<<' ';
				if(u==i) continue;
				maxx=max(maxx,iscut[u]);
			}
			//cout<<endl;
			ans=max(ans,maxx+tot);
			//cout<<'#'<<ans<<endl;
		}
		printf("%d\n",ans);
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值