hdu4587 TWO NODES(无向图割点)

题意给你一个无向图,问你从这个无向图中删除任意两个点之后所能获得的独立连通分量个数的最大值.

思路枚举需要删除的第一个点u,然后在这个G-u的新图中,找删除一个点能增加连通分量数量最多的,然后维护最大值就好了

吐槽:又忘记初始化TLE了无数次..太弱了..


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 5005
#define LL long long
int cas=1,T;
int n,m;
int sum = 0;
int dfs_clock;            //时钟,每访问一个结点增1
vector<int>G[maxn];       //图
int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问
int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
bool iscut[maxn];         //标记i结点是不是一个割点
int cut[maxn];            //切割这个结点后的儿子数
//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥
//初始调用dfs(root,-1)
int dfs(int u,int fa,int forb)
{
	int lowu=pre[u]=++dfs_clock;
	int child = 0;                //子结点数目
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		if (v==forb)
			continue;
		if (!pre[v])
		{
			child++;              //未访问过的结点才能算是u的孩子
			int lowv = dfs(v,u,forb);
			lowu = min(lowu,lowv);
			if (lowv >=pre[u])
			{
			//	iscut[u]=1;           //u是割点
				cut[u]++;
			//	if (lowv > pre[u])       //(u,v)边时桥
			//		printf("qiao")
			}
		}
		else if (pre[v] <pre[u] && v!=fa)  //v!=fa确保了(u,v)是从u到v的反向边
		{
			lowu = min(lowu,pre[v]);
		}
	}
	if (fa <0)   //若u是根
	{
		cut[u]--;
	}
	return low[u]=lowu;
}
void init()
{
	dfs_clock = 0;
	sum=0;
	memset(pre,0,sizeof(pre));
//	memset(iscut,0,sizeof(iscut));
	memset(cut,0,sizeof(cut));
	for (int i = 0;i<=n;i++)
		G[i].clear();
}
int main()
{
	while (scanf("%d%d",&n,&m)==2)
	{
		init();
		int u,v;
		for (int i = 0;i<m;i++)
		{
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		int ans = -100;
		for (int i = 0;i<n;i++)
		{
			sum=0;
			dfs_clock = 0;
			memset(cut,0,sizeof(cut));
			memset(pre,0,sizeof(pre));
			for (int j = 0;j<n;j++)
			{
                if (!pre[j] && i!=j)
				{
					sum++;
					dfs(j,-1,i);
				}
			}
			for (int j = 0;j<n;j++)
				if (i!=j)
					ans=max(ans,sum+cut[j]);
		}
		printf("%d\n",ans);

	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值