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 ...
随机推荐
- Spring+Quartz 整合一:常规整合
步骤一: 定时任务需要一个配置文件(spring-mvc-timeTask.xml 随便起名),将其在web.xml中加载 <context-param> <param-name&g ...
- 开发程序过程中遇到的调用Web Api小问题
在用Umbraco Web Api开发程序时,前端使用React调用Web Api 当时是有一个页面Search.cshtml,把用React产生的脚本代码,在这个页面进行引用 写了一个Api, 调用 ...
- ASP.NET面试题总结
1.ASP.NET中的身份验证有那些?你当前项目采用什么方式验证请解释ASP.NET身份验证模式包括Windows.Forms(窗体).Passport(护照)和None(无). 1.Windows身 ...
- UVaLive 6862 Triples (数学+分类讨论)
题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...
- java tools: jstack
SYNOPSIS jstack [ option ] pidclick here to see other detail If the given process is running on a 64 ...
- jquery 鼠标经过放大图片
jquery.elevatezoom.js文件请到演示文件查看 演示 JavaScript Code <script type="text/javascript"> $ ...
- SQL自定义函数split分隔字符串
SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...
- eclipse scons 使用指南
http://sconsolidator.com/projects/sconsolidator/wiki/Getting_Started Add SCons support to an existin ...
- JHipster的安装
JHipster GitHub地址:https://jhipster.github.io/ 刚开始接触JHipster,理解还不深,此次随笔只是把自己对JHipster的所学记录一下,也算是一种知识的 ...
- cocos2d-x CCArray
转自:http://blog.csdn.net/onerain88/article/details/8164210 1. CCArray只是提供了一个面向对象的封装类 其继承于CCObject类(CC ...