HUST 1017 Exact cover (Dancing Links 模板题)

题目大意:

就是N*M的01矩阵, 初始有C(C <= 100)个1, 问从中选出一些横行是否能使所有列都恰好有一个1


大致思路:

跳舞链解决的矩阵精确覆盖问题, 模板题

模板来自kuangbin大爷Orz


代码如下:

Result  :  Accepted     Memory  :  1916 KB     Time  :  164 ms

/*
 * Author: Gatevin
 * Created Time:  2015/10/1 22:09:50
 * File Name: Sakura_Chiyo.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

/*
 * Dacing Links
 * 舞蹈连算法, 通过四向链表加快搜索速度
 */

#define maxnode 100010
#define maxm 1010
#define maxn 1010

/*
 * 这个模板解决n*m的01矩阵中选择一定的row使得每列都只包含一个1
 * 初始n*m中1的个数只有C个(C <= 100)
 */
struct DLX
{
    int n, m, size;
    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    //Row[i], Col[i]表示第i个点所在的行和列
    
    int H[maxn], S[maxm];
    
    int ansd, ans[maxn];//ansd表示选择的row的数量,存储在ans数组中
    
    void init(int _n, int _m)
    {
        n = _n;
        m = _m;
        for(int i = 0; i <= m; i++)//初始化第0横行(head, C1, C2,..Cm)
        {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        R[m] = 0; L[0] = m;
        size = m;
        for(int i = 1; i <= n; i++) H[i] = -1;
    }
    void Link(int r, int c)//增加结点(r, c)
    {
        ++S[Col[++size] = c];
        Row[size] = r;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r] < 0) H[r] = L[size] = R[size] = size;
        else
        {
            R[size] = R[H[r]];
            L[R[H[r]]] = size;
            L[size] = H[r];
            R[H[r]] = size;
        }
    }
    void remove(int c)//(选择)移除列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];
                --S[Col[j]];
            }
    }
    void resume(int c)//(取消选择)恢复列c
    {
        for(int i = U[c]; i != c; i = U[i])
            for(int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }
    
    bool Dance(int dep)//dep为递归深度
    {
        if(R[0] == 0)//每一列都有1了
        {
            ansd = dep;
            return true;
        }
        int c = R[0];
        for(int i = R[0]; i != 0; i = R[i])//现在考虑从还需要1的那些列中选择对应的行删除
            if(S[i] < S[c])
                c = i;//从列中1最少的列处理起
        remove(c);//移除列c
        for(int i = D[c]; i != c; i = D[i])//以这1列中的第i行做贡献
        {
            ans[dep] = Row[i];
            for(int j = R[i]; j != i; j = R[j]) remove(Col[j]);
            if(Dance(dep + 1)) return true;
            for(int j = L[i]; j != i; j = L[j]) resume(Col[j]);
        }
        resume(c);//还原c
        return false;
    }
};

DLX dlx;

int main()
{
    int n, m;
    while(scanf("%d %d", &n, &m) != EOF)
    {
        dlx.init(n, m);
        for(int i = 1; i <= n; i++)
        {
            int num, j;
            scanf("%d", &num);
            while(num--)
            {
                scanf("%d", &j);
                dlx.Link(i, j);
            }
        }
        if(!dlx.Dance(0)) printf("NO\n");//没有解
        else
        {
            printf("%d", dlx.ansd);
            for(int i = 0; i < dlx.ansd; i++)
                printf(" %d", dlx.ans[i]);
            printf("\n");
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值