zoukankan      html  css  js  c++  java
  • 模板汇总 —— 最大团

    代码:

    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.

      

  • 相关阅读:
    MATLAB01
    Diffie-Hellman 密钥交换
    古典密码
    正则表达式
    装饰器初析
    进制转换的栈实现
    Log4j(异常日志)
    2018/6/6
    2018.1.1T19B3-u4
    2018.1.1T19-B3-U3jiangyi
  • 原文地址:https://www.cnblogs.com/MingSD/p/10916238.html
Copyright © 2011-2022 走看看