Dance Links(板子)

这里贴一篇博客和OIwiki的Dance Links的讲解讲的都很不错,建议先看OIWIKI理解一下。
OIWIKI
某位大佬

精确覆盖模板

这个板子好像是bin巨的,是用来解决精确覆盖问题的。

int n, m, cnt;//分别为矩阵大小和计数器
int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];//上下左右行列
int H[MAXM], S[MAXM];//H表示行首,S表示列中元素的个数
struct DLX
{
    
    
    int n, m, cnt;

    int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
    int H[MAXM], S[MAXM];

    void init(int _n, int _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,cnt = m;
        for (int i = 1; i <= n; i++)
            H[i] = -1;
    }

    void Link(int r, int c){
    
    
        ++S[Col[++cnt] = c];
        Row[cnt] = r,D[cnt] = D[c];
        U[D[c]] = cnt,U[cnt] = c;
        D[c] = cnt;
        if (H[r] < 0) H[r] = L[cnt] = R[cnt] = cnt;
        else {
    
    
            R[cnt] = R[H[r]],L[R[H[r]]] = cnt;
            L[cnt] = H[r],R[H[r]] = cnt;
        }
    }

    void remove (int c) {
    
    
        L[R[c]] = L[c], R[L[c]] = R[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) {
    
    
        for (int i = U[c]; i != c; i = U[i])
            for (int j = L[i]; j != i; j = L[j])
                ++S[Col[U[D[j]] = D[U[j]] = j]];
        L[R[c]] = R[L[c]] = c;
    }

    void Dance(int d) {
    
    //dance的操作要根据题目修改
        if (R[0] == 0) {
    
    
            return;
        }
        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]) {
    
    
            for (int j = R[i]; j != i; j = R[j]) remove (Col[j]);
            Dance (d + 1);
            for (int j = L[i]; j != i; j = L[j]) resume (Col[j]);
        }
        resume (c);
    }
}dlx;

用于解决重复覆盖问题

struct DLX{
    
    
    int n,m,cnt;
    int U[maxn2],D[maxn2],R[maxn2],L[maxn2],Row[maxn2],Col[maxn2];
    int H[maxn],S[maxn];
    int ansd,ans[maxn];
    void init(int _n,int _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,cnt = m;
        for(int i = 1;i <= n; i++)
            H[i] = -1;
    }
    void link(int r,int c){
    
    
        Col[++cnt] = c,S[c]++;
        Row[cnt] = r,D[cnt] = D[c];
        U[D[c]] = cnt,U[cnt] = c,D[c] = cnt;
        if(H[r]<0)
            H[r] = L[cnt] = R[cnt] = cnt;
        else{
    
    
            R[cnt] = R[H[r]],L[R[H[r]]] = cnt;
            L[cnt] = H[r],R[H[r]] = cnt;
        }
    }

    void remove(int c){
    
    
        for(int i = D[c];i != c;i = D[i])
            L[R[i]] = L[i],R[L[i]] = R[i];
    }

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

    bool v[maxn2];
    int f(){
    
    
        int ret = 0;
        for(int c = R[0];c != 0;c = R[c])
            v[c] = true;
        for(int c = R[0];c != 0;c = R[c])
            if(v[c]){
    
    
            ret++;
            v[c] = false;
            for(int i = D[c]; i != c;i = D[i])
                for(int j = R[i]; j != i;j = R[j])
                v[Col[j]] = false;
        }
        return ret;
    }

    bool dance(int d){
    
    
        if(d + f() > k)
            return false;
        if(R[0] == 0)
            return true;
        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);
            if(dance(d + 1)) return true;
            for(int j = L[i]; j != i;j = L[j])
                resume(j);
            resume(i);
        }
        return false;
    }
}dlx;

猜你喜欢

转载自blog.csdn.net/qq_36102055/article/details/107169514