#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <map>
#define LL long long using namespace std; const int N = 2e5,M = 2e3; struct DLX
{
int n,m,size;
int up[N],down[N],right[N],left[N],row[N],col[N]; //four pointer and the coordinate of nodes
int Head[M],Size[M]; //head pointer and the size of each linkList
int ans[M],ansd;
void init(int _n,int _m) //initialize the head line node
{
n = _n;
m = _m;
for(int i = ; i <= m; i++)
{
Size[i] = ;
up[i] = down[i] = i; //each column point to itself
left[i] = i-; //row point to their neighbor
right[i] = i+;
}
right[m] = ;
left[] = m; //circulate link
size = m;
for(int i = ; i <= n; i++)
Head[i] = -;
}
void Link(int r,int c) //modify the four pointer
{
++Size[col[++size]=c];
row[size] = r;
down[size] = down[c];
up[down[c]] = size;
up[size] = c;
down[c] = size; if(Head[r] < ) Head[r] = left[size] = right[size] = size;
else
{
right[size] = right[Head[r]];
left[right[Head[r]]] = size;
left[size] = Head[r];
right[Head[r]] = size;
}
} void remove(int c) //delete the column
{
left[right[c]] = left[c];
right[left[c]] = right[c];
for(int i = down[c]; i != c;i = down[i])
for(int j = right[i]; j != i; j = right[j])
{
up[down[j]] = up[j];
down[up[j]] = down[j];
--Size[col[j]];
}
} void resume(int c)
{ for(int i = up[c]; i != c; i = up[i])
for(int j = left[i]; j != i; j = left[j])
{
++Size[col[up[down[j]]=down[up[j]]=j]];
}
left[right[c]] = right[left[c]] = c;
} bool dance(int d) //the depth
{
if(right[] == )
{
ansd = d;
return true;
} int c = right[];
for(int i = right[]; i != ; i = right[i])
{
//if(Size[i] == 0) return false;
if(Size[i] < Size[c])
c = i;
}
remove(c);
for(int i = down[c]; i != c; i = down[i])
{
ans[d] = row[i];
for(int j = right[i]; j != i; j = right[j]) remove(col[j]);
if(dance(d+)) return true;
for(int j = left[i]; j != i; j = left[j]) resume(col[j]);
}
resume(c);
return false;
}
}; DLX g; void solve(int n,int m)
{
// int n,m;
// scanf("%d %d",&n,&m); g.init(n,m);
for(int i = ; i <= n; i++)
{
int t;
scanf("%d",&t);
for(int j = ; j <= t; j++)
{
int num;
scanf("%d",&num);
g.Link(i,num);
}
}
if( !g.dance())
printf("NO\n");
else
{
printf("%d ",g.ansd);
for(int i = ; i < g.ansd; i++)
{
printf("%d%c",g.ans[i],i == g.ansd - ?'\n':' ');
}
} } int main(void)
{
int n,m;
while(scanf("%d %d",&n,&m) != -)
{
solve(n,m);
}
return ;
}

DLX的更多相关文章

  1. DLX (poj 3074)

    题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%2030 ...

  2. HDU 3957 Street Fighter(搜索、DLX、重复覆盖+精确覆盖)

    很久以前就看到的一个经典题,一直没做,今天拿来练手.街霸 给n<=25个角色,每个角色有 1 or 2 个版本(可以理解为普通版以及爆发版),每个角色版本可以KO掉若干人. 问最少选多少个角色( ...

  3. 数独求解 DFS && DLX

    题目:Sudoku 题意:求解数独.从样例和结果来看应该是简单难度的数独 思路:DFS 设置3个数组,row[i][j] 判断第i行是否放了j数字,col[i][j] 判断第i列是否放了j数字.squ ...

  4. DLX模型问题

    问题:sevenzero liked Warcraft very much, but he haven't practiced it for several years after being add ...

  5. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

  6. HDU 5046 Airport(dlx)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. ...

  7. POJ2676,HDU4069解决数独的两种实现:DFS、DLX

    搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...

  8. HDU2295 Radar (DLX)

    下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...

  9. DLX舞蹈链 hdu5046

    题意: 在N个城市选出K个城市,建飞机场(1 ≤ N ≤ 60,1 ≤ K ≤ N),N个城市给出坐标,选择这K个机场,使得从城市到距离自己最近的机场的 最大的距离 最小. 输出这个最小值. 思路: ...

  10. hdu 3111 DLX解数独

    思路:裸的DLX解数独.关键是建图,感觉还不如写个dfs直接,DLX写这个的代码很烦. #include<set> #include<map> #include<cmat ...

随机推荐

  1. 【学术篇】状态压缩动态规划——POJ3254/洛谷1879 玉米田Corn Field

    我要开状压dp的坑了..直播从入门到放弃系列.. 那就先拿一道状压dp的水题练练手吧.. 然后就找到了这一道..这道题使我清醒地认识到阻碍我的不是算法,而是视力= = 传送门: poj:http:// ...

  2. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  3. MVVM test

    示例代码 public class RegisterUserViewModel { public UserInfo userInfo { get; set; } public ICommand Cli ...

  4. Apache Flink 1.9.0版本新功能介绍

    摘要:Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时,提供支持流处理和批处理两种类型应用的功能.目前,Apache Flink 1.9 ...

  5. Android 之 BroadcaseReceiver

    1.在AndroidManifest.xml中注册 <receiver android:name=".MyReceiver"> <intent-filter &g ...

  6. Window和Mac下端口占用情况及处理方式

    1. 在Mac下端口占用的情况: 找到占用的进程并杀掉: 1.查看端口占用进程 sudo lsof -i :8880 可以看到进程的PID 2.杀掉进程 sudo kill -9 4580(4580为 ...

  7. maven 工程搭建

    使用Maven建立一个Quite start 项目 2.命名卫 bhz-parent 3.groupid 为:  bhz 4.artifactId 为: bhz-parent package:为空不填 ...

  8. iOS开发NSFetchedResultsController的使用CoreData和TableView数据同步更新

    1.效果 2.代码 #import "ViewController.h" #import "Student+CoreDataProperties.h" #def ...

  9. diskpart

    比如格式化成fat32 format fs=fat32 quick 比鼠标方便 如何使用: 打开cmd输入diskpart进入命令 首先 list disk 然后 然后 clean 然后 create ...

  10. SpringMVC学习总结

    SpringMVC部分重点组建介绍 前端处理器(DispatcherServlet):接受请求,响应结果,是SpringMVC的核心 处理映射器(HandlerMapping):根据URL去查找处理器 ...