首页 技术 正文
技术 2022年11月15日
0 收藏 452 点赞 3,413 浏览 1412 个字

正题

题目链接:https://www.luogu.com.cn/problem/P4929


题目大意

\(n*m\)的矩形有\(0/1\),要求选出若干行使得每一列有且仅有一个\(1\)。


解题思路

精确覆盖问题指的是一个集合\(S\)和它的若干个子集集合\(T\),要求选出\(T\)的一个子集使得里面的集合元素刚好覆盖集合\(S\)。

\(DLX\)全称是\(dancing\ link\ X\),其中\(dancing\ link\)是指交叉十字循环双向链,\(X\)是指暴搜。

知道了这些,就可以去看洛谷题解了(


code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10100;
int n,m,cnt,l[N],r[N],u[N],d[N],h[N],row[N],col[N],s[N],ans[N];
void init(){
for(int i=0;i<=m;i++)
l[i]=i-1,r[i]=i+1,u[i]=d[i]=i;
l[0]=m;r[m]=0;cnt=m;
}
void link(int x,int y){
col[++cnt]=y;s[y]++;
d[cnt]=y;u[cnt]=u[y];
d[u[y]]=cnt;u[y]=cnt;
row[cnt]=x;
if(!h[x])h[x]=l[cnt]=r[cnt]=cnt;
else{
l[cnt]=l[h[x]];r[cnt]=h[x];
r[l[h[x]]]=cnt;l[h[x]]=cnt;
}
return;
}
void remove(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]]--;
return;
}
void recover(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]]=d[u[j]]=j,s[col[j]]++;
r[l[x]]=l[r[x]]=x;
return;
}
bool dance(int dep){
if(r[0]==0){
for(int i=0;i<dep;i++)
printf("%d ",ans[i]);
return 1;
}
int c=r[0];
for(int i=c;i!=0;i=r[i])
if(s[i]<s[c])c=i;
remove(c);
for(int i=d[c];i!=c;i=d[i]){
ans[dep]=row[i];
for(int j=r[i];j!=i;j=r[j])remove(col[j]);
if(dance(dep+1))return 1;
for(int j=l[i];j!=i;j=l[j])recover(col[j]);
}
recover(c);
return 0;
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
int x;scanf("%d",&x);
if(x)link(i,j);
}
if(!dance(0))
puts("No Solution!");
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295