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 ...
随机推荐
- K-th Number 线段树的区间第K大
http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...
- Suricata的总体架构
Suricata的总体架构 报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...
- AJPFX的反射学习笔记
反射是描述 数据结构的结构 属性.方法(数据)元数据 类(数据结构)描述数据的结构-->类也是特殊的对象---->元数据 CLASS类 描述数据结 ...
- 解决IDEA Tomcat控制台乱码问题
1.在Tomcat Server的配置中添加一句命令: 神秘代码: -Dfile.encoding=UTF-8 重启Tomcat,ok. 如果还不行,则需要: 1.在Settings中修改文件编码 2 ...
- poj3436 Computer Factory
题意: 电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi. 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移 ...
- Angularjs 实现 $(document).ready()的两种方法
1.在controller里面利用$on或者$watch bookControllers.controller('bookctrl_test', ['$scope', '$routeParams', ...
- 云态(YunCloud)的Centos服务器修改dns教程
DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终 ...
- 回顾Spring MVC_01_概述_入门案例
SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- bzoj3307 雨天的尾巴 题解(线段树合并+树上差分)
Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y 对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成 所有发放后,每个点存放最多的是哪种物品. Input ...