1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 6751 Solved: 3519
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

这大概就是DLX的模板题了。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=N*N;
int L[M],R[M],U[M],D[M];
int H[N],C[N],row[M],col[M];
int cnt,n,m,ans[N];
struct DLX{
void Init(int n,int m){
for(int i=;i<=m;i++){
L[i]=i-;R[i]=i+;
C[i]=;U[i]=D[i]=i;
}cnt=L[]=m;R[m]=;
for(int i=;i<=n;i++)H[i]=;
}
void Link(int r,int c){
++cnt;C[c]++;
col[cnt]=c;row[cnt]=r;
U[D[c]]=cnt;U[cnt]=c;
D[cnt]=D[c];D[c]=cnt;
if(H[r]){
R[cnt]=R[H[r]];L[cnt]=H[r];
L[R[H[r]]]=cnt;R[H[r]]=cnt;
}
else H[r]=L[cnt]=R[cnt]=cnt;
}
void Delete(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])
--C[col[j]],U[D[j]]=U[j],D[U[j]]=D[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=L[i];j!=i;j=L[j])
++C[col[j]],U[D[j]]=j,D[U[j]]=j;
}
bool Dance(int dep){
if(!R[]){
printf("%d",dep);
for(int i=;i<=dep;i++)
printf(" %d",ans[i]);
puts("");return true;
}int p=;
for(int i=R[];i;i=R[i])
if(!p||C[p]>C[i])p=i;
Delete(p);
for(int i=D[p];i!=p;i=D[i]){
ans[dep+]=row[i];
for(int j=R[i];j!=i;j=R[j])Delete(col[j]);
if(Dance(dep+))return true;
for(int j=L[i];j!=i;j=L[j])Resume(col[j]);
}
Resume(p);
return false;
}
}dlx; int main(){
while(scanf("%d%d",&n,&m)!=EOF){
dlx.Init(n,m);
for(int i=;i<=n;i++){
int k,x;
scanf("%d",&k);
while(k--){
scanf("%d",&x);
dlx.Link(i,x);
}
}if(!dlx.Dance())
printf("NO\n");
}
return ;
}

搜索(DLX):HOJ 1017 - Exact cover的更多相关文章

  1. [DLX] hust 1017 Exact cover

    题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...

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

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

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

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

  4. hustoj 1017 - Exact cover dancing link

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

  5. HUST 1017 Exact cover (Dancing links)

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

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

  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(DLX精确覆盖)

    Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...

  9. [HUST 1017] Exact cover

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

随机推荐

  1. springmvc xml 空模板

    <?xml version="1.0" encoding="UTF-8"?><!-- Bean头部 --><beans xmlns ...

  2. html获取gps坐标

    <script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(n ...

  3. Jquery 判断滚动条到达顶部或底部

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. asp.net利用ajax和jquery-ui实现进度条

    前台用ajax不停进行查询,直到任务完成.进度条用的是jquery-ui.后台用一般处理程序处理相应,进度信息保存在HttpContext.Application中. 代码作为简单示例,实际应用时应对 ...

  5. java.util.Random深入理解

    java.util.Random next方法的原理 比较好的参考文档: http://isky001.iteye.com/blog/1339979 package random.utilrandom ...

  6. CentOS 7重装mysql编译过程报错解决方法

    错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...

  7. C# 多个线程一直跑着While(true)

    在使用多线程的时候,开了多个线程一直在While(true),会造成CPU占用很高.这时候要在线程内加入一句Thread.Sleep(1),让他稍微睡一下.就不会消耗那么多CPU了. 代码: Thre ...

  8. mysql数据库中编码问题(续)

    其实之前的数据库中文乱码问题并没有彻底的解决,虽然在网页上显示正常,但是在数据库中却是乱码,虽然用户看起来没问题,但是自己就遭罪了,而且也是个极大的问题 究其原因,是没注意到一点,就是数据库中表的结构 ...

  9. Jmeter软件测试2--http接口测试

    上次利用Jmeter进行了webservice接口的测试,本次利用Jmeter进行http接口的测试 1.新建线程组 2.新建配置文件 3.新建http请求 4.配置动态请求 4.查看测试结果

  10. SGU 171.Sarov zones

    简单的贪心.优先weight最大的,优先匹配Q值大的地区 code #include <iostream> #include <algorithm> #include < ...