zoukankan      html  css  js  c++  java
  • DLX模板

     1 const int maxnode=100010;
     2 const int maxn=1010;
     3 const int maxm=1010;
     4 struct DLX
     5 {
     6     int L[maxnode],R[maxnode],U[maxnode],D[maxnode],Row[maxnode],Col[maxnode],C[maxm],H[maxn],cnt;
     7     int ans[maxn],ansd;
     8     void Init(int n,int m)
     9     {
    10         for(int i=0;i<=m;i++)
    11         {
    12             L[i]=i-1;R[i]=i+1;
    13             U[i]=D[i]=i;C[i]=0;
    14         }
    15         cnt=m;L[0]=m;R[m]=0;
    16 
    17         ansd=0;
    18 
    19         for(int i=1;i<=n;i++)
    20             H[i]=0;
    21     }
    22     void Link(int x,int y)
    23     {
    24         C[Col[++cnt]=y]++;
    25         Row[cnt]=x;
    26 
    27         U[cnt]=y;
    28         U[D[y]]=cnt;
    29         D[cnt]=D[y];
    30         D[y]=cnt;
    31 
    32         if(H[x])
    33             L[R[H[x]]]=cnt,R[cnt]=R[H[x]],R[H[x]]=cnt,L[cnt]=H[x];
    34         else
    35             H[x]=L[cnt]=R[cnt]=cnt;
    36     }
    37 
    38     void Delete(int c)
    39     {
    40         L[R[c]]=L[c];R[L[c]]=R[c];
    41         for(int i=D[c];i!=c;i=D[i])
    42             for(int j=R[i];j!=i;j=R[j])
    43                 --C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
    44     }
    45 
    46     void Resume(int c)
    47     {
    48         L[R[c]]=c;R[L[c]]=c;
    49         for(int i=U[c];i!=c;i=U[i])
    50             for(int j=L[i];j!=i;j=L[j])
    51                 ++C[Col[j]],U[D[j]]=j,D[U[j]]=j;
    52     }
    53 
    54     bool Solve()
    55     {
    56         if(!R[0])return true;
    57         int p=R[0];
    58         for(int i=R[p];i;i=R[i])
    59             if(C[p]>C[i])
    60                 p=i;
    61         Delete(p);
    62         ansd++;
    63         for(int i=D[p];i!=p;i=D[i]){
    64             ans[ansd]=Row[i];
    65             for(int j=R[i];j!=i;j=R[j])
    66                 Delete(Col[j]);
    67             if(Solve())
    68                 return true;
    69 
    70             for(int j=L[i];j!=i;j=L[j])
    71                 Resume(Col[j]);
    72         }
    73         Resume(p);
    74         --ansd;
    75         return false;
    76     }
    77 }DLX;
    尽最大的努力,做最好的自己!
  • 相关阅读:
    axb_2019_fmt32 盲打和格式化字符串
    ciscn_2019_final_3 需要避开当前free的chunk的下一个chunk不能超过arena的边界
    xdctf2015_pwn200
    valarray类
    Mysql 常用命令.
    如何处理IO
    等号两边自动添加括号
    Request JSON
    开机小脚本自动打开sublime text 和git-bash
    git 同步勾子
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5243222.html
Copyright © 2011-2022 走看看