1017 - Exact cover

时间限制:15秒 内存限制:128兆

自定评测 6110 次提交 3226 次通过
题目描述
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)的更多相关文章

  1. [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 ...

  2. HUST 1017 Exact cover dance links

    学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...

  3. HUST 1017 - Exact cover (Dancing Links 模板题)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...

  4. Dancing Link --- 模板题 HUST 1017 - Exact cover

    1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...

  5. hustoj 1017 - Exact cover dancing link

    1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...

  6. HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

    题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...

  7. (简单) 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 ...

  8. [HUST 1017] Exact cover

    Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6012 Solved: 3185 DESCRIP ...

  9. 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 ...

随机推荐

  1. hive UDF函数

    —虽然Hive提供了很多函数,但是有些还是难以满足我们的需求.因此Hive提供了自定义函数开发 —自定义函数包括三种UDF.UADF.UDTF —UDF(User-Defined-Function) ...

  2. JSF 2 radio buttons example

    In JSF, "h:selectOneRadio" tag is used to render a set of HTML input element of type " ...

  3. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

  4. HDU 3665 Seaside (最短路,Floyd)

    题意:给定一个图,你家在0,让你找出到沿海的最短路径. 析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的. 代码如下: #pragma comment(linker ...

  5. <select>改造成<s:select>实现表单的回显功能

    初始: <select name="viewType"> <option value="0">全部主题</option> & ...

  6. UVA1395

    // UVa1395 Slim Span // Rujia Liu #include<cstdio> #include<cmath> #include<cstring&g ...

  7. 单例模式总结(Java版)

    1:懒汉的设计模式,在第一次调用的时候才完成相关的初始化操作 懒汉式是典型的时间换空间,就是每次获取实例都会进行判断,看是否需要创建实例,浪费判断的时间.当然,如果一直没有人使用的话,那就不会创建实例 ...

  8. Galera 10.0.20 on CentOS 6.6

    Galera 10.0.20 on CentOS 6.6 0.使用场景 数据库软件:mariadb-galera-10.0.20-linux-x86_64.tar.gz 集群管理:galera-3-2 ...

  9. Wps的ppt里 让图片按顺序出现 就是点击一下 出现一张照片

    基本操作能够用两种方法来实现: 方法一.每页幻灯片插入一张图片,幻灯片默认就是单击鼠标切换幻灯片的,所以不用再做其它设置. 方法二.在一页幻灯片中插入多张图片,全选图片(插入图片后,点击图片,Ctrl ...

  10. 【Winform】Winform 制作一键发布web

    前言 最近web网站有个需要,就是打包给客户,客户能够自己手动的进行傻瓜式的安装发布web.找了很多资料,其中涉及到 文件解压 IIS操作 数据库还原 等. 发现现在就主要是两种解决方案: ①:使用V ...