HUST 1017 - Exact cover

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

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

HintSourcedupengDLX的模板题，关于这道题的原理请看：

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;
{
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;
{
++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++)
{
for(int j=1;j<=num;j++)
{
}
}
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;
}

1811 篇文章121 人订阅

0 条评论

相关文章

6710

32320

364100

8110

297110

11840

各种数论模板 不断更新 绝对精品

1.筛法求素数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inclu...

35190

洛谷P1351 联合权值(树形dp)

8820

EventBus是Guava的事件处理机制，是设计模式中的观察者模式（生产/消费者编程模型）的优雅实现，在应用中可以处理一些异步任务。对于事件监听和发布订阅模式...

27220

12420