学习:请看 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. (转载)Cocos2dx-OpenGL ES 2.0教程:你的第一个三角形(1)

    前言 在本系列教程中,我会以当下最流行的2D引擎Cocos2D-X为基础,介绍OpenGL ES 2.0的一些基本用法.本系列教程的宗旨是OpenGL扫盲,让大家在使用Cocos2D-X过程中,知其然 ...

  2. 使用自定义任务审批字段创建 SharePoint 顺序工作流

    http://msdn.microsoft.com/zh-cn/library/hh824675(v=office.14).aspx#odc_sp14_ta_CreatingSPSeqWorkflow ...

  3. 搭建Git Server

    windows上如何搭建Git Server   Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有 ...

  4. pageControl指示器和图片放大-b

    小编由于篇幅问题,截取了最后一篇,如果需要看其他的三篇文章,可以去笔者的简书看:http://www.jianshu.com/users/9f3739421d15/latest_articles 另外 ...

  5. 莫名其妙的主机名 VM_32_234_centos

    在腾讯服务器上部署的时候出现的莫名其妙的错误, 我在配置文件上都写 master, node1, 但是运行mapredure的时候的时候老是出现 Job job_1409546978133_0001 ...

  6. uva 348

    dp还是比较容易  关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...

  7. Codeforces Round #242 (Div. 2) A~C

    题目链接 A. Squats time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputou ...

  8. hdu 3389 Game 博弈论

    思路: 其本质为阶梯博弈; 阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈...     每一步则是将一个集体上的若干个点( >=1 )移到前面去,最后没有点可以移 ...

  9. Android 使用httpClient POST 模拟发送 multipart表单内容

    使用的环境:apache-mime4j-0.6.jar,httpcore-4.3.2.jar,httpmime-4.3.3.jar try { HttpPost httpPost = new Http ...

  10. LCMS

    LCMS(LearningContent Management System) 即学习内容管理系统