HUST1017(KB3-A Dancing links)
1017 - Exact cover
Time Limit: 15s Memory Limit: 128MB
DESCRIPTION
- There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find out the selected rows.
INPUT
- There are multiply test cases. First line: two integers N, M; The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.
OUTPUT
- First output the number of rows in the selection, then output the index of the selected rows. If there are multiply selections, you should just output any of them. If there are no selection, just output "NO".
SAMPLE INPUT
-
6 7
3 1 4 7
2 1 4
3 4 5 7
3 3 5 6
4 2 3 6 7
2 2 7
SAMPLE OUTPUT
-
3 2 4 6
HINT
SOURCE
- dupeng
- 精确覆盖问题,dancing links模板
-
//2017-03-09
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
const int M = ;
const int maxnode = N*M; struct DLX
{
int n, m, sz;
int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
int H[N], S[M];
int ansd, ans[N]; void init(int nn, int mm)
{
n = nn; m = mm;
for(int i = ; i <= m; i++)
{
S[i] = ;
U[i] = D[i] = i;
L[i] = i-;
R[i] = i+;
}
R[m] = ; L[] = m;
sz = m;
for(int i = ; i <= n; i++)H[i] = -;
} void link(int r, int c)
{
++S[Col[++sz] = c];
Row[sz] = r;
D[sz] = D[c];
U[D[c]] = sz;
U[sz] = c;
D[c] = sz;
if(H[r] < )H[r] = L[sz] = R[sz] = sz;
else{
R[sz] = R[H[r]];
L[R[H[r]]] = sz;
L[sz] = H[r];
R[H[r]] = sz;
}
} 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])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[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])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
} bool Dance(int d)
{
if(R[] == )
{
printf("%d ", d);
for(int i = ; i < d; i++)
if(i == d-)printf("%d\n", ans[i]);
else printf("%d ", ans[i]);
return true;
}
int c = R[];
for(int i = R[]; i != ; i = R[i])
if(S[i] < S[c])c = i;
Remove(c);
for(int i = D[c]; i != c; 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(c);
return false;
}
}dlx; int main()
{
int n, m, c, tmp;
while(scanf("%d%d", &n, &m)!=EOF)
{
dlx.init(n, m);
for(int i = ; i <= n; i++)
{
scanf("%d", &c);
for(int j = ; j < c; j++)
{
scanf("%d", &tmp);
dlx.link(i, tmp);
}
}
if(!dlx.Dance())printf("NO\n");
} return ;
}
HUST1017(KB3-A Dancing links)的更多相关文章
- HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题
题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- 【转】Dancing Links题集
转自:http://blog.csdn.net/shahdza/article/details/7986037 POJ3740 Easy Finding [精确覆盖基础题]HUST1017 Exact ...
- 【转】Dancing Links精确覆盖问题
原文链接:http://sqybi.com/works/dlxcn/ (只转载过来一部分,全文请看原文,感觉讲得很好~)正文 精确覆盖问题 解决精确覆盖问题 舞蹈步骤 效率分析 ...
- Dancing Links 专题总结
算法详细:Dancing Links博客 1.精确覆盖: ZOJ3209 Treasure Map HUST1017 Exact cover POJ3074 Sudoku 2.可重复覆盖: HDU22 ...
- dancing links 题集转自夏天的风
POJ3740 Easy Finding [精确覆盖基础题] HUST1017 Exact cover [精确覆盖基础] HDOJ3663 Power Stations [精确覆盖] Z ...
- Dancing Links and Exact Cover
1. Exact Cover Problem DLX是用来解决精确覆盖问题行之有效的算法. 在讲解DLX之前,我们先了解一下什么是精确覆盖问题(Exact Cover Problem)? 1.1 Po ...
- 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题
精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 例如:如下的矩阵 就包含了这样一个集合(第1.4.5行) 如何利用给定的矩阵求出相应的行的集合 ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
随机推荐
- Java并发编程总结5——ThreadPoolExecutor
一.ThreadPoolExecutor介绍 在jdk1.8中,构造函数有4个.以 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, ...
- OSX10.12搭建IPv6本地环境测试APP
前记 最近刚换了工作,生活终于又安定下来了,又可以更博了 正文 最近公司在上线APP(整体全是用JS去写的,就用了我原生的一个控制器),然后APP就去上线,就被苹果巴巴给拒了.通过阅读苹果回复的邮件, ...
- solr初识
参考资料http://blog.csdn.net/l1028386804/article/details/70199983
- 【转】iOS中属性与成员变量的区别
[转载自并整理 http://blog.csdn.net/itianyi/article/details/8618128] 一.类Class中的属性property 在ios第一版中,我们为输出口同时 ...
- python连接mysql数据库简单例子
今天用pyhton2连接本地的mysql数据库,总的来说比较简单,但还是遇到一些小问题 代码如下: # -*- coding: utf-8 -*- import os import MySQLdb i ...
- POJ 2606
#include<iostream> #include<set> #include<stdio.h> #include<math.h> #include ...
- (转)python之from_bytes、to_bytes
原文:https://blog.csdn.net/PYTandFA/article/details/78741339 https://python3-cookbook.readthedocs.io/z ...
- android开发学习——day4
自己手动创建空活动,创建和加载布局,效果:界面中出现靠上对齐的button 在活动中使用Toast,效果:对点击按钮做出响应 在活动中使用menu,效果:界面中出现菜单,并且点击对应选项会有响应 De ...
- Resolve类中错误体系的处理
标红的表示要走3步骤,也就是: final List<MethodResolutionPhase> methodResolutionSteps = List.of( MethodResol ...
- ilspy反编译
资料: http://www.cnblogs.com/JamesLi2015/archive/2011/09/08/2170519.html 软件: http://ilspy.net/