Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)

题目链接

传送门

题意

找第\(k\)小团。

思路

\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都有边则直接用\(bitset\)与一下即可,记得去重。

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 100 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, k;
bitset<101> vis[105];
int w[maxn], mp[maxn][maxn];
struct node {
    LL val;
    bitset<101> has;
    bool operator < (const node& x) const {
        return val > x.val;
    }
}nw;

priority_queue<node> q;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &w[i]);
        nw.has.set(i), nw.val = w[i];
        q.push(nw);
        nw.has.reset(i);
    }
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            scanf("%1d", &mp[i][j]);
            if(mp[i][j]) vis[i].set(j);
        }
    }
    LL ans = -1;
    if(k == 1) {
        printf("0\n");
        return 0;
    }
    --k;
    while(!q.empty()) {
        nw = q.top(), q.pop();
        if(--k == 0) {
            ans = nw.val;
            break;
        }
        int idx = 1;
        for(int i = 1; i <= n; ++i) if(nw.has.test(i)) idx = i + 1;
        for(int i = idx; i <= n; ++i) {
            if(nw.has.test(i)) continue;
            if((vis[i] & nw.has) != nw.has) continue;
            nw.val += w[i];
            nw.has.set(i);
            q.push(nw);
            nw.val -= w[i];
            nw.has.reset(i);
        }
    }
    printf("%lld\n", ans);
    return 0;
}

posted @ 2019-08-22 14:44  Dillonh  阅读(164)  评论(0编辑  收藏  举报