hdoj--1530--Maximum Clique(最大团)



Maximum Clique

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


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
最大团裸题
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1010
int n,m;
int clique[MAXN],map[MAXN][MAXN];
int New[MAXN][MAXN],ans;
int DFS(int t,int cnt)
{
	if(t==0)
	{
		if(ans<cnt)
		{
			ans=cnt;
			return 1;
		}
		return 0;
	}
	for(int i=0;i<t;i++)
	{
		if(t-i+cnt<=ans) return 0;
		int u=New[cnt][i];
		if(clique[u]+cnt <= ans) 
		return 0;
		int num=0;
		for(int j=i+1;j<t;j++)
			if(map[u][New[cnt][j]])
				New[cnt+1][num++]=New[cnt][j];
		if(DFS(num,cnt+1)) return 1;
	}
	return 0;
}
int Maxclique()
{
	memset(clique,0,sizeof(clique));
	ans=0;
	for(int i=n;i>=1;i--)
	{
		int size=0;
		for(int j=i+1;j<=n;j++)
		{
			if(map[i][j])
			New[1][size++]=j;
		}
		DFS(size,1);
		clique[i]=ans;
	}
	return ans; 
}
int main()
{
	while(scanf("%d",&n),n)
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				scanf("%d",&map[i][j]);
		printf("%d\n",Maxclique());
	}
	return 0;
}

你可能感兴趣的:(hdoj--1530--Maximum Clique(最大团))