Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 6012 Solved: 3185
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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MaxNode 100010
#define MaxN 1010
#define MaxM 1010 struct DLX
{
int n,m,size;
int U[MaxNode],D[MaxNode],R[MaxNode],L[MaxNode];
int Row[MaxNode],Col[MaxNode];
int H[MaxN],S[MaxM];
int ansd, ans[MaxN]; void Init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=;i<=m;i++)
{
S[i]=;
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
}
R[m]=;L[]=m;
size=m;
for(int i=;i<=n;i++)
H[i]=-;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
U[size]=U[c];
D[U[c]]=size;
D[size]=c;
U[c]=size;
if(H[r]==-) H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[L[H[r]]]=size;
R[size]=H[r];
L[H[r]]=size;
}
}
void Remove(int c)
{
L[R[c]]=L[c];
R[L[c]]=R[c];
for(int i=D[c];i!=c;i=D[i])
{
for(int j=R[i];j!=i;j=R[j])
{
U[D[j]]=U[j];
D[U[j]]=D[j];
S[Col[j]]--;
}
}
}
void Resume(int c)
{
for(int i = U[c];i != c;i = U[i])
{
for(int j = L[i];j != i;j = L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
L[R[c]] =c;
R[L[c]] =c;
}
bool Dance(int d)
{
if(R[]==)
{
ansd=d;
return ;
}
int c=R[];
for(int i=R[];i!=;i=R[i])
if(S[i]<S[c]) c=i;
Remove(c);
for(int i=D[c];i!=c;i=D[i])
{
ans[d]=Row[i];
for(int j=R[i];j!=i;j=R[j]) Remove(Col[j]); //移除
if(Dance(d+)) return ;
for(int j=L[i];j!=i;j=L[j]) Resume(Col[j]); //回标
}
Resume(c);
return ;
}
}g;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
g.Init(n,m);
for(int i=;i<=n;i++)
{
int num,j;
scanf("%d",&num);
while(num--)
{
scanf("%d",&j);
g.Link(i,j);
}
}
if(!g.Dance()) printf("NO\n");
else
{
printf("%d",g.ansd);
for(int i=;i<g.ansd;i++)
printf(" %d",g.ans[i]);
printf("\n");
}
}
return ;
}

[HUST 1017] Exact cover的更多相关文章

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

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

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

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

  3. HUST 1017 Exact cover (Dancing links)

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

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

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

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

  7. HUST 1017 Exact cover dance links

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

  8. [DLX] hust 1017 Exact cover

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

  9. hustoj 1017 - Exact cover dancing link

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

随机推荐

  1. hibernate案例 测试代码

    测试staff数据表连接到maeclipse 在staff中插入一行 package com.hibernate.test; import org.hibernate.Session; import ...

  2. poj 3565 uva 1411 Ants KM算法求最小权

    由于涉及到实数,一定,一定不能直接等于,一定,一定加一个误差<0.00001,坑死了…… 有两种事物,不难想到用二分图.这里涉及到一个有趣的问题,这个二分图的完美匹配的最小权值和就是答案.为啥呢 ...

  3. Linux作业控制

    在Linux中,利用Shell的作业控制是比较常用的操作,在这一节中我们将探究作业控制相关的操作.为了方便我们查看区分不同的进行,我们编写如下程序,其功能是每间隔2秒输出一次自己的编号. /* ** ...

  4. Java知识总结--CoreJava

    在网上看到的关于Java的知识总结,觉得很受用,分享给大家..... 如果有什么错误,也欢迎指正批评. 1 简述下java基本数据类型及所占位数,java基本数据类型:4类8种 整数类型:byte(1 ...

  5. mysql 之权限介绍

    转自:http://tech.it168.com/a2010/0114/837/000000837456_all.shtml 一.MySQL授权表概述首先从全局开始,如果全局的是允许的,即在 user ...

  6. [DevExpress]SplitContainerControl使用小计

    1.修改成纵向分割 Horizontal = false; 2.设置伸缩箭头 3.固定某个PANEL大小 最大化后依然保持着比例 4.隐藏某个PANEL splitContainerControl1. ...

  7. 图片裁切插件jCrop的使用心得(一)

    之前,项目经理为了提升用户体验让我在之前图片上传功能的基础上实现图片的裁切功能,作为一个前端小白来说听了这个需求心里一紧,毕竟没有做过,于是跟项目经理商量要先做下调研.在一上午的调研中发现了jCrop ...

  8. php中 __autoload函数 自动加载类文件机制

    魔术函数,自动加载机制function __autoload($class_name) {   //自动传递的是类名$path = str_replace('_', '/', $class_name) ...

  9. Python的设计模式学习

    1.工厂模式 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class ADD(): def getResult(self,*args): ret ...

  10. Spring MVC常用的注解

    @Controller @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为 类名称开头字母小写,你也可以自己指定,如下 方法一: @Controller ...