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<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define INF 1000000001 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 55; int mp[MAXN][MAXN],n,maxv[MAXN],ans,a[MAXN][MAXN];//maxv[]表示节点"i到n"能够构成的最大团节点个数、 //a[i][]表示i已经在最大团内的时候,还可能有多少个节点能够加入到最大团中。 int dfs(int cur,int x)//x指递归层数 也就是dfs树的层数 也就是最大团的节点个数 { if(cur == 0){ if(x > ans){ ans = x; return 1; } return 0; } for(int i = 0; i < cur; i++){ if(cur - i + x <= ans)return 0;//假设集合x中的所有点都是在当前的团中 但是个数还是小于已知的值 不必处理 int u = a[x][i]; if(maxv[u] + x <= ans)return 0;//由于我们是从大到小求出答案的 所以这里u有可能之前已经求过。所以这时候可以减枝 //maxv[u]说明后面所有可能的最大值已经不能比结果大了 不必在处理 int num = 0; for(int j = i + 1; j < cur; j++){ if(mp[u][a[x][j]])a[x+1][num++] = a[x][j]; } if(dfs(num,x+1))return 1;//一旦找到值就可以返回 因为后面的点所含的点数只会越来越少 } return 0; } void solve() { ans = 0; memset(maxv,0,sizeof(maxv)); for(int i = n; i >= 1; i --){ int cur = 0; for(int j = i + 1; j <= n; j++){ if(mp[i][j]){ a[1][cur++] = j; } } dfs(cur,1); maxv[i] = ans; } printf("%d ",ans); } int main() { while(~scanf("%d",&n)){ if(!n)break; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ scanf("%d",&mp[i][j]); } } solve(); } return 0; }