搜索(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 ...
随机推荐
- HDU 5592 ZYB's Premutation(树状数组+二分)
题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字 ...
- win 10 安装 mysql解压版 步骤
参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...
- mssql 创建触发器
MS-SMS里创建触发器: 首先右击表内的触发器文件夹图标 然后输入触发器创建指令,一下案例:(添加创建时间) 创建: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFI ...
- codevs 3119 高精度练习之大整数开根 (各种高精+压位)
/* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...
- gulp完成javascript压缩合并,css压缩
最近需要对项目进行优化,主要是对js的压缩合并和css文件的压缩,查找相关资料之后发现gulp可以实现相关的功能,特此分享一下使用心得. 1.安装gulp gulp是基于Node.js的前端构建工具. ...
- fiddler了解
常常听到有人会所抓包什么的,自己电脑上有一段fiddler软件,但是一直没有使用,因为不了解.今天终于看视频,看博客,大致了解了fiddler这个软件,看着是非常强大啊.那么fiddler到底是什么, ...
- DNS服务器的原理
当用户去访问一个网站(百度)的时候,首先去请求dns服务器,根据对应的域名返回所在ip,然后再使用ip去访问自己所在服务器空间.简单的说,DNS服务器就像114客服,dns服务器是树状结构的,dns服 ...
- 利用html+ashx实现aspx的功能
最近准备学习下ASP.NET,初期在网上看了些视频教程,准备将自己学习的东西整理整理,留着日后可以参考参考. 本文采用了html.ashx实现aspx,实现了一个最简单的动态网页效果,开发环境是VS2 ...
- 内联式css样式,直接写在现有的HTML标签中
CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式. 内联式css样式表就是把css代码直接写在现有的HTML标签中 ...
- ZendStudio快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当前 ...