[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 ...
随机推荐
- Android权限管理PermissionsDispatcher2.3.2使用+原生6.0权限使用
PermissionsDispatcher2.3.2使用 Android6.0权限官网https://developer.android.com/about/versions/marshmallow/ ...
- JavaScript之深浅拷贝
数组的浅拷贝 如果是数组,我们可以利用数组的一些方法比如:slice.concat 返回一个新数组的特性来实现拷贝.比如: , true, null, undefined]; var new_arr ...
- GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- 【cocos2dx开发技巧8】自定义控件-使自定义控件具有RGBA特性
转发,请保持地址:http://blog.csdn.net/stalendp/article/details/9948545 虽然CCNodeRGBA,CCLayerRGBA,sprite等提供颜色和 ...
- ToolBarTray
<ToolBarTray DockPanel.Dock="Top"> <ToolBar ToolBarTray.IsLocked="True" ...
- WordPress 后台添加额外选项字段到常规设置页面
有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例: 直接添加到主题的 functions.php 即可: /*** WordPres ...
- 内容匹配广告投放技术4:网盟CTR预估(百度文库课程)
原文:http://wbj0110.iteye.com/blog/2043065 该文是百度文库课程<计算广告学之内容匹配广告&展示广告原理.技术和实践>的课程笔记,感谢百度! 课 ...
- [Javascript] Ternary Conditionals
/** Ternary Conditionals */ // //**Bad** // var isArthur = false; var weapon; if(isArthur){ weapon = ...
- VB 中窗口发现冲突名称,将使用名称...怎么解决?
首先上图: 刚開始敲机房收费系统的时候就出现过这样的情况,当时有八个之多. 解决方法: (1)对窗口进行重命名(比如将frmGongZuoJiLu改成frmGongzuojilu),于是从8个错误降低 ...
- Adobe Dynamic Http Streaming的简单配置与实现 (FMS, HLS, HDS)
http://blog.csdn.net/avsuper/article/details/7663879 Adobe的Http Dynamic Streaming是针对苹果的HLS方案提出基于HTTP ...