精确覆盖问题:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1

还有重复覆盖问题

dancing links 是 一种数据结构,用来优化搜索,不算是一种算法。(双向循环十字链表)

参阅了白书训练指南上的模版,目前只有精确覆盖,等再补上重复覆盖

struct DLX
{
  int n , sz;                                                 // 行数,节点总数
  int S[maxn];                                                // 各列节点总数
  int row[maxnode],col[maxnode];                              // 各节点行列编号
  int L[maxnode],R[maxnode],U[maxnode],D[maxnode];            // 十字链表

  int ansd,ans[maxn];                                         //

  void init(int n )
  {
    this->n = n ;
    for(int i = 0 ; i <= n; i++ )
    {
      U[i] = i ;
      D[i] = i ;
      L[i] = i - 1;
      R[i] = i + 1;
    }
    R[n] = 0 ;
    L[0] = n;
    sz = n + 1 ;
    memset(S,0,sizeof(S));
  }
  void addRow(int r,vector<int> c1)
  {
    int first = sz;
    for(int i = 0 ; i < c1.size(); i++ ){
      int c = c1[i];
      L[sz] = sz - 1 ; R[sz] = sz + 1 ; D[sz] = c ; U[sz] = U[c];
      D[U[c]] = sz; U[c] = sz;
      row[sz] = r; col[sz] = c;
      S[c] ++ ; sz ++ ;
    }
    R[sz - 1] = first ; L[first] = sz - 1;
  }
  // 顺着链表A,遍历除s外的其他元素
  #define FOR(i,A,s) for(int i = A[s]; i != s ; i = A[i])

  void remove(int c){
    L[R[c]] = L[c];
    R[L[c]] = R[c];
    FOR(i,D,c)
      FOR(j,R,i) {U[D[j]] = U[j];D[U[j]] = D[j];--S[col[j]];}
  }
  void restore(int c){
    FOR(i,U,c)
      FOR(j,L,i) {++S[col[j]];U[D[j]] = j;D[U[j]] = j; }
    L[R[c]] = c;
    R[L[c]] = c;
  }
  bool dfs(int d){
    if(R[0] == 0 ){
      ansd = d;
      return true;
    }
    // 找S最小的列c
    int c = R[0] ;
    FOR(i,R,0) if(S[i] < S[c]) c = i;

    remove(c);
    FOR(i,D,c){
      ans[d] = row[i];
      FOR(j,R,i) remove(col[j]);
      if(dfs(d + 1)) return true;
      FOR(j,L,i) restore(col[j]);
    }
    restore(c);

    return false;
  }
  bool solve(vector<int> & v){
    v.clear();
    if(!dfs(0)) return false;
    for(int i = 0 ; i< ansd ;i ++ ) v.push_back(ans[i]);
    return true;
  }
};

DLX solver;


int main()
{
  int n,m;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    solver.init(m);
    int c , x;
    vector<int> c1;
    for(int i = 1; i<= n ; i ++ )
    {
      scanf("%d",&c);
      c1.clear();
      for(int j = 0 ; j < c ; j ++ ){scanf("%d",&x);c1.push_back(x);}
      solver.addRow(i,c1);
    }
    vector<int> ans;
    bool flag ;
    flag = solver.solve(ans);
    if(flag )
    {
      int size1 = ans.size();
      printf("%d",size1);
      for(int i = 0 ; i < size1;i ++ )
        printf(" %d",ans[i]);
      printf("\n");
    }
    else printf("NO\n");
  }
  return 0;
}
DLX 精确覆盖 template

相关文章: