HUST 1017 Exact cover dance links
学习:请看 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的更多相关文章
- HUST 1017 Exact cover (Dancing links)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...
- [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 ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- (简单) 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 ...
- [HUST 1017] Exact cover
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
- 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 ...
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- 搜索(DLX):HOJ 1017 - Exact cover
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 ...
随机推荐
- poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
- vs2008+cmake2.8+OpenCV2.8.4配置过程中OpenCV.sln重编译部分工程失败
解决方法来自此链接 http://www.tuicool.com/articles/qiQBb2N vs2008+cmake2.8+OpenCV2.8.4配置过程 1.解压opencv2.4.8 2. ...
- 应该如何入门deep learning呢?从UFLDL开始!
抱歉,大家,这里不是要分享如何学习deep learning,而是想要记录自己学习deep learning的小历程,算是给自己的一点小动力吧,希望各位业内前辈能够多多指教! 看到有网友提到,Andr ...
- CSS3新的字体尺寸单位rem
CSS3引入新的字体尺寸单位 rem ,可以简单记忆为root rm. CSS3的出现,他同时引进了一些新的单位,包括我们今天所说的rem.在W3C官网上 是这样描述rem的——“font size ...
- 【网络】IP地址分配、端口号、分层
3.网络分层 OSI的七层网络结构图和TCP/IP的五层结构图 OSI七层模型OSI中的层 功能 ...
- A JSTL primer, Part 2: Getting down to the core
In the initial article of this series, you got your first look at JSTL. We described the use of its ...
- Linux bash shell脚本语法入门
1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 # //shell脚本注释符号 2.变量和使用 HOME= ...
- linux 5.5 开xmanager远程
http://bbs.cqsztech.com/dv_rss.asp?s=xhtml&boardid=3&id=11&page=9 linux 5.5 开xmanager远程 ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)
一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...
- Permutation Test 置换检验(转)
Permutation Test 置换检验 显著性检验通常可以告诉我们一个观测值是否是有效的,例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等(或者那个均值更大).我们在实验中经 ...