HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

题目链接:https://vjudge.net/problem/HUST-1017


1017 - Exact cover

时间限制:15秒 内存限制:128兆

自定评测 7673 次提交 3898 次通过
题目描述
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.
输入
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.
输出
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".
样例输入
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
样例输出
3 2 4 6
提示
来源
dupeng


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
const int MAXM = 1e3+10;
const int maxnode = 1e6+10;

int n, m;
struct DLX      //矩阵的行和列是从1开始的
{
    int n, m, size; //size为结点数
    int U[maxnode], D[maxnode], L[maxnode], R[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXN], S[MAXM];   //H为每一行的头结点,但不参与循环。S为每一列的结点个数
    int ansd, ans[MAXN];

    void init(int _n, int _m)   //m为列
    {
        n = _n;
        m = _m;
        for(int i = 0; i<=m; i++)   //初始化列的头结点
        {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
        }
        R[m] = 0; L[0] = m;
        size = m;
        for(int i = 1; i<=n; i++) H[i] = -1;    //初始化行的头结点
    }

    void Link(int r, int c)
    {
        size++; //类似于前向星
        Col[size] = c;
        Row[size] = r;
        S[Col[size]]++;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r]==-1) H[r] = L[size] = R[size] = size; //当前行为空
        else        //当前行不为空: 头插法,无所谓顺序,因为Row、Col已经记录了位置
        {
            R[size] = R[H[r]];
            L[R[H[r]]] = size;
            L[size] = H[r];
            R[H[r]] = size;
        }
    }

    void remove(int c)  //c是列的编号, 不是结点的编号
    {
        L[R[c]] = L[c]; R[L[c]] = R[c];     //在列的头结点的循环队列中, 越过列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 resume(int c)
    {
        L[R[c]] = R[L[c]] = 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]]++;
        }
    }

    bool Dance(int d)
    {
        if(R[0]==0)
        {
            printf("%d ", d);
            for(int i = 0; i<d; i++)
                printf("%d ", ans[i]);
            printf("\n");
            return true;
        }

        int c = R[0];
        for(int i = R[0]; i!=0; i = R[i])   //挑结点数最少的那一列,否则会超时,那为什么呢?
            if(S[i]<S[c])
                c = i;

        remove(c);
        for(int i = D[c]; i!=c; i = D[i])
        {
            ans[d] = Row[i];
            for(int j = R[i]; j!=i; j = R[j]) remove(Col[j]);
            if(Dance(d+1)) return true;
            for(int j = L[i]; j!=i; j = L[j]) resume(Col[j]);
        }
        resume(c);
        return false;
    }
};

DLX dlx;
int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        dlx.init(n,m);  //初始化矩阵, n*m为矩阵的大小
        for(int i = 1; i<=n; i++)
        {
            int num, j;
            scanf("%d",&num);
            while(num--)
            {
                scanf("%d", &j);
                dlx.Link(i, j);      //在矩阵中的第i行, 第j列加入一个“1”
            }
        }
        if(!dlx.Dance(0))
            puts("NO");
    }
    return 0;
}


posted on 2017-09-17 09:15  h_z_cong  阅读(192)  评论(0编辑  收藏  举报

导航