hust-1017 Exact cover(dancing links模板题)

1017 - Exact cover

Time Limit: 15s Memory Limit: 128MB

Special Judge Submissions: 6812 Solved: 3544
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
 
  
题意:有一个n行m列的矩阵,下面是n行,每一行第一个数代表这一行有多少个1,后面几个数为这几个数的纵坐标。
求一个行的集合使每一列都有且仅有一个1  找到了输出一共几行和分别的行坐标  找不到输出NO
A的第一道精确覆盖题,很惭愧搞了这么久ACM才学这个。。好像挺实用的
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100005
#define Nn 1010
int L[N],R[N],U[N],D[N];
int Col[N],Row[N],ans[Nn],ansn;//Col是列标,Row是行标,ans是解
int H[Nn];
void init(int n,int m)
{
    for(int i=0;i<=m;i++)
    {
       L[i]=i-1;
       R[i]=i+1;
       U[i]=i;
       D[i]=i;
    }
    L[0]=m;R[m]=0;
    for(int i=0;i<=n;i++)
    {
        H[i]=-1;
    }
}
void update(int i,int j,int t)
{
    int k;
    Col[t]=j;
    Row[t]=i;
    D[t]=j;
    U[j]=t;
    for(k=j;D[k]!=j;k=D[k]);
    U[t]=k;
    D[k]=t;
    if(H[i]<0)
    {
        H[i]=L[t]=R[t]=t;
    }
    else
    {
        L[t]=t-1;
        R[t-1]=t;
        R[t]=H[i];
        L[H[i]]=t;
    }
}
void Remove(int k)
{
    L[R[k]]=L[k];
    R[L[k]]=R[k];
    for(int i=D[k];i!=k;i=D[i])
    {
        for(int j=R[i];j!=i;j=R[j])
        {
            U[D[j]]=U[j];
            D[U[j]]=D[j];
        }
    }
}
void Resume(int k)
{
    for(int i=U[k];i!=k;i=U[i])
    {
        for(int j=R[i];j!=i;j=R[j])
        {
            U[D[j]]=j;
            D[U[j]]=j;
        }
    }
    L[R[k]]=k;
    R[L[k]]=k;
}
bool dance(int k)
{
    int c=R[0];
    if(c==0)
        {
            ansn=k;
            return true;
        }
        Remove(c);
        for(int i=D[c];i!=c;i=D[i])
        {
            ans[k]=Row[i];
            for(int j=R[i];j!=i;j=R[j])
                 Remove(Col[j]);
                 if(dance(k+1)) return true;
             for(int j=L[i];j!=i;j=L[j])
                 Resume(Col[j]);
        }
        Resume(c);
        return false;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)==2)
    {
        int t=m+1;
        int c,j;
        init(n,m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&c);
            while(c--)
            {
                scanf("%d",&j);
                update(i,j,t);
                t++;
            }
        }
       if(!dance(0))
       {
           printf("NO\n");
       }
       else
       {
           printf("%d",ansn);
           for(int i=0;i<ansn;i++)
            printf(" %d",ans[i]);
           printf("\n");
       }
    }
    return 0;
}

你可能感兴趣的:(ACM,HDU)