Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 6012 Solved: 3185
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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MaxNode 100010
#define MaxN 1010
#define MaxM 1010 struct DLX
{
int n,m,size;
int U[MaxNode],D[MaxNode],R[MaxNode],L[MaxNode];
int Row[MaxNode],Col[MaxNode];
int H[MaxN],S[MaxM];
int ansd, ans[MaxN]; void Init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=;i<=m;i++)
{
S[i]=;
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
}
R[m]=;L[]=m;
size=m;
for(int i=;i<=n;i++)
H[i]=-;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
U[size]=U[c];
D[U[c]]=size;
D[size]=c;
U[c]=size;
if(H[r]==-) 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;
}
}
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];
S[Col[j]]--;
}
}
}
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;
S[Col[j]]++;
}
}
L[R[c]] =c;
R[L[c]] =c;
}
bool Dance(int d)
{
if(R[]==)
{
ansd=d;
return ;
}
int c=R[];
for(int i=R[];i!=;i=R[i])
if(S[i]<S[c]) c=i;
Remove(c);
for(int i=D[c];i!=c;i=D[i])
{
ans[d]=Row[i];
for(int j=R[i];j!=i;j=R[j]) Remove(Col[j]); //移除
if(Dance(d+)) return ;
for(int j=L[i];j!=i;j=L[j]) Resume(Col[j]); //回标
}
Resume(c);
return ;
}
}g;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
g.Init(n,m);
for(int i=;i<=n;i++)
{
int num,j;
scanf("%d",&num);
while(num--)
{
scanf("%d",&j);
g.Link(i,j);
}
}
if(!g.Dance()) printf("NO\n");
else
{
printf("%d",g.ansd);
for(int i=;i<g.ansd;i++)
printf(" %d",g.ans[i]);
printf("\n");
}
}
return ;
}

[HUST 1017] Exact cover的更多相关文章

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

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...

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

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  3. HUST 1017 Exact cover (Dancing links)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...

  4. [ACM] HUST 1017 Exact cover (Dancing Links,DLX模板题)

    DESCRIPTION There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...

  5. (简单) 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 ...

  6. 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 ...

  7. HUST 1017 Exact cover dance links

    学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...

  8. [DLX] hust 1017 Exact cover

    题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...

  9. hustoj 1017 - Exact cover dancing link

    1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...

随机推荐

  1. 13_CXF和Spring整合发布服务

    [服务端] 第一步:建立一个Web项目 第二步:填充CXF jar包 第三步:创建接口及服务类 [工程截图(对比之前的WebService_CXF_Server00)] [applicationCon ...

  2. 获取input标签的所有属性

    1.用jquery$("input[name='btnAdd']").attr("value") 获取value属性值,其它属性换attr的参数就OK 例1: ...

  3. CentOS 最小化安装后安装桌面

    通过yum的方式安装: yum groupinstall -y   "Desktop"   "Desktop Platform"   "Desktop ...

  4. 002.TPerlRegEx简单测试

    我要做什么? 将一个字符串中的所有连续的数字替换成一个* 代码: program Project1; {$APPTYPE CONSOLE} uses System.SysUtils, PerlRegE ...

  5. Kinetic使用注意点--canvas

    <virtual> new Canvas(width, height) 参数: width:canvas宽度 height:canvas高度 方法: applyShadow(shape, ...

  6. UNICODE编码表

    UNICODE简介 Unicode(统一码.万国码.单一码)是一种在计算机上使用的字符编码.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进 ...

  7. Unity3d Shader开发(三)Pass(Culling & Depth Testing)

    剔除是一种通过避免渲染背对观察者的几何体面来提高性能的优化措施.所有几何体都包含正面和反面.剔除基于大多数对象都是封闭的事实:如果你有一个立方体,你不会看到背离你的那一面(总是只有一面在你的前方),因 ...

  8. CODEVS 1004四子连棋

    [题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...

  9. 数据库应该使用异步吗 Should my database calls be Asynchronous?

    Should my database calls be Asynchronous? http://blogs.msdn.com/b/rickandy/archive/2009/11/14/should ...

  10. 个人作业-Homework1感想

    我以前没有系统学习过C++和C#,编程能力比较差.这次个人作业对我来说是一个很大的挑战.由于布置作业的时间是开学的第一周,因为还没有从假期的状态中转换出来,这对我写作业又增加了一定的难度. 在开始写作 ...