【DLX】Exact cover

3 篇文章 0 订阅

Exact cover

http://acm.hust.edu.cn/problem.php?id=1017

Description

There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows.
Try to find out the selected rows. 

Input

There are multiply test cases.
First line: two integers N, M;
The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.

Output

First output the number of rows in the selection, then output the index of the selected rows. 
If there are multiply selections, you should just output any of them.
If there are no selection, just output "NO".

Sample Input

6 7
3 1 4 7
2 1 4
3 4 5 7
3 3 5 6
4 2 3 6 7
2 2 7

Sample Output

3 2 4 6

算是一个开头吧,下面的是网上搜到的 一个模板,自己加了一些注释。

DLX写起来的确十分复杂啊。


#include<cstdio>
#include<cstring>
#include<climits>
#define N 1024
#define M 1024*110
using namespace std;
int l[M],r[M],d[M],u[M],col[M],row[M],h[M],res[N],cntcol[N];
//l,r,d,u代表左,右,下,上。col,row代表那结点的坐标。h代表行链表。res记录选了哪几行。cntcol记录列中有几个'1'。 
int dcnt=0;
//初始化一个节点
inline void addnode(int &x)
{
    ++x;
    r[x]=l[x]=u[x]=d[x]=x;
}
//将x加入到rowx后
inline void insert_row(int rowx, int x)
{//循环链表 
    r[l[rowx]] = x;
    l[x] = l[rowx];
    r[x] = rowx;
    l[rowx] = x;
}
//将x加入到colx后
inline void insert_col(int colx, int x)
{
    d[u[colx]] = x;
    u[x] = u[colx];
    d[x] = colx;
    u[colx] = x;
}
//全局初始化
inline void dlx_init(int cols)
{
    memset(h,-1,sizeof(h));
    memset(cntcol,0,sizeof(cntcol));
    dcnt=-1;
    addnode(dcnt);//建立'0'头结点 
    for(int i=1;i<=cols;++i)
    {
        addnode(dcnt);//初始化dcnt列 
        insert_row(0,dcnt);//添加dcnt列 
    }
}
//插入矩阵中的"1"节点
inline void insert_node(int x, int y)
{
    cntcol[y]++;//y列中的'1' 的个数+1 
    addnode(dcnt);
    row[dcnt] = x;
    col[dcnt] = y;
    insert_col(y,dcnt);
    if (h[x]==-1) h[x] = dcnt;//建立行链表 
    else insert_row(h[x], dcnt);
}
//删除一列以及相关的所有行
inline void remove(int c)
{
    l[r[c]]=l[c];
    r[l[c]]=r[c];
    for(int i=d[c];i!=c;i=d[i])
        for(int j=r[i];j!=i;j=r[j])
        {
            u[d[j]]=u[j];
            d[u[j]]=d[j];
            cntcol[col[j]]--;
        }
}
//恢复一列以及相关的所有行
inline void resume(int c)
{
    for(int i=u[c];i!=c;i=u[i])
        for(int j=l[i];j!=i;j=l[j])
        {
            u[d[j]]=j;
            d[u[j]]=j;
            cntcol[col[j]]++;
        }
    l[r[c]]=c;
    r[l[c]]=c;
}
//搜索部分
bool DLX(int deep)
{
    if(r[0]==0)
    {
        //Do anything you want to do here
        printf("%d", deep);
        for (int i=0;i<deep;++i) printf(" %d",res[i]);
        puts("");
        return true;
    }
    int min=INT_MAX,tempc;
    for(int i=r[0];i!=0;i=r[i])//优化,选取列中'1'个数最少的一列 
        if(cntcol[i]<min)
        {
            min=cntcol[i];
            tempc=i;
        }
    remove(tempc);
    for (int i=d[tempc];i!=tempc;i=d[i])
    {
        res[deep]=row[i];
        for(int j=r[i];j!=i;j=r[j]) remove(col[j]);
        if(DLX(deep+1)) return true;
        for(int j=l[i];j!=i;j=l[j]) resume(col[j]);
    }
    resume(tempc);
    return false;
}
int main()
{
    int n,m;
    for(;scanf("%d%d",&n,&m)!=EOF;)
    {
        dlx_init(m);
        for(int i=1;i<=n;++i)
        {
            int k,x;
            scanf("%d",&k);
            for(;k--;)
            {
                scanf("%d",&x);
                insert_node(i,x);
            }
        }
        if(!DLX(0)) puts("NO");
    }
    return 0;
}

来源: http://blog.csdn.net/ACM_Ted

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值