码迷,mamicode.com
首页 > 其他好文 > 详细

HUST1017----Exact cover

时间:2015-01-06 10:06:46      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:精确覆盖

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: 405045132@qq.com 
    > 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;
}



HUST1017----Exact cover

标签:精确覆盖

原文地址:http://blog.csdn.net/guard_mine/article/details/42433201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!