POJ 3740 精确覆盖 模板

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=1000111222;
const double INFF=1e100;
const double eps=1e-8;
//const LL mod=1000000007;
const int NN=305;
const int MM=2000010;
/* ****************** */


const int maxn=305;
const int maxr=20;
const int maxnode=305*20;
int sz;
int ans[maxr],ansd;
int S[maxn],dl_qw[maxn];
int row[maxnode],col[maxnode];
int L[maxnode],R[maxnode],U[maxnode],D[maxnode];

void dl_init(int n)
{
    int i;
    for(i=0;i<=n;i++)
    {
        U[i]=D[i]=i;
        L[i]=i-1;
        R[i]=i+1;
        S[i]=0;
    }
    L[0]=n;
    R[n]=0;
    sz=n+1;
}
/* cnt>0!!!! */
void dl_add(int r,int cnt)
{
    int i,first=sz;
    for(i=0;i<cnt;i++)
    {
        int c=dl_qw[i];

        L[sz]=sz-1;
        R[sz]=sz+1;
        U[sz]=U[c];
        D[sz]=c;

        D[ U[c] ]=sz;
        U[c]=sz;

        row[sz]=r;
        col[sz]=c;

        S[c]++;
        sz++;
    }
    L[first]=sz-1;
    R[sz-1]=first;
}
/* 删除c列 */
/* L,R只用删最上面的就好,其他点删U,D就好了 */
void remove(int c)
{
    int i,j;
    L[R[c]]=L[c];
    R[L[c]]=R[c];
    for(i=D[c];i!=c;i=D[i])
        for(j=R[i];j!=i;j=R[j])
        {
            U[D[j]]=U[j];
            D[U[j]]=D[j];
            --S[col[j]];
        }
}
/* 与remove顺序是相反的 */
void restore(int c)
{
    int i,j;
    for(i=U[c];i!=c;i=U[i])
        for(j=L[i];j!=i;j=L[j])
        {
            ++S[col[j]];
            U[D[j]]=j;
            D[U[j]]=j;
        }
    L[R[c]]=c;
    R[L[c]]=c;
}
bool dl_dfs(int d)
{
    if(R[0]==0)
    {
        ansd=d;
        return true;
    }

    int i,j,c=R[0];

    for(i=R[0];i!=0;i=R[i])
        if(S[i]<S[c])
            c=i;

    remove(c);
    for(i=D[c];i!=c;i=D[i])
    {
        ans[d]=row[i];
        for(j=R[i];j!=i;j=R[j])
            remove(col[j]);
        if(dl_dfs(d+1))
            return true;
        for(j=L[i];j!=i;j=L[j])
            restore(col[j]);
    }
    restore(c);

    return false;
}

int main()
{
    int n,m,i,j,toll,t;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        dl_init(m);

        for(i=1;i<=n;i++)
        {
            toll=0;
            for(j=1;j<=m;j++)
            {
                scanf("%d",&t);
                if(t)
                {
                    dl_qw[toll++]=j;
                }
            }
            if(toll>0)
                dl_add(i,toll);
        }

        if(dl_dfs(0))
            puts("Yes, I found it");
        else
            puts("It is impossible");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值