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: 

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. using namespace std;
  14. const int MAXNode = ;
  15. const int MAXN = ;
  16. struct DLX
  17. {
  18. int n,m,size;
  19. int U[MAXNode],D[MAXNode],R[MAXNode],L[MAXNode],Row[MAXNode],Col[MAXNode];
  20. int H[MAXN], S[MAXN]; // H[i]---第i行第一个为1的index S[i]---第i列为1的个数
  21. int ansd, ans[MAXN];
  22. void init(int _n,int _m)
  23. {
  24. n = _n;
  25. m = _m;
  26. for(int i = ;i <= m;i++) // 初始化第一行(图中的C[])
  27. {
  28. S[i] = ; // 第i列为1的个数
  29. U[i] = D[i] = i;
  30. L[i] = i-;
  31. R[i] = i+;
  32. }
  33. R[m] = ; L[] = m; // 第一行的最后一个指向第一行的第一个(成环)
  34. size = m; // 从m开始以后的都是普通结点
  35. for(int i = ;i <= n;i++)
  36. H[i] = -; // H[i]---第i行第一个为1的结点编号
  37. }
  38. void Link(int r,int c) // 行 列
  39. {
  40. // D[c] --- 第c列的下指针
  41. S[Col[++size]=c]++; // 普通结点下标++ 第size个结点的列数是c 第c列的结点个数++
  42. Row[size] = r; // 第size个结点的行数是r
  43. D[size] = D[c]; // 第size个结点的下指针是:第0行第c列的下指针
  44. U[size] = c; // 第size个结点的上指针是:第0行第c列 (只有输入行是递增时才可以这样)
  45. U[D[c]] = size; // 第0行第c列的上指针是:size
  46. D[c] = size; // size上面那个的下指针是:size (有点绕)
  47. if(H[r] < ) H[r] = L[size] = R[size] = size; // 该行只有一个结点 左右指针自己指向自己
  48. else
  49. {
  50. R[size] = R[H[r]]; // 成环
  51. L[R[H[r]]] = size;
  52. L[size] = H[r];
  53. R[H[r]] = size;
  54. }
  55. }
  56. void remove(int c) // 删除列c及其所在的行
  57. {
  58. L[R[c]] = L[c]; R[L[c]] = R[c]; // 左右两个结点连接,屏蔽掉c结点
  59. for(int i = D[c];i != c;i = D[i]) // 屏蔽掉所在的列
  60. for(int j = R[i];j != i;j = R[j])
  61. {
  62. U[D[j]] = U[j];
  63. D[U[j]] = D[j];
  64. --S[Col[j]]; // j所在的列的数目减少
  65. }
  66. }
  67. void resume(int c) //恢复列c缩对应的行
  68. {
  69. for(int i = U[c];i != c;i = U[i])
  70. for(int j = L[i];j != i;j = L[j])
  71. ++S[Col[U[D[j]]=D[U[j]]=j]];
  72. L[R[c]] = R[L[c]] = c;
  73. }
  74. //d为递归深度
  75. bool Dance(int d)
  76. {
  77. if(R[] == ) // R[0]==R[m] // 第0行已经没有结点
  78. {
  79. ansd = d;
  80. return true;
  81. }
  82. int c = R[];
  83. for(int i = R[];i != ;i = R[i]) // 往右走 ( 找出结点数最少的一列)
  84. if(S[i] < S[c]) //第i列结点个数 < 第c列结点个数
  85. c = i;
  86. remove(c); // 移除列c所对应的行
  87. for(int i = D[c];i != c;i = D[i]) // 找到最小的这一列往下走
  88. {
  89. ans[d] = Row[i];
  90. for(int j = R[i]; j != i;j = R[j]) remove(Col[j]); // 移除该行所对应的列
  91. if(Dance(d+))return true;//递归下一层
  92. for(int j = L[i]; j != i;j = L[j])resume(Col[j]);//倒着恢复
  93. }
  94. resume(c);
  95. return false;
  96. }
  97. };
  98.  
  99. DLX g;
  100. int main()
  101. {
  102. //freopen("in.txt","r",stdin);
  103. //freopen("out.txt","w",stdout);
  104. int n,m;
  105. while(scanf("%d%d",&n,&m) == )
  106. {
  107. g.init(n,m);
  108. for(int i = ;i <= n;i++) // 行
  109. {
  110. int num,j;
  111. scanf("%d",&num);
  112. while(num--)
  113. {
  114. scanf("%d",&j); // 列
  115. g.Link(i,j);
  116. }
  117. }
  118. if(!g.Dance()) printf("NO\n");
  119. else
  120. {
  121. printf("%d",g.ansd);
  122. for(int i = ;i < g.ansd;i++)
  123. printf(" %d",g.ans[i]);
  124. printf("\n");
  125. }
  126. }
  127. return ;
  128. }

这个博客讲得非常细:

http://www.cnblogs.com/grenet/p/3145800.html

Dancing Link --- 模板题 HUST 1017 - Exact cover的更多相关文章

  1. HUST 1017 - Exact cover (Dancing Links 模板题)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...

  2. HUST 1017 Exact cover (Dancing links)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...

  3. [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 ...

  4. (简单) 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 ...

  5. 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 ...

  6. HUST 1017 Exact cover dance links

    学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...

  7. [HUST 1017] Exact cover

    Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...

  8. [DLX] hust 1017 Exact cover

    题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...

  9. hustoj 1017 - Exact cover dancing link

    1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...

随机推荐

  1. GPL与LGPL的区别

    GPL(GNU General Public License)  我们很熟悉的Linux就是采用了GPL.GPL协议和BSD, Apache Licence等鼓励代码重用的许可很不一样.GPL的出发点 ...

  2. iOS开发 - AVPlayer实现流音频边播边存

    边播边下有三套左右实现思路,本文使用AVPlayer + AVURLAsset实现. 概述 1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为 ...

  3. 日常工作中的点滴总结from 2014-03

    一 关于 写方案: 写某个产品的方案基本应包括以下几点: 1产品目前现状(国内外)  2产品意义.作用 3产品架构 4产品优势 5产品功能讲解 二 关于 处理下属工作方向不正确的事务 首先 先肯定 下 ...

  4. 【书单】book list

    正在看: [泡沫经济学].(日)野口悠纪雄 数学模型--姜启源 R in action Programming with R Scrapy Parallel R     准备看: Advanced.A ...

  5. 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 ...

  6. 使用Aspose.Cells 根据模板生成excel里面的 line chart

    目的: 1.根据模板里面的excel数据信息,动态创建line chart 2.linechart 的样式改为灰色 3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端 4.使用 ...

  7. nginx server_name

    在我的机子了nginx的  server_name要配制成127.0.0.1才能用,否则就报错,刚试用nginx还不知道为什么,先记下来

  8. dlib库使用

    最近的工作中用到了dlib这个库,该库是一个机器学习的开源库,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).不过由于是开源的,所以bug多少有一些,我在example ...

  9. Nginx模块开发时unknown directive "echo"的处理

    实际上,Nginx并没有echo这个指令,所以你贸然使用时,自然会提示说无法识别的指令,处理方法有两个: 方法一是: 从下面连接下载echo-nginx-module模块并安装: https://gi ...

  10. visual studio snippets风格

    snippet挺好用,但是不是我喜欢的那种风格,比如if是这样的 if () { XX } 而我比较习惯这种: if () { XX } 可以这么做: 工具(Tools)——代码段管理器(Code S ...