HUST 1017 Exact Cover [DLX]

来源:互联网 发布:毕向东35天java教学 编辑:程序博客网 时间:2024/05/29 15:12

Exact cover
Time Limit: 15000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu

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


01覆盖裸题,其他都可以转化成该模型。
注意不要鲁莽地从C1开始找。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 1005;#define left(x) node[x].left#define right(x) node[x].right#define up(x) node[x].up#define down(x) node[x].down#define row(x) node[x].row#define col(x) node[x].colstruct Node{    int left,right,up,down;    int col,row;}node[maxn*maxn];int maxnode,maxrow,head;int rows,cols;int tot[maxn];inline void disable_row(int root){    right(left(root)) = right(root);    left(right(root)) = left(root);}inline void enable_row(int root){    right(left(root)) = root;    left(right(root)) = root;}inline void disable_col(int root){    down(up(root)) = down(root);    up(down(root)) = up(root);}inline void enable_col(int root){    down(up(root)) = root;    up(down(root)) = root;}void initialize(){    memset(tot,0,sizeof(tot));    memset(node,0,sizeof(node));    left(0)=cols; right(0)=1;    for(int i=1;i<=cols;i++)    {        left(i) = i-1;        right(i) = i+1;        up(i) = i;        down(i) = i;        row(i) = 0;        col(i) = i;    }    right(cols) = 0;    head = 0;    maxrow = 0;    maxnode = cols;}void add_row(int a[maxn],int cnt){    maxrow++;    for(int i=1;i<=cnt;i++)    {        maxnode++;        tot[a[i]]++;        if(i==1)        {            right(maxnode) = maxnode;            left(maxnode) = maxnode;        }        else        {            left(maxnode) = maxnode-1;            right(maxnode) = right(maxnode-1);            left(right(maxnode)) = maxnode;            right(maxnode-1) = maxnode;        }        down(maxnode) = a[i];        up(maxnode) = up(a[i]);        down(up(maxnode)) = maxnode;        up(a[i]) = maxnode;        row(maxnode) = maxrow;        col(maxnode) = a[i];    }}inline void removecol(int colnum){    disable_row(colnum);    for(int i=down(colnum);i^colnum;i=down(i))        for(int j=right(i);j^i;j=right(j))            disable_col(j);}inline void resumecol(int colnum){    enable_row(colnum);    for(int i=up(colnum);i^colnum;i=up(i))        for(int j=right(i);j^i;j=right(j))            enable_col(j);}int ans[maxn],top;bool Dance(int k){    int c1 = right(head);    if(c1 == head)    {        top = k;        return true;    }    for(int i=right(c1);i^head;i=right(i))        if(tot[i]<tot[c1]) c1=i;    removecol(c1);    for(int c=down(c1);c^c1;c=down(c))    {        ans[k] = row(c);        for(int j=right(c);j^c;j=right(j))            removecol(col(j));        if(Dance(k+1)) return true;        for(int j=left(c);j^c;j=left(j))            resumecol(col(j));    }    resumecol(c1);    return false;}void print(bool flag){    if(!flag) putchar('\n');    if(!top)    {        printf("NO");        return;    }    printf("%d",top);    sort(ans,ans+top);    for(int i=0;i<top;i++) printf(" %d",ans[i]);}int a[maxn];bool init(){    if(!~scanf("%d%d",&rows,&cols)) return false;    initialize();    for(int i=1;i<=rows;i++)    {        int cnt = 0;        scanf("%d",&cnt);        for(int j=1;j<=cnt;j++) scanf("%d",a+j);        add_row(a,cnt);    }    memset(ans,0,sizeof(ans));    top=0;    return true;}int main(){    freopen("dlx.in","r",stdin);    freopen("dlx.out","w",stdout);    bool flag = true;    while(init())    {        Dance(0);        print(flag);        flag = false;    }    return 0;}
0 0
原创粉丝点击