HUST 1017 Exact cover 解题报告(Dancing Link)

11 篇文章 0 订阅

1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 5558 Solved: 2966
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。队友找了论文和题目,就顺便刷下。
    论文的话可以在这里下载: http://pan.baidu.com/s/1pJDboKj
    这题是裸题,直接用模板就好了,建议敲几遍模板。代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
 
const int M=1024*110;
const int N=1024;
 
int l[M], r[M], d[M], u[M], col[M], row[M], h[N], res[N], control[N];
int dcnt = 0;
 
inline void addnode(int &x)
{
	++x;
	r[x]=l[x]=u[x]=d[x]=x;
}
 
inline void insert_row(int rowx, int x)
{
	r[l[rowx]]=x;
	l[x]=l[rowx];
	r[x]=rowx;
	l[rowx]=x;
}
 
inline void insert_col(int colx, int x)
{
	d[u[colx]]=x;
	u[x]=u[colx];
	d[x]=colx;
	u[colx]=x;
}
 
void dlx_init(int cols)
{
	memset(h, -1, sizeof(h));
	memset(control, 0, sizeof(control));
	dcnt=-1;
	addnode(dcnt);
 
	for(int i=1;i<=cols;++i)
	{
		addnode(dcnt);
		insert_row(0, dcnt);
	}
}
 
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];
			control[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;
			control[col[j]]++;
		}
	l[r[c]]=c;
	r[l[c]]=c;
}
 
bool DLX(int deep)
{
	if(r[0]==0)
	{
		printf("%d", deep);
		for(int i=0;i<deep;i++) printf(" %d", res[i]);
		puts("");
		return true;
	}
 
	int min=M, tempc;
	for(int i=r[0];i!=0;i=r[i])
		if(control[i]<min)
		{
			min=control[i];
			tempc=i;
		}
	remove(tempc);
	for(int i=d[tempc];i!=tempc;i=d[i])
	{
		res[deep]=row[i];
		for(int j=r[i];j!=i;j=r[j]) remove(col[j]);
		if (DLX(deep+1)) return true;
		for(int j=l[i];j!=i;j=l[j]) resume(col[j]);
	}
	resume(tempc);
	return false;
}
 
inline void insert_node(int x, int y)
{
	control[y]++;
	addnode(dcnt);
	row[dcnt]=x;
	col[dcnt]=y;
	insert_col(y, dcnt);
	if(h[x]==-1)
		h[x]=dcnt;
	else
		insert_row(h[x], dcnt);
}
 
int main()
{
//	freopen("in.txt", "r", stdin);
	int n, m;
	while(~scanf("%d%d", &n, &m))
	{
		dlx_init(m);
		for(int i=1;i<=n;++i)
		{
			int k, x;
			scanf("%d", &k);
			while(k--)
			{
				scanf("%d", &x);
				insert_node(i, x);
			}
		}
		if (!DLX(0)) puts("NO");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值