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. 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
- DLX的模板题,
-
关于这道题的原理请看:
http://www.cnblogs.com/grenet/p/3145800.html - 这里只给出代码
-
#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; }