首页 > 代码库 > hdu - 3498 - whosyourdaddy(反复覆盖DLX)

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)