HUST 1017 Exact cover

DLX.最近开始学习dancing links了。据说这道题是测试模板正确性的题。这个http://blog.csdn.net/fp_hzq/article/details/6800113挺好的,以前也经常逛他的博客。DLX也用了他的模板,等过一阵有了新的理解,也许会改成我自己的风格,不过预计改动不大(就是换个变量名什么的^_^)。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", s)
#define PC(n) printf("Case %d: ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 1e9+7;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const int N = 2222;
const int M = 111111;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

int U[M], D[M], L[M], R[M], X[M], Y[M];
int H[N], S[N], Q[N];
int size, n, m;

void remove(int c)
{
    R[L[c]] = R[c], L[R[c]] = L[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[Y[j]]--;
}

void resume(int c)
{
    R[L[c]] = L[R[c]] = c;
    for(int i = U[c]; i != c; i = U[i])
        for(int j = L[i]; j != i; j = L[j])
            S[Y[U[D[j]] = D[U[j]] = j]]++;
}

bool Dance(int k)
{
    int i, j, tmp, c;
    if(!R[0])
    {
        printf("%d", k);
        REP(i, k)printf(" %d", X[Q[i]]);
        puts("");
        return 1;
    }
    for(tmp = N, i = R[0]; i; i = R[i])if(S[i] < tmp)tmp = S[c = i];
    remove(c);
    for(i = D[c]; i != c; i = D[i])
    {
        Q[k] = i;
        for(j = R[i]; j != i; j = R[j])remove(Y[j]);
        if(Dance(k + 1))return 1;
        for(j = L[i]; j != i; j = L[j])resume(Y[j]);
    }
    resume(c);
    return 0;
}

void Link(int r, int c)
{
    S[Y[size] = c]++;
    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;
    }
    X[size++] = r;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int num, k;
    while(RII(n, m) != EOF)
    {
        FFF(i, 0, m)
        {
            S[i] = 0;
            D[i] = U[i] = i;
            L[i + 1] = i;
            R[i] = i + 1;
        }
        R[m] = 0;
        size = m + 1;
        FFF(i, 1, n)
        {
            H[i] = -1;
            RI(num);
            while(num--)RI(k), Link(i, k);
        }
        if(!Dance(0))puts("NO");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值