[DLX] hust 1017 Exact cover
题意:
给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个)
然后每一个包里有k个东西,每一个东西都有编号。
思路:
舞蹈连模板题
代码:
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"vector"
#include"string"
using namespace std;
#define N 1010*100 //总共元素个数
#define RN 1010 //总共行数
#define CN 1010 //总共列数
struct DLX
{
int n,m,C; //n,m代表范围行列数。C代表元素个数
int U[N],D[N],L[N],R[N],Row[N],Col[N]; //U、D、L、R代表第C个的上下左右各自是哪个 Row和Col代表第C的行号和列号
int H[RN],S[CN],cnt,ans[RN]; //H为每行开头是哪个,H[i]==-1代表本行没有元素 S代表每列有多少个元素 cnt为结果是选了几行 ans存结果是哪几行
void init(int _n,int _m) //初始化 创建0~m个原始元素(第0行)
{
n=_n;
m=_m;
for(int i=0; i<=m; i++)
{
U[i]=D[i]=i;
L[i]=(i==0? m:i-1);
R[i]=(i==m?0:i+1);
S[i]=0;
}
C=m;
for(int i=1; i<=n; i++) H[i]=-1;
}
void link(int x,int y) //连接行数,就是把元素插入
{
C++;
Row[C]=x;
Col[C]=y;
S[y]++;
U[C]=U[y];
D[C]=y;
D[U[y]]=C;
U[y]=C;
if(H[x]==-1) H[x]=L[C]=R[C]=C;
else
{
L[C]=L[H[x]];
R[C]=H[x];
R[L[H[x]]]=C;
L[H[x]]=C;
}
}
void del(int x) //删除函数。删除相应列
{
R[L[x]]=R[x];
L[R[x]]=L[x];
for(int i=D[x]; i!=x; 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 rec(int x) //恢复函数,恢复相应列
{
for(int i=U[x]; i!=x; i=U[i])
{
for(int j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
R[L[x]]=x;
L[R[x]]=x;
}
int dance(int x) //舞蹈连搜索函数
{
if(R[0]==0) //满足条件,默认是R[0]=0也就是第0行没有元素了,可是因题目而定
{
cnt=x;
return 1;
}
int now=R[0];
for(int i=R[0]; i!=0; i=R[i]) //选择含有元素最多的列。可加速
{
if(S[i]<S[now]) now=i;
}
del(now);
for(int i=D[now]; i!=now; i=D[i])
{
ans[x]=Row[i];
for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
if(dance(x+1)) return 1;
for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
}
rec(now);
return 0;
}
} dlx;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=-1)
{
dlx.init(n,m);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
while(k--)
{
int x;
scanf("%d",&x);
dlx.link(i,x);
}
}
int f=dlx.dance(0);
if(f==0) puts("NO");
else
{
printf("%d",dlx.cnt);
for(int i=0; i<dlx.cnt; i++) printf(" %d",dlx.ans[i]);
puts("");
}
}
return 0;
}
[DLX] hust 1017 Exact cover的更多相关文章
- 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 (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 , 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(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 dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...
- 搜索(DLX):HOJ 1017 - Exact cover
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 ...
随机推荐
- 通过小实例谈谈javascript的间隔调用和延时调用
用 setInterval方法可以以指定的间隔实现循环调用函数,直到clearInterval方法取消循环 用clearInterval方法取消循环时,必须将setInterval方法的调用赋值给一个 ...
- C#匿名方法与Delegate类型转换错误
问题描述 C#2.0出现了匿名方法, 这在一定程度上节省了我们维护代码上下文的精力, 也不需要思考为某个方法取什么名字比较合适. 在FCL的一些方法中要求传入一个Delegate类型的参数, 比如Co ...
- IOS之正则表达式
在现阶IOS开发的样式越来越多,我们在开发APP的时候难免会遇到对用户的登录和注册进行操作,但是登录注册如果想要做的人性化少不了的就是校验,对当前用户的登录信息进行校验,如果满足要求我们会把用户注册的 ...
- myeclipse创建maven android项目
一.搭建环境 1.安装android maven插件,我在网上找了半天.没有找到这个插件,于是选择了在线安装.选择myeclipse 的 [help]->[install form catalo ...
- UVA 11464 Even Parity (独特思路)
题意:有一个n*n的01矩阵,任务是把尽可能少的0变成1,使得每个元素的上.下.左.右元素之和为偶数. 思路:很容易想到的思路是枚举每个点是0还是1,因为n<=15,复杂度就是2^225显然TL ...
- Declaration Merging with TypeScript
原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/ Why might you need this? There can ...
- 正则 js截取时间
项目中要把时间截取,只要年月日,不要时分秒,于是 /\d{4}-\d{1,2}-\d{1,2}/g.exec("2012-6-18 00:00:00") 或者另一种 var dat ...
- CentOS6.3 安装配置 ant
OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...
- @SuppressLint("NewApi")和@TargetApi()的区别
在Android代码中,我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,此时编译器会提示警告, 解决方法是在方法上加上@Suppr ...
- picture control控件
注意:picture control控件,需要先更改其ID再使用. CImage myImage; CFileDialog fileDlg(TRUE,NULL,NULL,OFN_ALLOWMULTIS ...