「CF1105E」Helping Hiasat

Topic Link

I poke

\(Solution\)

Friends will visit your home state survive in binary

Wherein if the first \ (i \) bit is \ (1 \) , then this friend at the \ (i \) a \ (1 \) after the operation visited your home, otherwise no access.

So if two friends are happy binary number is two friends \ (\ & \) value of \ (0 \)

So this becomes a problem of the maximum independent set

If \ (\ & \) is not \ (0 \) of these two friends is not in a collection.

Look at the data range, we consider binary

We first half, a half-maximal independent set of reservations. The other half used high-dimensional prefix and (in fact, when high-dimensional \ (max \) )

No independent set enumeration process

The answer is independent of the number may be such that the upper set of prefix + independent set and meet the other half. Take a \ (max \) where you can see who does not know the code

My code complexity might be a little wrong, because I was directly in the search, too lazy, really do not want to build map

\(Code\)

#include<bits/stdc++.h>
#define rg register
#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') f=(c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
    return f*x;
}
bitset<1000001> vis[44];
string s;
map<string,int> Map;
int f[44][44],bj[44];
int Ans[20048576][3],c[55],n,m,tot,js;
void dfs(int x,int begin,int end,int ans,int flag){
    if(x==end+1){
        int p=0;
        for(int i=begin;i<=end;i++){
            if(c[i]==1) p++; else continue;
            for(int j=begin+1;j<=end;j++)
                if(c[i]==1&&c[j]==1&&f[i][j])
                    return ;
        }
        Ans[ans][flag]=p;return;
    }
    for(int i=0;i<=1;i++)
        c[x]=i,dfs(x+1,begin,end,(ans<<1)|i,flag);
}

int main(){
    m=read(),n=read();
    for(int i=1;i<=m;i++){
        int opt=read();
        if(opt==1) { tot++;continue;} 
        cin>>s;
        int p=Map[s];
        if(!p) p=Map[s]=++js;
        vis[p][tot]=1;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i!=j&&((vis[i]&vis[j])!=0))
                f[i][j]=1;
    int mid=n>>1,len=1<<(n/2+1),maxx=0;
    dfs(1,1,mid,0,0);
    dfs(mid+1,mid+1,n,0,1);
    for(int i=1;i<len;i<<=1)
        for(int j=0;j<len;j++)
            if(i&j)
                Ans[j][1]=max(Ans[j][1],Ans[i^j][1]);
    for(int i=0;i<len;i++){
        int x=i,pp=mid;
        memset(bj,0,sizeof(bj));
        while(x){
            int now=x&1;
            if(now==1)
                for(int j=mid+1;j<=n;j++)
                    if(f[pp][j])
                        bj[j]=1;
            x>>=1,pp--;
        }
        int y=0;
        for(int j=mid+1;j<=n;j++)
            y=((y<<1)|(bj[j]^1));
        maxx=max(maxx,Ans[i][0]+Ans[y][1]);
    }
    cout<<maxx;
    return 0;
}

Guess you like

Origin www.cnblogs.com/hbxblog/p/11766216.html