hust 1017 dancing links 精确覆盖模板题
最基础的dancing links的精确覆盖题目
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
#define N 1005
#define MAXN 1000100 struct DLX{
int n , m , size;//size表示当前dlx表中有多少个元素
int ans[N] , k;//ans[]记录选取的行
int U[MAXN] , D[MAXN] , L[MAXN] , R[MAXN];
int row[MAXN] , col[MAXN] ; // 分别表示第 i 号节点属于第几行或者第几列
int cnt_col[N];//分别表示第i行或者第i列有多少个节点
int first[N]; //行上的起始指针 void init(int _n , int _m)
{
n = _n , m = _m;
size = m;
for(int i= ; i<=m ; i++){
U[i] = D[i] = i;
L[i] = i- , R[i] = i+;
}
L[] = m , R[m] = ;
for(int i= ; i<=n ; i++) first[i]=-;
for(int i= ; i<=m ; i++) cnt_col[i] = ;
// for(int i=0 ; i<=n ; i++) cout<<"here: "<<i<<" "<<L[i]<<" "<<R[i]<<endl;
} void link(int r , int c)
{
++size;
//修改列上的情况
D[size] = D[c] , U[D[c]] = size;
U[size] = c , D[c] = size;
cnt_col[c]++ , col[size] = c ; //修改行上的情况
if(first[r]<) first[r] = L[size] = R[size] = size;
else{
R[size] = R[first[r]] , L[R[first[r]]] = size;
L[size] = first[r] , R[first[r]] = size;
}
// cout<<" r: "<<r<<" c: "<<c<<" "<<size<<" "<<L[size]<<" "<<R[size]<<" "<<U[size]<<" "<<D[size]<<endl;
row[size] = r;
} 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]){
D[U[j]] = D[j] , U[D[j]] = U[j];
--cnt_col[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;
++cnt_col[col[j]];
}
}
R[L[c]] = L[R[c]] = c;
} bool Dance(int d)
{ if(!R[]){
k = d;
return true;
}
int st = R[];
//找到能删除最少节点的列先删除,这样递归的行的次数就会减少,提高效率
for(int i=st ; i!= ; i=R[i]){
if(cnt_col[st]>cnt_col[i])
st = i;
} Remove(st);
for(int i=D[st] ; i!=st ; i=D[i]){
ans[d] = row[i];
for(int j=R[i] ; j!=i ; j=R[j]) Remove(col[j]);
if(Dance(d+)) return true;
for(int j=L[i] ; j!=i ; j=L[j]) Resume(col[j]);
}
Resume(st);
return false;
}
}dlx; int main()
{
// freopen("a.in" , "r" , stdin);
int n , m; while(~scanf("%d%d" , &n , &m))
{
dlx.init(n , m);
for(int r= ; r<=n ; r++){
int m , c;
scanf("%d" , &m);
for(int i= ; i<m ; i++){
scanf("%d" , &c);
dlx.link(r , c);
}
}
bool ok = dlx.Dance();
if(ok){
printf("%d" , dlx.k);
sort(dlx.ans , dlx.ans+dlx.k);
for(int i= ; i<dlx.k ; i++) printf(" %d" , dlx.ans[i]);
puts("");
}
else puts("NO");
}
return ;
}
hust 1017 dancing links 精确覆盖模板题的更多相关文章
- HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题
题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...
- hihoCoder #1321 : 搜索五•数独 (Dancing Links ,精确覆盖)
hiho一下第102周的题目. 原题地址:http://hihocoder.com/problemset/problem/1321 题意:输入一个9*9数独矩阵,0表示没填的空位,输出这个数独的答案. ...
- 【转】Dancing Links精确覆盖问题
原文链接:http://sqybi.com/works/dlxcn/ (只转载过来一部分,全文请看原文,感觉讲得很好~)正文 精确覆盖问题 解决精确覆盖问题 舞蹈步骤 效率分析 ...
- HDU 3111 Sudoku ( Dancing Links 精确覆盖模型 )
推荐两篇学DLX的博文: http://bbs.9ria.com/thread-130295-1-1.html(这篇对DLX的工作过程演示的很详细) http://yzmduncan.iteye.co ...
- POJ3074 Sudoku —— Dancing Links 精确覆盖
题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...
- Hdu3498-whosyourdaddy(精确覆盖模板题)
Problem Description sevenzero liked Warcraft very much, but he haven't practiced it for several year ...
- HDU5046 Airport dancing links 重复覆盖+二分
这一道题和HDU2295是一样 是一个dancing links重复覆盖解决最小支配集的问题 在给定长度下求一个最小支配集,只要小于k就行 然后就是二分答案,每次求最小支配集 只不过HDU2295是浮 ...
- DLX精确覆盖与重复覆盖模板题
hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...
随机推荐
- PHP使用iconv函数遍历数组转换字符集
/** * 字符串/二维数组/多维数组编码转换 * @param string $in_charset * @param string $out_charset * @param mixed $dat ...
- Elasticsearch (2) - 映射
常用映射类型 核心的字段类型如下: String 字符串包括text和keyword两种类型: 1.text analyzer 通过analyzer属性指定分词器. 下边指定name的字段类型为tex ...
- 【经验总结】VS2010下建立MFC程序
孙鑫的MFC教学视频非常不错,但是由于视频中孙鑫老师采用VC6.0版本,而现在 许多人都转向了使用VS,VS为我们生成了许多不需要的代码,这也导致在这节课的学习编程中总是遇到一些困难.那么,如何去掉这 ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- (转)搭建Spring4.x.x开发环境
http://blog.csdn.net/yerenyuan_pku/article/details/52831306 先去Spring官网下载Spring4.x.x开发包(本人使用的版本是Sprin ...
- 2019年今日头条机试_JAVA后台岗_第二题
使用map的递推,java对象做key需要重写equeal,hashCode方法,使拥有相同属性值的对象被识别为同一对象. import java.util.*; class Cat{ public ...
- uva1628 Pizza Delivery
fixing great wall 的变形dp(i,j,k,p)不考虑i-j的客人,还要送k个人,目前位置在p起点i和总数量k都要枚举dp(i,j,k,p)=max(dp(m,j,k-1,p)+val ...
- 阿里云人脸比对API封装
这是根据封装是根据阿里云官方给的Demo进行修改的,当时是因为编写微信小程序云函数需要使用到阿里云人脸比对接口,才对其进行封装的. 记录下来,方便下次使用. 复制下来可以直接使用. 用到的依赖如下: ...
- 公共dao的抽取
package cn.sxx.dao; import java.util.List; import cn.sxx.model.Dep; import cn.sxx.query.DepQuery; pu ...
- Melancholy(磨懒虫主义)
题目大意:给出n个地点和q个询问.其中每个地点有距离和权值,每个询问给出l,r,k,表示在[l,r]区间内不取最小点的情况下任取k个,求所有情况权值之积之和(n,q<=1e5,k<=6). ...