最基础的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 精确覆盖模板题的更多相关文章

  1. HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

    题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...

  2. hihoCoder #1321 : 搜索五•数独 (Dancing Links ,精确覆盖)

    hiho一下第102周的题目. 原题地址:http://hihocoder.com/problemset/problem/1321 题意:输入一个9*9数独矩阵,0表示没填的空位,输出这个数独的答案. ...

  3. 【转】Dancing Links精确覆盖问题

    原文链接:http://sqybi.com/works/dlxcn/ (只转载过来一部分,全文请看原文,感觉讲得很好~)正文    精确覆盖问题    解决精确覆盖问题    舞蹈步骤    效率分析 ...

  4. HDU 3111 Sudoku ( Dancing Links 精确覆盖模型 )

    推荐两篇学DLX的博文: http://bbs.9ria.com/thread-130295-1-1.html(这篇对DLX的工作过程演示的很详细) http://yzmduncan.iteye.co ...

  5. POJ3074 Sudoku —— Dancing Links 精确覆盖

    题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  6. ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )

    题意 :  给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m )  .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...

  7. Hdu3498-whosyourdaddy(精确覆盖模板题)

    Problem Description sevenzero liked Warcraft very much, but he haven't practiced it for several year ...

  8. HDU5046 Airport dancing links 重复覆盖+二分

    这一道题和HDU2295是一样 是一个dancing links重复覆盖解决最小支配集的问题 在给定长度下求一个最小支配集,只要小于k就行 然后就是二分答案,每次求最小支配集 只不过HDU2295是浮 ...

  9. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

随机推荐

  1. 自動獲取外網IP并發郵件

    問題: 公司有一Web系統需開放給香港Office公司查詢資料,但最近動態域名需實名認證, 因系統較小型,非公開大範圍使用,所以再認證一域名也沒多大必要, 所以想定時生成一封能查詢外網IP的郵件發送給 ...

  2. redis过期事件

    背景:目前在商城项目,订单有过期逻辑,小伙伴提议用redis做,经讨论分析,redis有key的过期事件,貌似可以实现,但是咨询大神,好像不建议这样用,可能会丢数据 随便写了段python代码测试 i ...

  3. 【转】java序列化一定要应该注意的6个事项!

    1.如果子类实现Serializable接口而父类未实现时,父类不会被序列化,但此时父类必须有个无参构造方法,否则会抛InvalidClassException异常. 2.静态变量不会被序列化,那是类 ...

  4. 基于udp协议的套接字及udp协议粘包问题

    udp协议的套接字 udp协议传输  服务端和客户端没有建立连接一说. import socket # 总结一下基础工作流程:服务端生成套接字并绑定ip_port,进入数据传输循环,服务端接受客户端发 ...

  5. CF385C Bear and Prime Numbers

    思路: 需要对埃氏筛法的时间复杂度有正确的认识(O(nlog(log(n)))),我都以为肯定超时了,结果能过. 实现: #include <bits/stdc++.h> using na ...

  6. python中的seteuid

    seteuid(...)    seteuid(uid)        Set the current process's effective user id.

  7. 获取父页面的dom元素

    $("li.jericho_tabs", window.top.document); 上面的代码意思是获取父页面的li元素,class为jericho_tabs的所有元素.

  8. 华为S3700交换机DHCP 配置

    1.设置交换机名称 system-view [Huawei]sysname dhcp01 [dhcp01] 2.配置管理IP [dhcp01]interface Vlanif 1 [dhcp01-Vl ...

  9. javaweb系列-关于HttpSessionListener的sessionDestroyed什么时候触发

    根据书本写了下面这个监听器,然后开始调试,打开一个浏览器来访问该网页,可以正常触发sessionCreated,然后关闭浏览器,发现没有触发sessionDestroyed,然后我怀疑是不是这个监听器 ...

  10. T1订正记-AC自动机-从树到图

    AC自动机已经足够棒了. 但是,好像有时还是要TLE的. 一般的AC自动还是比较好,如果在某些情况下还是会被卡掉,像是这个水题 考试的感觉 我看到这个题后,我清清楚楚的知道,这是个AC自动机+栈. 经 ...