Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover
Problem's Link: http://acm.hust.edu.cn/problem/show/1017
Mean:
给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1
analyse:
初学DLX。
这是DLX处理的最简单的问题,也是模板题。
Time complexity: O(n*d)
Source code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXNode = ;
const int MAXN = ;
struct DLX
{
int n,m,size;
int U[MAXNode],D[MAXNode],R[MAXNode],L[MAXNode],Row[MAXNode],Col[MAXNode];
int H[MAXN], S[MAXN]; // H[i]---第i行第一个为1的index S[i]---第i列为1的个数
int ansd, ans[MAXN];
void init(int _n,int _m)
{
n = _n;
m = _m;
for(int i = ;i <= m;i++) // 初始化第一行(图中的C[])
{
S[i] = ; // 第i列为1的个数
U[i] = D[i] = i;
L[i] = i-;
R[i] = i+;
}
R[m] = ; L[] = m; // 第一行的最后一个指向第一行的第一个(成环)
size = m; // 从m开始以后的都是普通结点
for(int i = ;i <= n;i++)
H[i] = -; // H[i]---第i行第一个为1的结点编号
}
void Link(int r,int c) // 行 列
{
// D[c] --- 第c列的下指针
S[Col[++size]=c]++; // 普通结点下标++ 第size个结点的列数是c 第c列的结点个数++
Row[size] = r; // 第size个结点的行数是r
D[size] = D[c]; // 第size个结点的下指针是:第0行第c列的下指针
U[size] = c; // 第size个结点的上指针是:第0行第c列 (只有输入行是递增时才可以这样)
U[D[c]] = size; // 第0行第c列的上指针是:size
D[c] = size; // size上面那个的下指针是:size (有点绕)
if(H[r] < ) H[r] = L[size] = R[size] = size; // 该行只有一个结点 左右指针自己指向自己
else
{
R[size] = R[H[r]]; // 成环
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
void remove(int c) // 删除列c及其所在的行
{
L[R[c]] = L[c]; R[L[c]] = R[c]; // 左右两个结点连接,屏蔽掉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]]; // j所在的列的数目减少
}
}
void resume(int c) //恢复列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;
}
//d为递归深度
bool Dance(int d)
{
if(R[] == ) // R[0]==R[m] // 第0行已经没有结点
{
ansd = d;
return true;
}
int c = R[];
for(int i = R[];i != ;i = R[i]) // 往右走 ( 找出结点数最少的一列)
if(S[i] < S[c]) //第i列结点个数 < 第c列结点个数
c = i;
remove(c); // 移除列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 g;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m) == )
{
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 ;
}
这个博客讲得非常细:
http://www.cnblogs.com/grenet/p/3145800.html
Dancing Link --- 模板题 HUST 1017 - Exact cover的更多相关文章
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- 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 dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...
- [HUST 1017] Exact cover
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- hustoj 1017 - Exact cover dancing link
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...
随机推荐
- MVC使用基架添加控制器出现的错误:无法检索XXX的元数据
环境 vs2012 框架 mvc3 数据库 sqlservercompact4.0 出现的错误如下: “ ---------------------------Microsoft Visual St ...
- Living one day at a time (update for a long time)
1, http://acm.hdu.edu.cn/showproblem.php?pid=1228 2014-04-14 10:39:52 分析:字符串处理题... #include<iost ...
- GoldenGate 配置extract,replicat进程自启动
在GoldenGate中主进程是manager进程,使用start mgr启动.可以在mgr进程中添加一些参数用来在启动mgr进程的同时启动extract和replicat进程 GGSCI (gg01 ...
- ubuntu 12.04 安装Docker 实战
2016-3-8 从网络服务商那里申请到一台Ubuntu测试服务器,用来测试安装Docker环境. 注:本人初学Docker,对Linux命令也仅是稍稍了解,如有错误,烦请告知. 查看系统相关信息 可 ...
- AndroidStudio小技巧--依赖库
同步发表于http://avenwu.net/2015/02/12/androidstudio_library_dependency Fork on github https://github.com ...
- iscroll简单使用说明
iScroll是一个高性能,资源占用少,无依赖,跨平台的javascript上拉加载,下拉刷新的滚动插件,目前版本v5.2.0. GitHub下载地址:https://github.com/cubiq ...
- android 性能分析、优化
.主要介绍了一些分析工具,比如GT.ITest等http://www.jianshu.com/p/8b77d394b2a6 .详细介绍啦android平台常见性能优化工具http://blog.csd ...
- Why restTemplate.put() throws “HttpClientErrorException: 404 Not Found”
I make a put request RestTemplate restTemplate = new RestTemplate(); restTemplate.put(new URI(&quo ...
- SQL语句 - 嵌套查询
嵌套查询的意思是,一个查询语句(select-from-where)查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询.其中外层查询也称为父查询,主查询.内层查询也称子查询,从查询. ...
- MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)
MVC .EF 学习有大半年了,用的还不是很熟练,正好以做这样一个简单的权限管理系统作为学习的切入点,还是非常合适的. 开发环境: VS 2013 + Git + MVC 5 + EF 6 Code ...