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 ...
随机推荐
- netfilter分析
转自:http://blog.sina.com.cn/s/blog_a31ff26901013n07.html 一.概述 1. Netfilter/IPTables框架简介 Netfilter/IPT ...
- quick -- 添加按钮
cc.ui.UIPushButton.new({ normal = "comm_btnGreenBackBack.png", pressed = "comm_btnGre ...
- Scala 深入浅出实战经典 第43讲:主要介绍类型变量bound
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- [LeetCode] Find Median from Data Stream
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
- JS区别不同浏览器(微信、手机等)
最近一直在忙于自己公司的旅游产品,设计方面太广并且要兼容各种设备和场景,包括PC.Mobile.Pad.还有各种支付.由于微信支付和支付宝存在竞争,所以需要区别不同的浏览器,并且WEB项目还要出现在A ...
- 初探单点登录 SSO
单点登录 单点登录(Single sign-on,SSO)是一种访问控制,在多个软件应用中,用户只需登录其中一个应用,就可以成功访问其他应用:同样,用户只需注销其中一个应用,就可以成功注销其他应用. ...
- .NET Core:面向未来的开源跨平台开发技术
作为一种全新的开源和跨平台的开发平台,.NET Core 历经两年多的开发,终于在于2016年6月27日针对所有主流服务器和桌面操作系统发布 1.0 RTM 版本..NET Core 是一种通用开发平 ...
- 解决mac os x下 tomcat启动报 java.net.BindException: Permission denied <null>:80 错误
我在mac os x上启动tomcat的时候,报 java.net.BindException: Permission denied <null>:80,java.net.BindExce ...
- java-cef系列视频第三集:添加flash支持
上一集我们介绍了如何搭建java-cef调试环境. 本视频介绍如何给java-cef客户端添加flashplayer支持 第四集视频我们将介绍java-cef中的自定义协议. 本作品采用知识共享署名- ...
- nc
http://www.oschina.net/translate/nc-command-examples http://nixdoc.net/man-pages/openbsd/man1/nc.1.h ...