搜索(DLX):HOJ 1017 - Exact cover
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搜索(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 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
这大概就是DLX的模板题了。1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdio> 6 using namespace std; 7 const int maxnode=100010; 8 const int maxn=1010; 9 int n,m,k,x; 10 struct Dancing_Links_X 11 { 12 int L[maxnode],R[maxnode],U[maxnode],D[maxnode],H[maxn],sum[maxn],Row[maxnode],Col[maxnode],ans[maxn],cnt; 13 void Init(int n,int m){ 14 for(int i=0;i<=m;i++){ 15 L[i]=i-1;R[i]=i+1;sum[i]=0;U[i]=i;D[i]=i; 16 } 17 L[0]=m;R[m]=0; 18 cnt=m; 19 for(int i=1;i<=n;i++) 20 H[i]=0; 21 } 22 void Link(int r,int c) 23 { 24 Row[++cnt]=r; 25 Col[cnt]=c; 26 sum[c]++; 27 U[D[c]]=cnt; 28 D[cnt]=D[c]; 29 D[c]=cnt; 30 U[cnt]=c; 31 32 if(!H[r]) 33 H[r]=L[cnt]=R[cnt]=cnt; 34 else{ 35 L[R[H[r]]]=cnt; 36 R[cnt]=R[H[r]]; 37 R[H[r]]=cnt; 38 L[cnt]=H[r]; 39 } 40 } 41 42 void Delete(int c) 43 { 44 R[L[c]]=R[c];L[R[c]]=L[c]; 45 for(int i=D[c];i!=c;i=D[i]) 46 for(int j=R[i];j!=i;j=R[j]) 47 --sum[Col[j]],D[U[j]]=D[j],U[D[j]]=U[j]; 48 } 49 50 void Resume(int c) 51 { 52 for(int i=U[c];i!=c;i=U[i]) 53 for(int j=L[i];j!=i;j=L[j]) 54 ++sum[Col[j]],D[U[j]]=j,U[D[j]]=j; 55 R[L[c]]=c;L[R[c]]=c; 56 } 57 58 bool Solve(int dep) 59 { 60 if(!R[0]){ 61 printf("%d",dep-1); 62 for(int i=1;i<dep;i++) 63 printf(" %d",ans[i]); 64 printf("\n"); 65 return true; 66 } 67 int p=-1; 68 for(int i=R[0];i;i=R[i]) 69 if(p==-1||sum[p]>sum[i]) 70 p=i; 71 Delete(p); 72 for(int i=D[p];i!=p;i=D[i]){ 73 ans[dep]=Row[i]; 74 for(int j=R[i];j!=i;j=R[j])Delete(Col[j]); 75 if(Solve(dep+1)) 76 return true; 77 for(int j=L[i];j!=i;j=L[j])Resume(Col[j]); 78 } 79 Resume(p); 80 return false; 81 } 82 83 }DLX; 84 int main() 85 { 86 while(scanf("%d%d",&n,&m)==2) 87 { 88 DLX.Init(n,m); 89 for(int i=1;i<=n;i++) 90 { 91 scanf("%d",&k); 92 while(k--){ 93 scanf("%d",&x); 94 DLX.Link(i,x); 95 } 96 } 97 if(!DLX.Solve(1)) 98 puts("NO"); 99 } 100 return 0; 101 }
以上是关于搜索(DLX):HOJ 1017 - Exact cover的主要内容,如果未能解决你的问题,请参考以下文章
搜索(DLX):HDU 3663 Power Stations
c_cpp HOJ - 302:最大平均值[AC]; http://hoj.twbbs.org/judge/judge/submission/21872