搜索(DLX):HOJ 1017 - Exact cover
1017 - Exact cover
Time Limit: 15s Memory Limit: 128MB
- 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的更多相关文章
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- hustoj 1017 - Exact cover dancing link
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...
- HUST 1017 Exact cover (Dancing links)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...
- [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 ...
- (简单) 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 ...
- 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 ...
- [HUST 1017] Exact cover
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
随机推荐
- Python_oldboy_自动化运维之路(一)
python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...
- 从创建webservice到发布webservice的一些相关总结
最近做了一个web服务,开始什么也不懂,就在网上到处找,对于刚毕业的我,感觉没用实际代码经过自己的手写出来,看什么都一头雾水,然后就看到很多人说webservice已经融入WCF..然后就先创建了WC ...
- 使用了hibernate时候乱码问题
在配置文件的url地址最后加上characterEncoding=utf-8
- Android使用 selector 自定义控件背景 (以spinner 为例)
1. 在drawable中设置背景spinner_style.xml 文件 如图: 2. 在 styles.xml 中添加该背景 3. 最后在 spinner 控件添加样式 4.参考 http:// ...
- ResultSetMetaData rsmd = rs.getMetaData()是什么意思?
ResultSetMetaData rsmt=rs.getMetaData(); 得到结果集(rs)的结构,比如字段数.字段名等.使用rs.getMetaData().getTableName(1)) ...
- Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化
图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...
- angular的post提交
用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...
- 递归 与 js 对象的引用
<script> //递归 function test(n) { if (n == 1) { return 1 } console.log(n) return n * test(n - 1 ...
- [开源]jquery-ajax-cache:快速优化页面ajax请求,使用localStorage缓存请求
项目:jquery-ajax-cache 地址:https://github.com/WQTeam/jquery-ajax-cache 最近在项目中用到了本地缓存localStorage做数据 ...