题干:
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
解题报告:
暴力dfs+剪枝。(然而剪枝并不能快多少,也就快了200ms左右)
AC代码:(4134ms)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 55 + 5;
int a[MAX][MAX];
int bk[MAX];
int n,ans;
void dfs(int cur,int tmp) {
if(cur == n+1) {
ans = max(ans,tmp);
return;
}
int tar = cur+1;
dfs(tar,tmp);
for(int j = 1; j<=tmp; j++) {
if(a[tar][bk[j]] == 0) return;
}
bk[tmp+1] = tar;
dfs(tar,tmp+1);
}
int main()
{
while(~scanf("%d",&n) && n) {
ans = 0;
memset(a,0,sizeof a);
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=n; j++) scanf("%d",&a[i][j]);
}
dfs(0,0);
printf("%d\n",ans);
}
return 0 ;
}