hustoj 1017 - Exact cover dancing link
1017 - Exact cover
Time Limit: 15s Memory Limit: 128MB
- 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
- HINT
- SOURCE
- dupeng
- dancing link裸題,就不細說了。
-
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define MAXD 100000
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=(char)getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=(char)getchar(),ch<='' && ch>='');
return x*(flag?-:);
} int n,m;
struct DLX_t
{
static const int maxd=;
static const int maxn=;
static const int maxm=;
int L[maxd],R[maxd],U[maxd],D[maxd];
int rw[maxd];
int head;
int topd;
int chd[maxm];
int col[maxd];
int tt[maxm];
int n,m;
vector<int> res;
void init(int nn,int mm)
{
n=nn;m=mm;
topd=;
memset(L,,sizeof(L));
memset(R,,sizeof(R));
memset(D,,sizeof(D));
memset(U,,sizeof(U));
memset(tt,,sizeof(tt));
res.clear();
head=++topd;
L[head]=R[head]=head;
for (int i=;i<=m;i++)
{
chd[i]=++topd;
col[chd[i]]=i;
rw[chd[i]]=;
R[chd[i]]=head;
L[chd[i]]=L[head];
R[chd[i]]=head;
L[R[chd[i]]]=chd[i];
R[L[chd[i]]]=chd[i];
U[chd[i]]=D[chd[i]]=chd[i];
}
}
void Add_row(int r,const vector<int> &vec)
{
int i;
int nowh;
int now;
for (i=;i<(int)vec.size();i++)
{
now=++topd;
rw[now]=r;
col[now]=vec[i];
tt[vec[i]]++;
U[now]=U[chd[vec[i]]];
D[now]=chd[vec[i]];
D[U[now]]=now;
U[D[now]]=now;
}
L[U[chd[vec[]]]]=R[U[chd[vec[]]]]=U[chd[vec[]]];
nowh=U[chd[vec[]]];
for (i=;i<(int)vec.size();i++)
{
R[U[chd[vec[i]]]]=nowh;
L[U[chd[vec[i]]]]=L[nowh];
L[R[U[chd[vec[i]]]]]=U[chd[vec[i]]];
R[L[U[chd[vec[i]]]]]=U[chd[vec[i]]];
}
}
void Finish()
{
vector<int> res2=res;
sort(res2.begin(),res2.end());
printf("%d",(int)res2.size());
for (int i=;i<(int)res2.size();i++)
printf(" %d",res2[i]);
printf("\n");
}
void cover(int c)
{
int i,j;
R[L[chd[c]]]=R[chd[c]];
L[R[chd[c]]]=L[chd[c]];
for (i=D[chd[c]];i!=chd[c];i=D[i])
{
for (j=R[i];j!=i;j=R[j])
{
tt[col[j]]--;
U[D[j]]=U[j];
D[U[j]]=D[j];
}
}
}
void resume(int c)
{
int i,j;
R[L[chd[c]]]=chd[c];
L[R[chd[c]]]=chd[c];
for (i=D[chd[c]];i!=chd[c];i=D[i])
{
for (j=R[i];j!=i;j=R[j])
{
tt[col[j]]++;
U[D[j]]=j;
D[U[j]]=j;
}
}
}
bool dfs()
{
int now=head;
if (L[now]==now)
{
Finish();
return true;
}
int bst=INF,bi=-;
int i,j;
for (i=R[head];i!=head;i=R[i])
{
if (tt[col[i]]<bst)
{
bst=tt[i];
bi=i;
}
}
cover(col[bi]);
for (i=D[bi];i!=bi;i=D[i])
{
res.push_back(rw[i]);
for (j=R[i];j!=i;j=R[j])
cover(col[j]);
if (dfs())return true;
res.pop_back();
for (j=R[i];j!=i;j=R[j])
resume(col[j]);
}
resume(col[bi]);
return false;
}
}DLX;
vector<int> vec;
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
while (~scanf("%d%d",&n,&m))
{
DLX.init(n,m);
for (i=;i<=n;i++)
{
scanf("%d",&y);
vec.clear();
for (j=;j<=y;j++)
{
scanf("%d",&x);
vec.push_back(x);
}
sort(vec.begin(),vec.end());
DLX.Add_row(i,vec);
}
if (!DLX.dfs())
printf("NO\n");
}
return ;
}
hustoj 1017 - Exact cover dancing link的更多相关文章
- HUST 1017 Exact cover (Dancing links)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 There is an N*M matrix with only 0 ...
- [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 ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- 搜索(DLX):HOJ 1017 - Exact cover
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 ...
- 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 ...
随机推荐
- bash手册 之重定向原理与实现
http://www.gnu.org/software/bash/manual/bashref.html#Redirections http://www.cnblogs.com/weidagang20 ...
- 在WinForm中使用委托来在其他线程中改变控件的显示
假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...
- NDK开发之数组操作
JNI把Java数组当作引用类型来处理,JNI提供了必要的函数来访问和处理Java数组. 下面一个一个来看. 1.创建数组 我们可以使用NewArray函数在原生代码中创建数组实例,其中可以是Int. ...
- Android(java)学习笔记189:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。
导致这个错误的原因是工程重名了: 并不是仅仅指文件夹重名,相信很多人也曾经修改过文件夹的名称,可惜没什么用处,关键是修改工程里面的一个文件!也就是.project这个文件! 用记事本打开,修改一下&l ...
- 当append里面的标签显示不出来的时候,用下面的方式做
$("#result_td").append(tem1+tem3) $("#result_td").append($(tem1+tem3))
- php5魔术函数、魔术常量
魔术函数 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct ...
- 标准web架构分层
标准Web系统的架构分层 转载:http://blog.csdn.net/yinwenjie http://blog.csdn.net/yinwenjie/article/details/464 ...
- (转)Asp.Net 请求处理机制
原文:http://www.cnblogs.com/cilence/archive/2012/05/28/2520712.html Asp.Net 请求处理机制 前言 我们都知道Web请求响应是基 ...
- O-C相关04:类方法的概述与定义和调用
类方法的概述与定义和调用 1, 类方法的概述 类方法(class method)在其他编程语言中常常称为静态方法(例如 Java 或 C# 等). 与实例方法不同的是,类方法只需要使用类名即可调用, ...
- 这次是C#中的接口
接口的出现,是为了解决C#中不允许多重继承的问题. 1.什么是接口? 我觉得可以把接口理解为对一组方法声明进行的统一命名,但这些方法没有提供任何实现. 通过接口,就可以对方法进行统一管理,避免了在每种 ...