[DLX] hust 1017 Exact cover

题意:

给你N个包,要拿到M个东西(编号1~M每个只能有一个)

然后每个包里有k个东西,每个东西都有编号。

思路:

舞蹈连模板题

代码:

#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"vector"
#include"string"
using namespace std;
#define N 1010*100   //总共元素个数
#define RN 1010      //总共行数
#define CN 1010      //总共列数
struct DLX
{
    int n,m,C;                              //n,m代表范围行列数,C代表元素个数
    int U[N],D[N],L[N],R[N],Row[N],Col[N];  //U、D、L、R代表第C个的上下左右分别是哪个  Row和Col代表第C的行号和列号
    int H[RN],S[CN],cnt,ans[RN];            //H为每行开头是哪个,H[i]==-1代表本行没有元素 S代表每列有多少个元素 cnt为结果是选了几行 ans存结果是哪几行
    void init(int _n,int _m)                //初始化 创建0~m个原始元素(第0行)
    {
        n=_n;
        m=_m;
        for(int i=0; i<=m; i++)
        {
            U[i]=D[i]=i;
            L[i]=(i==0?m:i-1);
            R[i]=(i==m?0:i+1);
            S[i]=0;
        }
        C=m;
        for(int i=1; i<=n; i++) H[i]=-1;
    }
    void link(int x,int y)                  //连接行数,就是把元素插入
    {
        C++;
        Row[C]=x;
        Col[C]=y;
        S[y]++;
        U[C]=U[y];
        D[C]=y;
        D[U[y]]=C;
        U[y]=C;
        if(H[x]==-1) H[x]=L[C]=R[C]=C;
        else
        {
            L[C]=L[H[x]];
            R[C]=H[x];
            R[L[H[x]]]=C;
            L[H[x]]=C;
        }
    }
    void del(int x)                        //删除函数,删除对应列
    {
        R[L[x]]=R[x];
        L[R[x]]=L[x];
        for(int i=D[x]; i!=x; 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 rec(int x)                        //恢复函数,恢复对应列
    {
        for(int i=U[x]; i!=x; i=U[i])
        {
            for(int j=L[i]; j!=i; j=L[j])
            {
                U[D[j]]=j;
                D[U[j]]=j;
                S[Col[j]]++;
            }
        }
        R[L[x]]=x;
        L[R[x]]=x;
    }
    int dance(int x)                       //舞蹈连搜索函数
    {
        if(R[0]==0)                        //满足条件,默认是R[0]=0也就是第0行没有元素了,但是因题目而定
        {
            cnt=x;
            return 1;
        }
        int now=R[0];
        for(int i=R[0]; i!=0; i=R[i])      //选择含有元素最多的列,可加速
        {
            if(S[i]<S[now]) now=i;
        }
        del(now);
        for(int i=D[now]; i!=now; i=D[i])
        {
            ans[x]=Row[i];
            for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
            if(dance(x+1)) return 1;
            for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
        }
        rec(now);
        return 0;
    }
} dlx;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        dlx.init(n,m);
        for(int i=1;i<=n;i++)
        {
            int k;
            scanf("%d",&k);
            while(k--)
            {
                int x;
                scanf("%d",&x);
                dlx.link(i,x);
            }
        }
        int f=dlx.dance(0);
        if(f==0) puts("NO");
        else
        {
            printf("%d",dlx.cnt);
            for(int i=0; i<dlx.cnt; i++) printf(" %d",dlx.ans[i]);
            puts("");
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 11:59:27

[DLX] hust 1017 Exact cover的相关文章

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 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 o

Dancing Link --- 模板题 HUST 1017 - Exact cover

1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 略 analyse: A Time complexity: O(n) Source code:  #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vecto

HUST 1017 Exact cover (Dancing links)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 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 o

(简单) HUST 1017 Exact cover , DLX+精确覆盖。

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. DLX精确覆盖的模板题...... 代码如下: //HUST 1

HUST 1017 Exact cover DLX

题意:一个n×m行的矩阵,每个格子可能是 0 或者1  现在让你选择 几列  使得 每一列 有且只有一个1.(精确覆盖模板题) 解题思路: dancing links 模板 关于dancing links  献上几篇必看论文 :http://par.buaa.edu.cn/acm-icpc/filepool/r/35/ 板子用的kuangbin的板子(还是适牛的..) 解题思路: 1 // File Name: hust1017.cpp 2 // Author: darkdream 3 // C

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 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 e

hustoj 1017 - Exact cover dancing link

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 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 e

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

hust 1017 dancing links 精确覆盖模板题

最基础的dancing links的精确覆盖题目 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 #define N 1005 8 #define MAXN 1000100 9 10 struct DLX{ 11 int n , m , size;//size表示当前dlx表中有多少