Petya and Spiders DLX+重複覆蓋 最小支配集(最少多少個點能覆蓋所有的點)

Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 40, n·m ≤ 40) — the board sizes.

Output

In the first line print the maximum number of cells without spiders.

Sample test(s)
Input
1 1
Output
0
Input
2 3
Output
4
Note

In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

rdl
rul

s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.


//


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define eps 1e-8
#define N 185
#define V 36000
int n,m;//n行 m列
int L[V],R[V];
int D[V],U[V];
int C[V];
int S[N],H[N],mark[V],OK[N],tans[V];
int ak,size;//ak 最少多少行可以覆蓋所有列(可重複)
void Link(int r,int c)
{
    S[c]++;C[size]=c;
    U[size]=U[c];D[U[c]]=size;
    D[size]=c;U[c]=size;
    if(H[r]==-1) H[r]=L[size]=R[size]=size;
    else
    {
        L[size]=L[H[r]];R[L[H[r]]]=size;
        R[size]=H[r];L[H[r]]=size;
    }
    mark[size]=r;
    size++;
}
void remove(int c)
{
    int i;
    for(i=D[c];i!=c;i=D[i])
        L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
    int i;
    for(i=U[c];i!=c;i=U[i])
        L[R[i]]=R[L[i]]=i;
}
int h()
{
    int i,j,k,count=0;
    bool visit[N];
    memset(visit,0,sizeof(visit));
    for(i=R[0];i;i=R[i])
    {
        if(visit[i]) continue;
        count++;
        visit[i]=1;
        for(j=D[i];j!=i;j=D[j])
        {
            for(k=R[j];k!=j;k=R[k])
                visit[C[k]]=1;
        }
    }
    return count;
}
void Dance(int k)
{
    int i,j,c,Min,ans;
    ans=h();
    if(k+ans>=ak) return;
    if(!R[0])
    {
        if(k<ak)
        {
            ak=k;
            for(int i=0;i<k;i++)
            {
                tans[i]=mark[OK[i]];
            }
        }
        return;
    }
    for(Min=N,i=R[0];i;i=R[i])
        if(S[i]<Min) Min=S[i],c=i;
    for(i=D[c];i!=c;i=D[i])
    {
        OK[k]=i;
        remove(i);
        for(j=R[i];j!=i;j=R[j])
            remove(j);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])
            resume(j);
        resume(i);
    }
    return;
}
int xx[5]={0,0,0,1,-1};
int yy[5]={0,1,-1,0,0};
int mat[400][400];
int main()
{
    int tn,tm;
    while(scanf("%d%d",&tn,&tm)==2)
    {
        memset(mat,0,sizeof(mat));
        for(int i=1;i<=tn;i++)
        {
            for(int j=1;j<=tm;j++)
            {
                for(int k=0;k<5;k++)
                {
                    int x=i+xx[k],y=j+yy[k];
                    if(x>=1&&x<=tn&&y>=1&&y<=tm)
                    {
                        mat[(i-1)*tm+j][(x-1)*tm+y]=1;
                    }
                }
            }
        }
        n=tn*tm,m=tn*tm;
        //DLX
        for(int i=0;i<=m;i++)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i+1]=i;R[i]=i+1;
        }R[m]=0;
        memset(H,-1,sizeof(H));
        memset(mark,0,sizeof(mark));
        memset(tans,0,sizeof(tans));
        size=m+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {//cout<<i<<".."<<j<<endl;
                if(mat[i][j]) Link(i,j);
            }
        }
        ak=N;
        Dance(0);
        printf("%d\n",n-ak);
       // for(int i=0;i<ak;i++) cout<<tans[i]<<endl;
    }
    return 0;
}

發佈了743 篇原創文章 · 獲贊 5 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章