给出一个\(n\times m\)的01矩阵,每行最多有\(c\)个1,求一个精确覆盖,即选出一些行使得每列有有且仅有一个1。输出方案。

分析

被这个题坑到了啊!!第一次上HUSTOJ做题,不知道没有ONLINE_JUDGE编译参数,又WA了几个小时。

我感觉这个spj是有问题的,只处理了顺序不同的问题,而没有处理方案不同,所以其实有些对的代码过不了。

我最开始看完这篇写的超级棒,主要是配了图的教程,然后自己乱写一个,发现很好写,才45行左右,然后就WA了。最终知道了是ONLINE_JUDGE的问题,但是,把之前的代码去掉文件读写还是会错!

WA的过程中,我去网上搜了很多题解。我要批判一下那些人啊,全都拿别人的代码来当模版……哎。

我在他们的代码中发现了一个优化。原来的教程中说的是找到Head右边的第一个开始搜索。其实搜索的顺序是无关的,因为这是基于所有的列都会被覆盖到,所以顺序无关。所以我们可以找其中列标下面剩余1的个数最少的那一列来找,可以大大剪枝。所以每一列维护一下size即可。

还有一个要注意的地方,我们在删除列和回退的时候,方向是不一样的。比如说,删除列我们一开始向下走走一圈,那么回退的时候我们向上走,估计是为了避免一些冲突。同样在便利我们要删除的行的时候,从左到右,回退从右到左。

代码

网上那些“模版”写得那么奇怪还一堆人拿来抄。这个多漂亮~

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int read() {
  7. int x=0,f=1;
  8. char c=getchar();
  9. for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
  10. for (;isdigit(c);c=getchar()) x=x*10+c-'0';
  11. return x*f;
  12. }
  13. const int maxn=1e3+10;
  14. const int maxc=1e2+10;
  15. const int maxp=maxn*maxc;
  16. int a[maxc],ans[maxn];
  17. struct node {
  18. int l,r,u,d,col,row;
  19. };
  20. struct DLX {
  21. node p[maxp];
  22. int tot,last[maxn],size[maxn];
  23. void clear(int m) {
  24. tot=m;
  25. memset(last,0,sizeof last);
  26. memset(size,0,sizeof size);
  27. memset(p,0,sizeof p);
  28. p[0]=(node){0,0,0,0,0,0};
  29. for (int i=1;i<=m;++i) {
  30. last[i]=i;
  31. p[i]=(node){i-1,p[i-1].r,i,i,i,0};
  32. p[p[i].l].r=i,p[p[i].r].l=i;
  33. }
  34. }
  35. void build(int row,int a[],int len) {
  36. p[++tot]=(node){tot,tot,last[a[1]],p[last[a[1]]].d,a[1],row};
  37. p[p[tot].u].d=p[p[tot].d].u=last[a[1]]=tot;
  38. ++size[p[tot].col];
  39. for (int i=2;i<=len;++i) {
  40. int x=a[i];
  41. p[++tot]=(node){tot-1,p[tot-1].r,last[x],p[last[x]].d,x,row};
  42. p[p[tot].l].r=p[p[tot].r].l=p[p[tot].u].d=p[p[tot].d].u=last[x]=tot;
  43. ++size[p[tot].col];
  44. }
  45. }
  46. void del(int c) {
  47. p[p[c].l].r=p[c].r,p[p[c].r].l=p[c].l;
  48. for (int i=p[c].d;i!=c;i=p[i].d) for (int j=p[i].r;j!=i;j=p[j].r) p[p[j].u].d=p[j].d,p[p[j].d].u=p[j].u,--size[p[j].col];
  49. }
  50. void back(int c) {
  51. p[p[c].l].r=p[p[c].r].l=c;
  52. for (int i=p[c].u;i!=c;i=p[i].u) for (int j=p[i].r;j!=i;j=p[j].r) p[p[j].u].d=p[p[j].d].u=j,++size[p[j].col];
  53. }
  54. int dance(int k) {
  55. if (p[0].r==0) return k;
  56. int first,mi=maxp;
  57. for (int i=p[0].r;i;i=p[i].r) if (size[i]<mi) mi=size[i],first=i; //here
  58. if (p[first].d==first) return 0;
  59. del(first);
  60. for (int i=p[first].d;i!=first;i=p[i].d) {
  61. for (int j=p[i].r;j!=i;j=p[j].r) del(p[j].col);
  62. ans[k+1]=p[i].row;
  63. int ret=dance(k+1);
  64. if (ret) return ret;
  65. ans[k+1]=0;
  66. for (int j=p[i].l;j!=i;j=p[j].l) back(p[j].col);
  67. }
  68. back(first);
  69. return 0;
  70. }
  71. } dlx;
  72. int main() {
  73. int n,m;
  74. while (~scanf("%d%d",&n,&m)) {
  75. dlx.clear(m);
  76. for (int i=1;i<=n;++i) {
  77. int c=read();
  78. if (!c) continue;
  79. for (int j=1;j<=c;++j) a[j]=read();
  80. sort(a+1,a+c+1);
  81. dlx.build(i,a,c);
  82. }
  83. int gs=dlx.dance(0);
  84. if (!gs) {
  85. puts("NO");
  86. continue;
  87. }
  88. printf("%d ",gs);
  89. sort(ans+1,ans+gs+1);
  90. for (int i=1;i<=gs;++i) printf("%d ",ans[i]);
  91. puts("");
  92. }
  93. return 0;
  94. }

