2019牛客暑期多校训练营(第五场)E.independent set 1(状压dp)

题意:给你n个点 m条边 问你所有子图的最大独立集的和

思路:我们可以设f state 为当前点集下的最大独立集的大小 所以我们可以把集合分为两个部分 绝对包含了这个一个点 绝对不包含这个点 两种情况 然后去去一个最大值

由于空间开的比较紧 我们要用char类型的数组

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 1e5+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e7+9;
int e[27];
char f[1<<26];
char max(char a,char b){
    if(a>b) return a;
    else return b;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n,m; cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b; cin>>a>>b;
        e[a]=(e[a]|(1<<b));
        e[b]=(e[b]|(1<<a));
    }
    for(int i=0;i<n;i++){
        e[i]=(e[i]|(1<<i));
        e[i]=(~e[i]);
    }
    ll ans=0;
    f[0]=0;
    for(int i=1;i<(1<<n);i++){
        int po;
        for(int j=0;j<26;j++)
            if((i>>j)&1){
                po=j;
                break;
            }
        f[i]=max(f[i^(1<<po)],f[(i&e[po])]+1);
        ans+=f[i];
    }
    cout<<ans<<endl;
}

猜你喜欢

转载自www.cnblogs.com/wmj6/p/11317584.html