学习:请看 www.cnblogs.com/jh818012/p/3252154.html

模板题,上代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=;
const LL mod=1e9+;
int n,m,sz;
int u[N],l[N],r[N],d[N];
int col[N],row[N];
int h[],s[];
void init()
{
for(int i=;i<=m;++i)
{
s[i]=;
u[i]=d[i]=i;
l[i]=i-;
r[i]=i+;
}
r[m]=;l[]=m;
sz=m;
for(int i=;i<=n;++i)
h[i]=-;
}
void link(int x,int y)
{
++sz;
++s[y],col[sz]=y,row[sz]=x;
u[sz]=u[y],d[u[y]]=sz;
d[sz]=y,u[y]=sz;
if(h[x]==-)h[x]=l[sz]=r[sz]=sz;
{
l[sz]=l[h[x]];
r[l[h[x]]]=sz;
r[sz]=h[x];
l[h[x]]=sz;
}
}
void del(int y)
{
l[r[y]]=l[y];
r[l[y]]=r[y];
for(int i=d[y];i!=y;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]];
}
}
}
void resume(int y)
{
for(int i=u[y];i!=y;i=u[i])
{
for(int j=l[i];j!=i;j=l[j])
{
d[u[j]]=u[d[j]]=j;
++s[col[j]];
}
}
r[l[y]]=l[r[y]]=y;
}
int ans[],tmp;
bool dance(int pos)
{
if(!r[])
{
tmp=pos;
return ;
}
int t=r[];
for(int i=r[];i!=;i=r[i])
if(s[i]<s[t])t=i;
del(t);
for(int i=d[t];i!=t;i=d[i])
{
ans[pos]=row[i];
for(int j=r[i];j!=i;j=r[j])
del(col[j]);
if(dance(pos+))return ;
for(int j=l[i];j!=i;j=l[j])
resume(col[j]);
}
resume(t);
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=;i<=n;++i)
{
int x,y;
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
link(i,y);
}
}
if(dance())
{
printf("%d",tmp);
for(int i=;i<tmp;++i)
printf(" %d",ans[i]);
printf("\n");
}
else printf("NO\n");
}
return ;
}

HUST 1017 Exact cover dance links的更多相关文章

  1. HUST 1017 Exact cover (Dancing links)

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

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

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

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

  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 , 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

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

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

  8. [DLX] hust 1017 Exact cover

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

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

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

随机推荐

  1. Oracle 常用操作

    修改数据库表名:rename [old-table] to [new-table]; while循环添加测试数据: declare     i int:=1;     begin     while ...

  2. 1509 -- Glass Beads POJ

    题意:求一个字符串的最小表示的开始下标 就当模板题写了 把字符串重复一遍,再建后缀自动机,贪心的选最小字典序在上面走len步 因为走出来的一定是子串,长度又是len,所以一定是原来的字符串旋转得到的, ...

  3. PHP 性能分析第三篇: 性能调优实战

    注意:本文是我们的 PHP 性能分析系列的第三篇,点此阅读 PHP 性能分析第一篇: XHProf & XHGui 介绍 ,或  PHP 性能分析第二篇: 深入研究 XHGui. 在本系列的 ...

  4. memcached部署memcached环境及PHP扩展

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lxsym.blog.51cto.com/1364623/876209 Memca ...

  5. ANDROID_MARS学习笔记_S01_003layout初步

    一.layout介绍 二.测试linear_layout1.activity_main.xml <?xml version="1.0" encoding="utf- ...

  6. Android性能优化之如何避免Overdraw

    什么是Overdraw? Overdraw就是过度绘制,是指在一帧的时间内(16.67ms)像素被绘制了多次,理论上一个像素每次只绘制一次是最优的,但是由于重叠的布局导致一些像素会被多次绘制,而每次绘 ...

  7. 15个必知的Android开发者选项

    Android开发者选项,看起来很简单的事情,其实很多同学对它了解得不够,Google用心良苦得为我们设计了这么多小开关都是有它的作用的,今天也花了点时间,过了一遍全部的30多个开关,从中整理出15个 ...

  8. allegro中数据库检查

    1. -------------------- ----

  9. WPF程序中处理Windows消息

    首先通过WindowInteropHelper类,我们可以获取WPF Window的Handle. WindowInteropHelper helper = new WindowInteropHelp ...

  10. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...