(简单) HUST 1017 Exact cover , DLX+精确覆盖。
Description
//HUST 1017 #include<iostream>
#include<cstring> using namespace std; // N 行 M 列 。 。 。 const int INF=10e8; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; // 这个的大小可以适当减少。 。 。 struct DLX
{
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode],row[MaxNode]; //row 可以不要。
int H[MaxN],S[MaxM];
int size,n,m;
int ansnum,ans[MaxN]; void init(int _n,int _m)
{
n=_n;
m=_m;
size=m;
ansnum=INF; for(int i=;i<=m;++i)
{
L[i]=i-;
R[i]=i+;
U[i]=D[i]=i; S[i]=;
}
R[m]=;
L[]=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
++S[c];
row[size]=r; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
L[R[c]]=L[c];
R[L[c]]=R[c]; for(int i=D[c];i!=c;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 c)
{
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
{
U[D[j]]=D[U[j]]=j; ++S[col[j]];
} L[R[c]]=R[L[c]]=c;
} void showans(int d)
{
cout<<d; for(int i=;i<d;++i)
cout<<' '<<ans[i]; cout<<endl;
} bool Dance(int d)
{
if(R[]==)
{
showans(d); return ;
} int c=R[]; for(int i=R[];i;i=R[i])
if(S[i]<S[c])
c=i; remove(c); for(int i=D[c];i!=c;i=D[i])
{
ans[d]=row[i]; for(int j=R[i];j!=i;j=R[j])
remove(col[j]); if(Dance(d+))
return ; for(int j=L[i];j!=i;j=L[j])
resume(col[j]);
} resume(c); return ;
}
}; DLX dlx; int main()
{
ios::sync_with_stdio(false); int N,M,K;
int t; while(cin>>N>>M)
{
dlx.init(N,M); for(int i=;i<=N;++i)
{
cin>>K; for(int j=;j<=K;++j)
{
cin>>t; dlx.Link(i,t);
}
} if(!dlx.Dance())
cout<<"NO"<<endl;
} return ;
}
(简单) HUST 1017 Exact cover , DLX+精确覆盖。的更多相关文章
- 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 ...
- 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 ...
- [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
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。
Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...
- HUST 1017 Exact cover dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...
随机推荐
- sql server多行数据(一列)转换成一个字段
create table #test ( id int not null, memeo int not null ) ,) ,) ,) )) ,,'') drop table #test 总结: 格式 ...
- C++随机崩溃捕捉处理
1. 会引起异常的几个原因(主要记录目前遇到过的几个问题) 程序读取了无效的内存地址 堆栈的溢出,比如无限循环导致那段内存溢出,比如把size为20的缓存拷贝到size为10的缓存块等 无法申请到有效 ...
- Temporary exceptions can be configured via your app's Info.plist file.
报错: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure ...
- php截取中文字符串,英文字符串,中英文字符串长度的方法
今天学习了php函数截取中文字符串,英文字符串,中英文字符串的函数使用方法.对中英文截取方法不理解,此处先做记录. PHP自带的函数如strlen().mb_strlen()都是通过计算字符串所占字节 ...
- 转: JMeter技巧集锦
from:http://java.chinaitlab.com/tools/355421.html JMeter 是一个流行的用于负载测试的开源工具, 具有许多有用的功能元件,如线程组(thread ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- Java Web 错误排查
排查404 1. 检查web.xml,有没有放在web-inf下面,再检查过滤器有没有配置 <filter> <filter-name>struts</filter-na ...
- 【转】js/jquery中刷新iframe方法(兼容主流)
一.js实现刷新两种方式: 1.//方法1 2.document.getElementById('FrameID').contentWindow.location.reload(true); 3.// ...
- Mac linux 安装memcached服务 用法
今天在Mac上安装memcached服务的时候 由于安装memcached之前需要安装libevent依赖包 所以使用brew install libevent 安装过程中报错 Warning: Yo ...
- 网络层 IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
目录 IP协议首部格式地址解析协议 ARP逆向地址解析协议 RARP网际控制报文协议 ICMP网际组管理协议IGMP IP 数据报首部 IP数据报首部格式: 最高位在左边,记为0 bit:最低位在右边 ...