hdu - 3498 - whosyourdaddy(反复覆盖DLX)

Posted clnchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu - 3498 - whosyourdaddy(反复覆盖DLX)相关的知识,希望对你有一定的参考价值。

题意:N(2 ≤ N ≤ 55)个点,M(0 ≤ M ≤ N*N)条无向边,删除一个点会把与其相邻的点一起删掉。问最少删几次能够删掉全部点。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3498

——>>N个点看成 N 个要被覆盖的列,每一个点作为一行,与其相邻的点的位置在这一行中标为 1,还有它自已的位置也标记为 1。。

这就是经典的反复覆盖问题了。。于是,DLX上场。。

#include <cstdio>
#include <cstring>

const int MAXR = 55 + 10;
const int MAXC = 55 + 10;
const int MAXNODE = MAXR * MAXC;
const int INF = 0x3f3f3f3f;

struct DLX
{
    int sz;
    int H[MAXR], S[MAXC];
    int row[MAXNODE], col[MAXNODE];
    int U[MAXNODE], D[MAXNODE], L[MAXNODE], R[MAXNODE];
    int Min;

    void Init(int n)
    {
        for (int i = 0; i <= n; ++i)
        {
            U[i] = D[i] = i;
            L[i] = i - 1;
            R[i] = i + 1;
        }
        L[0] = n;
        R[n] = 0;

        sz = n + 1;
        memset(S, 0, sizeof(S));
        memset(H, -1, sizeof(H));
    }

    void Link(const int& r, const int& c)
    {
        row[sz] = r;
        col[sz] = c;
        D[sz] = D[c];
        U[D[c]] = sz;
        D[c] = sz;
        U[sz] = c;
        if (H[r] == -1)
        {
            H[r] = L[sz] = R[sz] = sz;
        }
        else
        {
            R[sz] = R[H[r]];
            L[R[H[r]]] = sz;
            R[H[r]] = sz;
            L[sz] = H[r];
        }
        S[c]++;
        sz++;
    }

    void Remove(const int& c)
    {
        for (int i = D[c]; i != c; i = D[i])
        {
            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }
    }

    void Restore(const int& c)
    {
        for (int i = U[c]; i != c; i = U[i])
        {
            L[R[i]] = i;
            R[L[i]] = i;
        }
    }

    int A()
    {
        int ret = 0;
        bool vis[MAXC];

        memset(vis, 0, sizeof(vis));
        for (int i = R[0]; i != 0; i = R[i])
        {
            if (!vis[i])
            {
                vis[i] = true;
                ++ret;
                for (int j = D[i]; j != i; j = D[j])
                {
                    for (int k = R[j]; k != j; k = R[k])
                    {
                        vis[col[k]] = true;
                    }
                }
            }
        }

        return ret;
    }

    void Dfs(int cur)
    {
        if (cur + A() >= Min) return;

        if (R[0] == 0)
        {
            if (cur < Min)
            {
                Min = cur;
            }
            return;
        }

        int c = R[0];
        for (int i = R[0]; i != 0; i = R[i])
        {
            if (S[i] < S[c])
            {
                c = i;
            }
        }

        for (int i = D[c]; i != c; i = D[i])
        {
            Remove(i);
            for (int j = R[i]; j != i; j = R[j])
            {
                Remove(j);
            }
            Dfs(cur + 1);
            for (int j = L[i]; j != i; j = L[j])
            {
                Restore(j);
            }
            Restore(i);
        }
    }

    void Solve()
    {
        Min = INF;
        Dfs(0);
        printf("%d\n", Min);
    }

} dlx;

int N, M;

void Read()
{
    int a, b;

    dlx.Init(N);
    while (M--)
    {
        scanf("%d%d", &a, &b);
        dlx.Link(a, b);
        dlx.Link(b, a);
    }
    for (int i = 1; i <= N; ++i)
    {
        dlx.Link(i, i);
    }
}

int main()
{
    while (scanf("%d%d", &N, &M) == 2)
    {
        Read();
        dlx.Solve();
    }

    return 0;
}


以上是关于hdu - 3498 - whosyourdaddy(反复覆盖DLX)的主要内容,如果未能解决你的问题,请参考以下文章

poj 3498 最大流

bzoj 3498

POJ 3498 March of the Penguins(网络流+枚举)

P3498 [POI2010]KOR-Beads

3498 小木棍

ZOJ 3498 Javabeans