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

分析

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

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

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

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

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

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

代码

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

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=1e3+10;
const int maxc=1e2+10;
const int maxp=maxn*maxc;
int a[maxc],ans[maxn];
struct node {
int l,r,u,d,col,row;
};
struct DLX {
node p[maxp];
int tot,last[maxn],size[maxn];
void clear(int m) {
tot=m;
memset(last,0,sizeof last);
memset(size,0,sizeof size);
memset(p,0,sizeof p);
p[0]=(node){0,0,0,0,0,0};
for (int i=1;i<=m;++i) {
last[i]=i;
p[i]=(node){i-1,p[i-1].r,i,i,i,0};
p[p[i].l].r=i,p[p[i].r].l=i;
}
}
void build(int row,int a[],int len) {
p[++tot]=(node){tot,tot,last[a[1]],p[last[a[1]]].d,a[1],row};
p[p[tot].u].d=p[p[tot].d].u=last[a[1]]=tot;
++size[p[tot].col];
for (int i=2;i<=len;++i) {
int x=a[i];
p[++tot]=(node){tot-1,p[tot-1].r,last[x],p[last[x]].d,x,row};
p[p[tot].l].r=p[p[tot].r].l=p[p[tot].u].d=p[p[tot].d].u=last[x]=tot;
++size[p[tot].col];
}
}
void del(int c) {
p[p[c].l].r=p[c].r,p[p[c].r].l=p[c].l;
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];
}
void back(int c) {
p[p[c].l].r=p[p[c].r].l=c;
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];
}
int dance(int k) {
if (p[0].r==0) return k;
int first,mi=maxp;
for (int i=p[0].r;i;i=p[i].r) if (size[i]<mi) mi=size[i],first=i; //here
if (p[first].d==first) return 0;
del(first);
for (int i=p[first].d;i!=first;i=p[i].d) {
for (int j=p[i].r;j!=i;j=p[j].r) del(p[j].col);
ans[k+1]=p[i].row;
int ret=dance(k+1);
if (ret) return ret;
ans[k+1]=0;
for (int j=p[i].l;j!=i;j=p[j].l) back(p[j].col);
}
back(first);
return 0;
}
} dlx;
int main() {
int n,m;
while (~scanf("%d%d",&n,&m)) {
dlx.clear(m);
for (int i=1;i<=n;++i) {
int c=read();
if (!c) continue;
for (int j=1;j<=c;++j) a[j]=read();
sort(a+1,a+c+1);
dlx.build(i,a,c);
}
int gs=dlx.dance(0);
if (!gs) {
puts("NO");
continue;
}
printf("%d ",gs);
sort(ans+1,ans+gs+1);
for (int i=1;i<=gs;++i) printf("%d ",ans[i]);
puts("");
}
return 0;
}

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. WPF设置ListBoxItem失去焦点时的背景色

    <!--全局ListBoxItem--> <Style TargetType="ListBoxItem"> <Style.Resources> ...

  2. Object重写equals()、hashcode()方法的原因

    一.问题 在我们新建java对象的时候,如果后期用到对象比较,就必须重写equals(0.hashcode()方法 为什么必须重写这两个方法? 只是比较相等的话,重写equals()方法不就可以吗?为 ...

  3. 使用 AFNetworking做过断点续传吗?

    断点续传的主要思路: 检查服务器文件信息 检查本地文件 如果比服务器文件小, 断点续传, 利用 HTTP 请求头的 content-range实现断点续传(如果content-range不存在就取Co ...

  4. python-前方高能-面向对象-进阶3

    面向对象 你写代码的时候 什么时候用面向对象 代码量大,功能多的时候 处理比较复杂的角色之间的关系 qq 好友 陌生人 群 组 复杂的电商程序 公司/学校的人事管理/功能的系统 我的代码的清晰度更高了 ...

  5. hexo部署

    title: hexo 部署(一) date: 2018-09-16 18:01:26 tags: hexo部署配置 categories: 博客搭建 hexo博客搭建 折腾了好久的时间,终于使用he ...

  6. tomcat 部署项目到服务器

    参考博客,我选了一种最简单的方法来部署项目. 在tomcat 目录下 的  conf\Catalina\localhost 目录中,新建一个   ' 项目名.xml '   文件,名字用项目名表示, ...

  7. url乱码问题

    //url乱码,有时候要解码2次才能成功 String url=URLDecoder.decode(URLDecoder.decode(returnUrl, "UTF-8"),&q ...

  8. AsciiPic Java视频转成字符画

    AsciiPic Java视频转成字符画 github下载 https://github.com/dejavudwh/AsciiPic 运行截图 //没有做GUI 比较简陋 节省时间 main里的文件 ...

  9. js判断PC端 移动端 并跳转到对应页面

    一.PC端跳转到移动端 html页面: <script>var webroot="/",catid="{$catid}",murl="m/ ...

  10. 美国警察iPhone数据线挡住歹徒子弹获救

    泡泡网手机频道11月1日 现在手机的功能越来越丰富,不仅可以接打电话.收发短信.玩游戏聊天,关键时刻还能救命.前天HTC手机再次忠心护主,让许多同学对HTC赞赏有加.而现在又有人捡了一条命,不过这次救 ...