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

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 
#include 
#include 
#include 
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


你可能感兴趣的:(ACM,搜索)