首页 > 其他 > 详细

hust 1017 DLX模板

时间:2014-02-08 09:39:32      阅读:394      评论:0      收藏:0      [点我收藏+]
Time Limit:15000MS   Memory Limit:131072KB   64bit IO Format:%lld & %llu

[]  [Go Back]  [Status]  

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

Source

dupeng

[]  [Go Back]  [Status]  


人生第一道DLX,抄个模板,慢慢理解。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-8 4:41:36
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
struct DLX{
	const static int SZ=1000*100+10;
	const static int COL=1010;
	int L[SZ],R[SZ],U[SZ],D[SZ],X[SZ],Y[SZ];
	int S[COL],H[COL];
	int cnt;
	int ans[COL],acnt;
	void del(int *L,int *R,int x){
		L[R[x]]=L[x];
		R[L[x]]=R[x];
	}
	void res(int *L,int *R,int x){
		L[R[x]]=R[L[x]]=x;
	}
	void init(int col){
		cnt=col+1;
		memset(H,-1,sizeof(H));
		memset(S,0,sizeof(S));
		for(int i=0;i<=col;i++){
			L[i]=i-1;
			R[i]=i+1;
			U[i]=D[i]=i;
		}
		L[0]=col;R[col]=0;
	}
	void add(int *L,int *R,int head,int now){
		int tail=L[head];
		L[now]=tail;
		R[now]=head;
		R[tail]=L[head]=now;
	}
	void insert(int x,int y){
		X[cnt]=x;
		Y[cnt]=y;
		add(U,D,y,cnt);
		if(H[x]<0)H[x]=L[cnt]=R[cnt]=cnt;
		else add(L,R,H[x],cnt);
		S[y]++;
		cnt++;
	}
	void remove(int y){
		if(y==0)return;
		for(int i=D[y];i!=y;i=D[i]){
			for(int j=R[i];j!=i;j=R[j]){
				del(U,D,j);
				S[Y[j]]--;
			}
		}
		del(L,R,y);
	}
	void resume(int y){
		if(y==0)return;
		for(int i=U[y];i!=y;i=U[i]){
			for(int j=L[i];j!=i;j=L[j]){
				res(U,D,j);
				S[Y[j]]++;
			}
		}
		res(L,R,y);
	}
	bool dfs(int k){
		if(R[0]==0){
			acnt=k;
			return 1;
		}
		int c=1;
		int mins=10000;
		for(int i=R[0];i!=0;i=R[i]){
			if(S[i]<mins){
				mins=S[i];
				c=i;
				if(mins==1)break;
			}
		}
		remove(c);
		for(int i=D[c];i!=c;i=D[i]){
			for(int j=R[i];j!=i;j=R[j])
				remove(Y[j]);
			ans[k]=X[i];
			if(dfs(k+1))return 1;
			for(int j=L[i];j!=i;j=L[j])
				resume(Y[j]);
		}
		resume(c);
		return 0;
	}
}dlx;
int n,m;
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     while(cin>>n>>m){
		 dlx.init(m);
		 for(int i=1;i<=n;i++){
			 int num,col;
			 cin>>num;
			 while(num--){
				 cin>>col;
				 dlx.insert(i,col);
			 }
		 }
		 if(dlx.dfs(0)){
			 printf("%d",dlx.acnt);
			 for(int i=0;i<dlx.acnt;i++)
				 printf(" %d",dlx.ans[i]);
			 puts("");
		 }
		 else puts("NO");
	 }
     return 0;
}


hust 1017 DLX模板

原文:http://blog.csdn.net/xianxingwuguan1/article/details/18977679

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!