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 ...
随机推荐
- LSP
Liskov Substitution Principle里氏替换原则,OCP作为OO的高层原则,主张使用“抽象(Abstraction)”和“多态(Polymorphism)”将设计中的静态结构改为 ...
- 《Head First HTML与CSS》的CSS属性
关于继承的结论. 1.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(详见(3)<Head First HTML与CSS>学习笔记---CSS入门的2) 2.基于类的选择器> ...
- Keil简介
最早接触Keil是学习开发8051系列的单片机.Keil C51是Keil公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易 ...
- bt设置指定的ip地址
auto eth0 iface eth0 inet staticaddress 192.168.1.112 IP地址netmask 255.255.255.0 子网掩码network 192.168. ...
- Redis杂谈
这是2015年初应邀在南华智闻作技术交流时所作的Redis方面的一个presentation. 因为原件是Keynote格式,已经转成PDF,点击下面链接打开或者下载PDF: Redis 杂谈
- Winform用Post方式打开IE
1.主要实现Code void OpenNewIe(string url, string postData)///url是要post的网址,postData是要传入的参数 { if (ie != nu ...
- codevs 1219 骑士游历 1997年
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象 ...
- 洛谷 P1361 小猫爬山
题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...
- python json.loads json.dumps的区别
json.loads() 是将字符串传化为字典 json.dumps () 是将字典转化为字符串 >>> dict = "{8:'bye', 'you':'coder'}& ...
- vue.js中的表单radio,select,textarea的v-model属性的用法
只要是表单元素,其值已经不会再用value来定义了,但是placeholder还是可以用来设置默认值. section1--input:type="text" type=" ...