[HUST 1017] Exact cover
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
#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的更多相关文章
- 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组成的矩阵,是否 ...
- 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 ...
- (简单) 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(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> ...
- [DLX] hust 1017 Exact cover
题意: 给你N个包,要拿到M个东西(编号1~M每一个仅仅能有一个) 然后每一个包里有k个东西,每一个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" ...
- hustoj 1017 - Exact cover dancing link
1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 ...
随机推荐
- 19_高级映射:一对多查询(使用resultMap)
[需求] 查询订单以及订单明细的信息. 确定主查询表:订单表orders 确定关联查询表:订单明细表 orderdetail 在一对一查询的基础上添加订单明细表关联即可. [分析] 使用resultM ...
- CheckSum
1.What is Checksum? A check sum is basically a value that is computed from data packet to check its ...
- 百练_4120 硬币(DP)
描述 宇航员Bob有一天来到火星上,他有收集硬币的习惯.于是他将火星上所有面值的硬币都收集起来了,一共有n种,每种只有一个:面值分别为a1,a2… an. Bob在机场看到了一个特别喜欢的礼物,想买来 ...
- “Assign Random Colors” is not working in 3ds Max 2015
Go to Customize -> Preferences…-> General (tab) Uncheck “Default to By Layer for New Nodes”
- Xml通用操作类
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...
- 在Mac OS X中使用VIM开发STM32(1)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在我先前的博文⎣在Mac OS X中搭建STM32开发环境⎤中,我们在Mac中DIY出了最 ...
- USB初始化
//USB初始化void CFileManagerDlg::usbinit(){ #define BUFFER_SIZE 64 struct usb_bus *bus; struct usb_devi ...
- Qt-获取主机网络信息之QNetworkInterface
QNetworkInterface类提供了一个主机IP地址和网络接口的列表. QNetworkInterface表示了当前程序正在运行时与主机绑定的一个网络接口.每个网络接口可能包含0个或者多个IP地 ...
- 实时监听input输入框value值的变化
1.js 的 oninput & onpropertychange JS中的 oninput 事件在 IE9 以下版本不支持,需要使用 IE 特有的 onpropertychange 事件替代 ...
- 客户端(android,ios)与服务器通信
android,ios客户端与服务器通信为了便于理解,直接用PHP作为服务器端语言 其实就是一个 http请求响应的过程序,先从 B/S模式说起浏览器发起http请求,服务器响应请求,并把数据返回给浏 ...