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 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 <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=N*N;
int L[M],R[M],U[M],D[M];
int H[N],C[N],row[M],col[M];
int cnt,n,m,ans[N];
struct DLX{
void Init(int n,int m){
for(int i=;i<=m;i++){
L[i]=i-;R[i]=i+;
C[i]=;U[i]=D[i]=i;
}cnt=L[]=m;R[m]=;
for(int i=;i<=n;i++)H[i]=;
}
void Link(int r,int c){
++cnt;C[c]++;
col[cnt]=c;row[cnt]=r;
U[D[c]]=cnt;U[cnt]=c;
D[cnt]=D[c];D[c]=cnt;
if(H[r]){
R[cnt]=R[H[r]];L[cnt]=H[r];
L[R[H[r]]]=cnt;R[H[r]]=cnt;
}
else H[r]=L[cnt]=R[cnt]=cnt;
}
void Delete(int c){
R[L[c]]=R[c];L[R[c]]=L[c];
for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
--C[col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
}
void Resume(int c){
R[L[c]]=c;L[R[c]]=c;
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++C[col[j]],U[D[j]]=j,D[U[j]]=j;
}
bool Dance(int dep){
if(!R[]){
printf("%d",dep);
for(int i=;i<=dep;i++)
printf(" %d",ans[i]);
puts("");return true;
}int p=;
for(int i=R[];i;i=R[i])
if(!p||C[p]>C[i])p=i;
Delete(p);
for(int i=D[p];i!=p;i=D[i]){
ans[dep+]=row[i];
for(int j=R[i];j!=i;j=R[j])Delete(col[j]);
if(Dance(dep+))return true;
for(int j=L[i];j!=i;j=L[j])Resume(col[j]);
}
Resume(p);
return false;
}
}dlx; int main(){
while(scanf("%d%d",&n,&m)!=EOF){
dlx.Init(n,m);
for(int i=;i<=n;i++){
int k,x;
scanf("%d",&k);
while(k--){
scanf("%d",&x);
dlx.Link(i,x);
}
}if(!dlx.Dance())
printf("NO\n");
}
return ;
}

搜索(DLX):HOJ 1017 - Exact cover的更多相关文章

  1. [DLX] hust 1017 Exact cover

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

  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 Link --- 模板题 HUST 1017 - Exact cover

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

  4. hustoj 1017 - Exact cover dancing link

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

  5. HUST 1017 Exact cover (Dancing links)

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

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

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

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

  9. [HUST 1017] Exact cover

    Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...

随机推荐

  1. oracle的安装与plsql的环境配置

    1,首先得有oracle的安装包和plsql的安装包,安装包地址可见百度云 http://pan.baidu.com/s/1miTqhmg 2.解压下来进入0817账套,找到set.exe文件,双击安 ...

  2. NP-难题

    所谓NP-难题,在给定的一个信息系统中,假设研究对象书目为m,属性书目为n,则要考察的属性集P的一个子集是否为最小子集,要进行n*m*m次的比较.而n个属性可构成2的n次方个子集,这些子集都有可能是最 ...

  3. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  4. 借用Toad 生成表空间的使用量图示

    图示产生方法 图示(tablespace uage)如下

  5. SGU 112.a^b - b^a

    题意: 如标题. 方法: 简单高精度... 代码(继续JAVA 水过) import java.util.*; import java.math.*; public class Solution { ...

  6. 传值 UI考试知识点

    传值: 1. 属性传值:从前往后 2. 代理传值:从后往前 3. block: 4. 单例:普通写法和GCD写法 5 . 通知 NSNotification GCD 单例: static PlayMu ...

  7. sql新增后返回主键

    对于刚学的人来说有点帮助,新增后返回主键有两种方法: 1,返回自增的主键: INSERT INTO 表名 (字段名1,字段名2,字段名3,…) VALUES (值1,值2,值3,…) SELECT @ ...

  8. div+css知识点

    前端书写规范: 1.所有书写均在英文半角状态下的小写: 2.id,class必须以字母开头: 3.所有标签必须闭合: 4.html标签用tab键缩进: 5.属性值必须带引号: 6.<!-- ht ...

  9. OSSchedLock()函数透析

    uC/OS-II的OSSchedLock()和OSSchedUnlock()函数允许应用程序锁定当前任务不被其它任务抢占. 使用时应当注意的是:当你调用了OSSchedLock()之后,而在调用OSS ...

  10. OpenSource.com 评出 2014 年十佳开源软件

    Docker 应用容器平台 “电源管理和虚拟化以相同的方式允许我们从服务器利用率中获取最大的利益.如何真正的解决虚拟化,这世界第一难题仍然是普遍存在的.Docker 自从 2013 年开源以来,刚好在 ...