HDU 1530 Maximum Clique 图论最大团问题

Maximum Clique

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2802    Accepted Submission(s): 1483

Problem Description
Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex.
 
Input
Input contains multiple tests. For each test:
The first line has one integer n, the number of vertex. (1 < n <= 50)
The following n lines has n 0 or 1 each, indicating whether an edge exists between i (line number) and j (column number).
A test with n = 0 signals the end of input. This test should not be processed.
 
Output
One number for each test, the number of vertex in maximum clique.
 
Sample Input
  
  
5 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0
 
Sample Output
  
  
4
/*
HDU 1530 图论最大团问题 
最大团问题:团(clique)是图论中的用语。对于给定图G=(V,E)。
其中,V={1,…,n}是图G的顶点集,E是图G的边集。
图G的团就是一个两两之间有边的顶点集合。
如果一个团不被其他任一团所包含,即它不是其他任一团的真子集,
则称该团为图G的极大团(maximal clique)。
顶点最多的极大团,称之为图G的最大团(maximum clique)。
最大团问题的目标就是要找到给定图的最大团。
*/
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 60
int n,ans;
int map[N][N],set[N];//set集合用以保存加入的点 

//判断新点是否与原set集合里的点互相连通
bool isClique(int end,int point)
{
	for(int i=1;i<end;i++)
	{
		if(!map[set[i]][point])
			return false;
	}
	return true;
}

void dfs(int depth,int point)
{
	//这个深度,即使后面全加入,还是比当前小,不搜了 
	if(depth+(n-point+1)<=ans)
		return ;
	//从一个点开始,递归深搜,
	//如果该点与前面集合中的点相邻,点加入集合,继续搜下一个点
	//如果该点不与前面集合中的点相邻,i++,搜下一个点,直到搜完所有点 
	for(int i=point;i<=n;i++)
	{
		if(isClique(depth+1,i))
		{
			set[depth+1]=i;
			dfs(depth+1,i+1);
		}
	}
	//搜完所有点,更新最大值 
	if(depth>ans)
		ans=depth;
}

int main()
{
	int i,j;
	while(scanf("%d",&n),n)
	{
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				scanf("%d",&map[i][j]);
		ans=0;
		dfs(0,1);
		printf("%d\n",ans);
	}
	return 0;
} 



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值