1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 7270 Solved: 3754

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

HINT

SOURCE

dupeng
精确覆盖问题,dancing links模板
 //2017-03-09
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
const int M = ;
const int maxnode = N*M; struct DLX
{
int n, m, sz;
int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
int H[N], S[M];
int ansd, ans[N]; void init(int nn, int mm)
{
n = nn; m = mm;
for(int i = ; i <= m; i++)
{
S[i] = ;
U[i] = D[i] = i;
L[i] = i-;
R[i] = i+;
}
R[m] = ; L[] = m;
sz = m;
for(int i = ; i <= n; i++)H[i] = -;
} void link(int r, int c)
{
++S[Col[++sz] = c];
Row[sz] = r;
D[sz] = D[c];
U[D[c]] = sz;
U[sz] = c;
D[c] = sz;
if(H[r] < )H[r] = L[sz] = R[sz] = sz;
else{
R[sz] = R[H[r]];
L[R[H[r]]] = sz;
L[sz] = H[r];
R[H[r]] = sz;
}
} 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])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
} bool Dance(int d)
{
if(R[] == )
{
printf("%d ", d);
for(int i = ; i < d; i++)
if(i == d-)printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
return true;
}
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 true;
for(int j = L[i]; j != i; j = L[j])resume(Col[j]);
}
resume(c);
return false;
}
}dlx; int main()
{
int n, m, c, tmp;
while(scanf("%d%d", &n, &m)!=EOF)
{
dlx.init(n, m);
for(int i = ; i <= n; i++)
{
scanf("%d", &c);
for(int j = ; j < c; j++)
{
scanf("%d", &tmp);
dlx.link(i, tmp);
}
}
if(!dlx.Dance())printf("NO\n");
} return ;
}

HUST1017(KB3-A Dancing links)的更多相关文章

  1. HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

    题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...

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

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

  3. 【转】Dancing Links题集

    转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...

  4. 【转】Dancing Links精确覆盖问题

    原文链接:http://sqybi.com/works/dlxcn/ (只转载过来一部分,全文请看原文,感觉讲得很好~)正文    精确覆盖问题    解决精确覆盖问题    舞蹈步骤    效率分析 ...

  5. Dancing Links 专题总结

    算法详细:Dancing Links博客 1.精确覆盖: ZOJ3209 Treasure Map HUST1017 Exact cover POJ3074 Sudoku 2.可重复覆盖: HDU22 ...

  6. dancing links 题集转自夏天的风

    POJ3740     Easy Finding [精确覆盖基础题] HUST1017    Exact cover [精确覆盖基础] HDOJ3663 Power Stations [精确覆盖] Z ...

  7. Dancing Links and Exact Cover

    1. Exact Cover Problem DLX是用来解决精确覆盖问题行之有效的算法. 在讲解DLX之前,我们先了解一下什么是精确覆盖问题(Exact Cover Problem)? 1.1 Po ...

  8. 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题

    精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 例如:如下的矩阵 就包含了这样一个集合(第1.4.5行) 如何利用给定的矩阵求出相应的行的集合 ...

  9. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of ...

随机推荐

  1. 11_python_闭包迭代器

    一.函数名(第一类对象) 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数的变量. def func(): print("呵呵") print(func) 结果: & ...

  2. VS2013 生成事件,删除不必要的DLL

    解决方案中有一个 Project 是 Windows Service,用来从消息队列中取出事件,发送通知电邮: UI是一个MVC网站,两个Project都引用了同一个类库,这个类库引用了第三方的生成P ...

  3. SQL Server清理大日志文件方法 不分离数据库 执行SQL语句即可

    SQL 2008清空日志的SQL语句如下: USE[master] GO ALTER DATABASE 要清理的数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO AL ...

  4. JAVA实现QRCode的二维码生成以及打印

    喜欢的朋友可以关注下,粉丝也缺. 不说废话了直接上代码 注意使用QRCode是需要zxing的核心jar包,这里给大家提供下载地址 https://download.csdn.net/download ...

  5. B2C电商项目

    经历四个月的自学. 结合所学的知识(HTML,CSS,javascript,jQuery,Mysql,Redis,Django,celery,fastDfs,haystack,whoosh,uWSGI ...

  6. MyBatis框架介绍及其实操

    一.基本概念和介绍 数据持久化的概念 数据持久化是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存中的数据模型的统称.例如,文件的存储.数据的读取等都是数据持久化操作.数据模型可以是任何数据 ...

  7. 安装SVN并进行汉化的详细步骤

    安装SVN并进行汉化的详细步骤 SAE提供了不同的代码部署方式,可以分为两类:一是通过SVN客户端部署,这是SAE推荐的代码部署方法.另一个是通过非SVN客户端部署,即在线代码在线编辑器和推荐应用安装 ...

  8. Centos 7 快速搭建IOS可用IPsec

    安装 strongswan yum install -y http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/7/x86_64/Packages/e/ ...

  9. DDD漫想

    领域专用语言 领域驱动设计(Domain Driver Design)开发中,最令我震撼的是领域专用语言(Domain specific language),领域专用语言专注于描述当前领域内的业务细节 ...

  10. Nutch抓取流程

    nutch抓取流程注入起始url(inject).生成爬取列表(generate).爬取(fetch).解析网页内容(parse).更新url数据库(updatedb)1:注入起始url(inject ...