HUST1017-Exact Cover的更多相关文章

  1. HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

    题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...

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

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

  3. Dancing Links and Exact Cover

    1. Exact Cover Problem DLX是用来解决精确覆盖问题行之有效的算法. 在讲解DLX之前,我们先了解一下什么是精确覆盖问题(Exact Cover Problem)? 1.1 Po ...

  4. Dancing Link --- 模板题 HUST 1017 - Exact cover

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  5. HUST 1017 Exact cover (Dancing links)

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

  6. [HUST 1017] Exact cover

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

  7. hustoj 1017 - Exact cover dancing link

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

  8. 搜索(DLX):HOJ 1017 - Exact cover

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

  9. [HDU1017]Exact cover[DLX][Dancing Links详解][注释例程学习法]

    Dancing Links解决Exact Cover问题. 用到了循环双向十字链表. dfs. 论文一知半解地看了一遍,搜出一篇AC的源码,用注释的方法帮助理解. HIT ACM 感谢源码po主.链接 ...

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

随机推荐

  1. Java 中的正则(Pattern)

    /**String 中 replaceAll(),matches(),split() 等方法,都是调用Pattern中的方法.学习了,瞬间觉得Pattern强大了 public String repl ...

  2. 前端--javaScript之BOM和DOM

    BOM和DOM概述 BOM(Browser Object Model):是指浏览器对象模型,它使js有能力和浏览器进行"对话". DOM(Document Object Model ...

  3. python编码和小数据池

    python_day_6 一. 回顾上周所有内容一. python基础 Python是一门解释型. 弱类型语言 print("内容", "内容", end=&q ...

  4. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  5. 软银集团和共享办公空间公司WeWork在日本成立合资公司

    [TechWeb报道]7月18日消息,据国外媒体报道,软银集团和共享办公空间公司WeWork联合宣布,在日本成立合资公司WeWork Japan. 该合资公司将在日本开设联合办公空间,于明年初在东京设 ...

  6. hbase 预分区

    转载 http://www.cnblogs.com/bdifn/p/3801737.html

  7. app开发相关

    app播放UIWebview 没有声音解决: 设置 allowsInlineMediaPlayback  = YES; mediaPlaybackRequiresUserAction = NO

  8. USACO 1.3.2 Barn Repair 修理牛棚(贪心)

    Description 在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有牛,有些没 ...

  9. Java中的抽象类abstract

    abstract定义抽象类 abstract定义抽象方法,只需要声明,不需要实现 包含抽象方法的类是抽象类 抽象类中可以包含抽象方法,也可以包含普通方法 抽象类不能直接创建,可以定义父类引用变量指向子 ...

  10. 团队Alpha冲刺(四)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:何家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员:何宇恒 展示组内最新 ...