HUST1017----Exact cover

1017 - Exact cover

Time Limit: 15s Memory Limit:
128MB

Special Judge Submissions: 6023 Solved: 3188

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

精确覆盖模板题

/*************************************************************************
    > File Name: hust1017.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年01月05日 星期一 19时17分39秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;

int k;
int U[N * N], D[N * N], L[N * N], R[N * N];
int C[N * N], ans[N];
int cntr[N], cntc[N];
int head;
int row[N][N]; //表示第i行第j个为1的列是多少
int col[N][N]; //表示第i列第j个为1的行是多少
int n, m;

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];
			cntc[C[j]]--;
		}
	}
}

void resume(int c)
{
	L[R[c]] = c;
	R[L[c]] = c;
	for (int i = D[c]; i != c; i = D[i])
	{
		for (int j = R[i]; j != i; j = R[j])
		{
			U[D[j]] = j;
			D[U[j]] = j;
			cntc[C[j]]++;
		}
	}
}

bool dance()
{
	if (R[head] == head)
	{
		return true;
	}
	int c;
	int mins = 0x3f3f3f3f;
	for (int i = R[head]; i != head; i = R[i])
	{
		if (mins > cntc[i])
		{
			c = i;
			mins = cntc[i];
		}
	}
	remove(c);
	for (int i = D[c]; i != c; i = D[i])
	{
		ans[k++] = (i - 1) / m;
		for (int j = R[i]; j != i; j = R[j])
		{
			remove(C[j]);
		}
		if (dance())
		{
			return true;
		}
		for (int j = L[i]; j != i; j = L[j])
		{
			resume(C[j]);
		}
		k--;
	}
	resume(c);
	return false;
}

bool build()
{
	head = 0;
	for (int i = 0; i < m; ++i)
	{
		R[i] = i + 1;
		L[i + 1] = i;
	}
	R[m] = 0;
	L[0] = m;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j < cntr[i]; ++j)
		{
			R[row[i][j]] = row[i][j + 1];
			L[row[i][j + 1]] = row[i][j];
		}
		R[row[i][cntr[i]]] = row[i][1];
		L[row[i][1]] = row[i][cntr[i]];
	}
	for (int i = 1; i <= m; ++i)
	{
		for (int j = 1; j < cntc[i]; ++j)
		{
			D[col[i][j]]  = col[i][j + 1];
			U[col[i][j + 1]] = col[i][j];
		}
		D[col[i][cntc[i]]] = i;
		D[i] = col[i][1];
		U[i] = col[i][cntc[i]];
		U[col[i][1]] = i;
		if (cntc[i] == 0)
		{
			return false;
		}
	}
	return true;
}

int main()
{
	int tmp;
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 1; i <= m; ++i)
		{
			cntc[i] = 0;
		}
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &cntr[i]);
			for (int j = 1; j <= cntr[i]; ++j)
			{
				scanf("%d", &tmp);
				row[i][j] = tmp + i * m;
				cntc[tmp]++;
				col[tmp][cntc[tmp]] = tmp + i * m;
				C[tmp + i * m] = tmp;
			}
		}
		if (build())
		{
			k = 0;
			if (dance())
			{
				printf("%d", k);
				for (int i = 0; i < k; ++i)
				{
					printf(" %d", ans[i]);
				}
				printf("\n");
			}
			else
			{
				printf("NO\n");
			}
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}
时间: 2024-08-10 00:06:58

HUST1017----Exact cover的相关文章

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

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 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 o

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 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 e

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

1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 略 analyse: A Time complexity: O(n) Source code:  #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vecto

hustoj 1017 - Exact cover dancing link

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 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 e

(简单) 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 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. DLX精确覆盖的模板题...... 代码如下: //HUST 1

HUST 1017 Exact cover (Dancing links)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 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 o

HUST 1017 Exact cover DLX

题意:一个n×m行的矩阵,每个格子可能是 0 或者1  现在让你选择 几列  使得 每一列 有且只有一个1.(精确覆盖模板题) 解题思路: dancing links 模板 关于dancing links  献上几篇必看论文 :http://par.buaa.edu.cn/acm-icpc/filepool/r/35/ 板子用的kuangbin的板子(还是适牛的..) 解题思路: 1 // File Name: hust1017.cpp 2 // Author: darkdream 3 // C

Dancing Links - nuclear weapon for Exact Cover problems

http://www.cnblogs.com/grenet/p/3145800.htmlBest elaboration on dancing list I found. in Chinese. Traditional backtracing is a heuristic-like procedure. Say, there are N items to try with one by one, when N[i] is picked, all rest N[i + 1, N-1] items

[DLX] hust 1017 Exact cover

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

Dancing Links 专题总结

算法详细:Dancing Links博客 1.精确覆盖: ZOJ3209 Treasure Map HUST1017 Exact cover POJ3074 Sudoku 2.可重复覆盖: HDU2295 Radar FZU1686 神龙的难题