CodeForces 1105E Helping Hiasat 最大独立集

Helping Hiasat  

题解:

如果我们把连续的2出现的人都相互连边的话, 题目就是问最大独立集的答案是多少。

求最大独立集可以将图变成反图, 然后求最大团。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 2e5 + 100;
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;
    }
}
map<string,int> mp;
int main(){
    int m, cnt = 0;
    scanf("%d%d", &m, &n);
    LL x = 0;
    int op;
    for(int i = 1;i <= m; ++i){
        scanf("%d", &op);
        if(op == 1){
            for(int j = 0; j <= n;++j)
                for(int k = 1;k <= n; ++k)
                    if(((x>>j)&1) && ((x>>k)&1))
                        e[j][k] = e[k][j] = 1;
            x = 0;
        }
        else{
            string s;
            cin >> s;
            if(!mp.count(s)) mp[s] = ++cnt;
            x |= (1ll << mp[s]);
        }
    }
    for(int j = 0; j <= n;++j)
            for(int k = 1;k <= n; ++k)
                if(((x>>j)&1) && ((x>>k)&1))
                    e[j][k] = e[k][j] = 1;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            e[i][j] ^= 1;
//    cout << "?" << endl;
    maxclique();
    printf("%d
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/MingSD/p/10916188.html