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 ...
随机推荐
- BUILD_BUG_ON 的解释
知乎上个问题<C 语言有什么奇技淫巧?>排名第一的是一个“抖机灵”的答案. C有一个鲜为人知的运算符叫”趋向于”, 写作“-->”.比如说如果要实现一个倒数的程序,我们可以定义一个变 ...
- HDU 5776 sum (模拟)
sum 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5776 Description Given a sequence, you're asked ...
- C#多线程的几种实现方法
1.最简单的多线程 using System; using System.Threading; namespace ThreadTest1 { public class SimpleThread { ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CentOS 搭建LNMP服务器和LAMP服务器
CentOS 搭建LNMP服务器 方法一:yum安装 1.更新YUM源 wget http://www.atomicorp.com/installers/atomic #下载atomic自动更新Y ...
- Linux命令行访问网页
找到个好资料,备份行: http://hi.baidu.com/oyvfhp/blog/item/3aa5ced5b40563d351da4bb0.html CURL --- 命令行浏览器 这东西 ...
- POS机刷卡失败的郁闷事
6月13号在老家的金店为未来老婆买首饰,刷的工行POS机. 结果8000多RMB从卡里扣了,商家又没收到钱……POS机提示“交易超时”…… 当天和商家到就近的工行,工行经理说他负责协调这事,只要钱到商 ...
- 在WinForm中使用Web Service来实现软件自动升级
来源:互联网 winform程序相对web程序而言,功能更强大编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术 ...
- wikioi 1214 线段覆盖
题目描述 Description 给定x轴上的N(0<N<100)条线段,每个线段由它的二个端点a_I和b_I确定,I=1,2,--N.这些坐标都是区间(-999,999)的整数.有些线段 ...
- IIS下的身份验证方式管理
设置.查看身份验证方式 #导航到某站点下: cd IIS:\Sites\DemoSite\DemoApp #启用站点test01下的Windows身份验证 Set-WebConfigurationPr ...