zoj1492 最大团

Maximum Clique

Time Limit: 10 Seconds       Memory Limit: 32768 KB

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\n",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;
}

 

转载于:https://www.cnblogs.com/sweat123/p/5530594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值