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 ...
随机推荐
- GPL与LGPL的区别
GPL(GNU General Public License) 我们很熟悉的Linux就是采用了GPL.GPL协议和BSD, Apache Licence等鼓励代码重用的许可很不一样.GPL的出发点 ...
- iOS开发 - AVPlayer实现流音频边播边存
边播边下有三套左右实现思路,本文使用AVPlayer + AVURLAsset实现. 概述 1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为 ...
- 日常工作中的点滴总结from 2014-03
一 关于 写方案: 写某个产品的方案基本应包括以下几点: 1产品目前现状(国内外) 2产品意义.作用 3产品架构 4产品优势 5产品功能讲解 二 关于 处理下属工作方向不正确的事务 首先 先肯定 下 ...
- 【书单】book list
正在看: [泡沫经济学].(日)野口悠纪雄 数学模型--姜启源 R in action Programming with R Scrapy Parallel R 准备看: Advanced.A ...
- is running beyond physical memory limits. Current usage: 2.0 GB of 2 GB physical memory used; 2.6 GB of 40 GB virtual memory used
昨天使用hadoop跑五一的数据,发现报错: Container [pid=,containerID=container_1453101066555_4130018_01_000067] GB phy ...
- 使用Aspose.Cells 根据模板生成excel里面的 line chart
目的: 1.根据模板里面的excel数据信息,动态创建line chart 2.linechart 的样式改为灰色 3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端 4.使用 ...
- nginx server_name
在我的机子了nginx的 server_name要配制成127.0.0.1才能用,否则就报错,刚试用nginx还不知道为什么,先记下来
- dlib库使用
最近的工作中用到了dlib这个库,该库是一个机器学习的开源库,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).不过由于是开源的,所以bug多少有一些,我在example ...
- Nginx模块开发时unknown directive "echo"的处理
实际上,Nginx并没有echo这个指令,所以你贸然使用时,自然会提示说无法识别的指令,处理方法有两个: 方法一是: 从下面连接下载echo-nginx-module模块并安装: https://gi ...
- visual studio snippets风格
snippet挺好用,但是不是我喜欢的那种风格,比如if是这样的 if () { XX } 而我比较习惯这种: if () { XX } 可以这么做: 工具(Tools)——代码段管理器(Code S ...