HUST 1017 Exact cover (Dancing links)
1017 - Exact cover
时间限制:15秒 内存限制:128兆
- 题目描述
- 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.
- 输入
- 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.
- 输出
- 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".
- 样例输入
-
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 - 样例输出
-
3 2 4 6
- 提示
- 来源
- dupeng
本人智商奇低,看了三天才学会,模板验证题。
顺便一提这是正式转入C++后第一A。
#include<iostream>
#include<cstdio>
using namespace std; const int HEAD = ;
const int N = ;
int MAP[N][N];
int U[N * N],D[N * N],L[N * N],R[N * N],H[N * N],C[N * N],ANS[N * N]; void ini(int col);
bool dancing(int k);
void output(void);
void remove(int c);
void resume(int c);
int main(void)
{
int n,m,num,col;
int count,front,first; while(cin >> n >> m)
{
ini(m); count = m + ;
for(int i = ;i <= n;i ++)
{
cin >> num;
front = first = count;
while(num --)
{
cin >> col; U[count] = U[col];
D[count] = col;
L[count] = front;
R[count] = first; D[U[col]] = count;
U[col] = count;
R[front] = count; H[count] = i;
C[count] = col;
front = count;
count ++;
}
L[first] = count - ;
}
if(!dancing())
cout << "NO" << endl;
} return ;
} void ini(int col)
{
U[HEAD] = D[HEAD] = H[HEAD] = C[HEAD] = HEAD;
R[HEAD] = ;
L[HEAD] = col; int front = HEAD;
for(int i = ;i <= col;i ++)
{
U[i] = D[i] = i;
L[i] = front;
R[i] = HEAD;
R[front] = i;
front = i; C[i] = i;
H[i] = ;
}
} bool dancing(int k)
{
int c = R[HEAD];
if(c == HEAD)
{
output();
return true;
} remove(C[c]);
for(int i = D[c];i != c;i = D[i])
{
ANS[k] = H[i];
for(int j = R[i];j != i;j = R[j])
remove(C[j]);
if(dancing(k + ))
return true;
for(int j = L[i];j != i;j = L[j])
resume(C[j]);
}
resume(C[c]); return false;
} void output(void)
{
int i,j;
for(i = ;ANS[i];i ++);
cout << i - << " ";
for(j = ;j < i - ;j ++)
cout << ANS[j] << " ";
cout << ANS[j] << endl;
} void remove(int c)
{
R[L[c]] = R[c];
L[R[c]] = L[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];
}
} void resume(int c)
{
R[L[c]] = c;
L[R[c]] = c; for(int i = U[c];i != c;i = U[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = j;
U[D[j]] = j;
}
}
HUST 1017 Exact cover (Dancing links)的更多相关文章
- [ACM] HUST 1017 Exact cover (Dancing Links,DLX模板题)
DESCRIPTION There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- HUST 1017 Exact cover dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- hustoj 1017 - Exact cover dancing link
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...
- HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题
题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...
- (简单) HUST 1017 Exact cover , DLX+精确覆盖。
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- [HUST 1017] Exact cover
Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...
- HUST 1017 Exact cover(DLX精确覆盖)
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
随机推荐
- 分享一道我认为非常有思考价值JavaScript题目
这是一道综合性的题目,如果你能快速清晰的分析整理出来,那我相信你对JavaScript是有一定的理解的了.我会先将题目的图片截取出来,供大家思考,在结尾在给出我的分析过程和答案,作个总结. 好,废话不 ...
- [每日一题] 11gOCP 1z0-053 :2013-10-11 Flashback Data Archive属性.........................43
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12656897 正确答案:BD 闪回数据归档请参考:http://blog.csdn.net ...
- 谈谈 JavaScript 中的 this 指向问题
JavaScript 中的 this 为一个重难点,它不像静态语言 C#.Java 一样,就表示当前对象.而在 JS 中, this 是运行时确定,而并非定义时就已确定其值. 谈起 this ,必须少 ...
- python视频教程大全
python3英文视频教程(全87集) http://pan.baidu.com/s/1dDnGBvV python从入门到精通视频(全60集)链接:http://pan.baidu.com/s/1e ...
- [置顶] iOS开发规范
iOS代码编程规范 详细讲解代码该如何写,怎样写,如何规范. 什么样的代码是最美的,本文档会给你讲解 iOS代码编程规范........................................ ...
- 445port入侵具体解释
445port入侵具体解释 关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...
- 很值得学习的java 画图板源码
很值得学习的java 画图板源码下载地址:http://download.csdn.net/source/2371150 package minidrawpad; import java.awt.*; ...
- 一个奇怪的html上url参数问题
今天踩了一个坑 如xxx.com/xxx/xxx?code=+adfdf 我需要拿到 code=+adfdf 但是后台拿到的是 adfdf, 后来只能对 code的值进行 urlencode处理了
- Hibernate征途(六)之数量和关系映射
本来如果和关系模型一样,只需要一对一.一对多.多对多映射就够了,但是前面<Hibernate征途(四)之映射 序>中说到,对象模型中关联是有方向的,所以对一对多而言,就会产生一对多还是多对 ...
- springMVC1 springmvc的基础知识
springmvc第一天 springmvc的基础知识 springmvc课程安排: 第一天: 基础知识 springmvc框架(重点) mvc在b/s系统中应用方式 springmvc框架原理(Di ...