前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HUST 1017 - Exact cover

HUST 1017 - Exact cover

作者头像
attack
发布2018-04-12 11:45:33
5550
发布2018-04-12 11:45:33
举报

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 7636 Solved: 3898

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.InputThere 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.OutputFirst 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

代码语言:javascript
复制
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

代码语言:javascript
复制
3 2 4 6

HintSourcedupengDLX的模板题,关于这道题的原理请看:

http://www.cnblogs.com/grenet/p/3145800.html这里只给出代码

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<bitset>
#define ls k<<1
#define rs k<<1|1
using namespace std;
const int MAXN=1000001;
inline void read(int &n)
{
	char c='+';int x=0;bool flag=0;
	while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
	while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
	n=flag==1?-x:x;
}
int U[MAXN],D[MAXN],L[MAXN],R[MAXN];// 上下左右 
int s[MAXN];// 每一列中1出现的次数
int row[MAXN],col[MAXN];//每个节点原本属于哪一行哪一列
int h[MAXN];// 行头
int n,m;
int size;// 总结点的数目 
void pre()
{
	for(int i=0;i<=m;i++)
	{
		s[i]=0;
		U[i]=D[i]=i;
		L[i]=i-1;R[i]=i+1;
	}	
	L[0]=m;R[m]=0;
	size=m;
	memset(h,-1,sizeof(h));
}
int ans[MAXN];
int ansnum;
void add(int r,int c)
{
	++s[col[++size]=c];
	row[size]=r;
	D[size]=D[c];
	U[D[c]]=size;
	U[size]=c;
	D[c]=size;
	if(h[r]<0)
		h[r]=L[size]=R[size]=size;
	else
	{
		R[size]=R[h[r]];
		L[R[h[r]]]=size;
		L[size]=h[r];
		R[h[r]]=size;
	}
}
void dele(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 re(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]]=D[U[j]]=j;
			++s[col[j]];
		}
	}
	L[R[c]]=R[L[c]]=c;
}
bool work(int deep)
{
	if(R[0]==0)
	{
		ansnum=deep;
		return 1;
	}
	int c=R[0];
	for(int i=R[0];i!=0;i=R[i])
	{
		if(s[c]<s[i])
			c=i;
	}
	dele(c);
	for(int i=D[c];i!=c;i=D[i])
	{
		ans[deep]=row[i];
		for(int j=R[i];j!=i;j=R[j])
			dele(col[j]);
		if(work(deep+1))	return true;
		for(int j=L[i];j!=i;j=L[j])
			re(col[j]);
	}
	re(c);
	return false;
}
int main()
{
	
	while(scanf("%d%d",&n,&m))
	{
		pre();
		for(int i=1;i<=n;i++)
		{
			int num;read(num);
			for(int j=1;j<=num;j++)
			{
				int pos;read(pos);
				add(i,pos);
			}
		}
		if(!work(0))
			printf("NO\n");
		else
		{
			printf("%d ",ansnum);
			for(int i=0;i<ansnum;i++)
				printf("%d ",ans[i]);
			printf("\n");
		}
	}
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档