模板汇总 —— 最大团

代码:

int e[50][50];
int cnt[N], group[N], sta[N], ans;
int n;
bool dfs(int u, int deep){
    for(int i = u + 1; i <= n; ++i){
        if(cnt[i] + deep <= ans) return 0;
        if(e[u][i]){
            int j = 0;
            for(; j < deep; ++j) if(!e[i][sta[j]]) break;
            if(j == deep){
                sta[deep] = i;
                if(dfs(i, deep+1)) return 1;
            }
        }
    }
    if(deep > ans){
        for(int i = 0; i < deep; ++i) group[i] = sta[i];
        ans = deep;
        return 1;
    }
    return 0;
}
void maxclique(){
    ans = -1;
    for(int i = n; i; --i){
        sta[0] = i;
        dfs(i, 1);
        cnt[i] = ans;
    }
}
View Code

cnt[i] 为 只考虑 i ~ n的情况下最大团是多少。

所以cnt[i]一定是一个递减的数组。

所以可以提前剪枝 

if(cnt[i] + deep <= ans) return 0;

保证了就选进行下去, 答案也不会大于ans。

然后,只要更新了一次ans, 在这次搜索中不需要再去找答案了。

每次搜索的时候只会加入一个点, 故不可能再变大了。

这是为什么要开bool, 然后teturn 1, teturn 0.

  

原文地址:https://www.cnblogs.com/MingSD/p/10916238.html