zoukankan      html  css  js  c++  java
  • HUST1017(KB3-A Dancing links)

    1017 - Exact cover

    Time Limit: 15s Memory Limit: 128MB

    Special Judge Submissions: 7270 Solved: 3754

    DESCRIPTION

    There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find out the selected rows.

    INPUT

    There are multiply test cases. First line: two integers N, M; The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.

    OUTPUT

    First output the number of rows in the selection, then output the index of the selected rows. If there are multiply selections, you should just output any of them. If there are no selection, just output "NO".

    SAMPLE INPUT

    6 7
    3 1 4 7
    2 1 4
    3 4 5 7
    3 3 5 6
    4 2 3 6 7
    2 2 7
    

    SAMPLE OUTPUT

    3 2 4 6
    

    HINT

    SOURCE

    dupeng
    精确覆盖问题,dancing links模板
      1 //2017-03-09
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <cstring>
      5 
      6 using namespace std;
      7 
      8 const int N = 1010;
      9 const int M = 1010;
     10 const int maxnode = N*M;
     11 
     12 struct DLX
     13 {
     14     int n, m, sz;
     15     int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
     16     int H[N], S[M];
     17     int ansd, ans[N];
     18 
     19     void init(int nn, int mm)
     20     {
     21         n = nn; m = mm;
     22         for(int i = 0; i <= m; i++)
     23         {
     24             S[i] = 0;
     25             U[i] = D[i] = i;
     26             L[i] = i-1;
     27             R[i] = i+1;
     28         }
     29         R[m] = 0; L[0] = m;
     30         sz = m;
     31         for(int i = 1; i <= n; i++)H[i] = -1;
     32     }
     33 
     34     void link(int r, int c)
     35     {
     36         ++S[Col[++sz] = c];
     37         Row[sz] = r;
     38         D[sz] = D[c];
     39         U[D[c]] = sz;
     40         U[sz] = c;
     41         D[c] = sz;
     42         if(H[r] < 0)H[r] = L[sz] = R[sz] = sz;
     43         else{
     44             R[sz] = R[H[r]];
     45             L[R[H[r]]] = sz;
     46             L[sz] = H[r];
     47             R[H[r]] = sz;
     48         }
     49     }
     50 
     51     void Remove(int c)
     52     {
     53         L[R[c]] = L[c]; R[L[c]] = R[c];
     54         for(int i = D[c]; i != c; i = D[i])
     55               for(int j = R[i]; j != i; j = R[j])
     56             {
     57                 U[D[j]] = U[j];
     58                 D[U[j]] = D[j];
     59                 --S[Col[j]];
     60             }
     61     }
     62 
     63     void resume(int c)
     64     {
     65         for(int i = U[c]; i != c; i = U[i])
     66               for(int j = L[i]; j != i; j = L[j])
     67                   ++S[Col[U[D[j]]=D[U[j]]=j]]; 
     68         L[R[c]] = R[L[c]] = c;
     69     }
     70 
     71     bool Dance(int d)
     72     {
     73         if(R[0] == 0)
     74         {
     75             printf("%d ", d);
     76             for(int i = 0; i < d; i++)
     77                   if(i == d-1)printf("%d
    ", ans[i]);
     78                 else printf("%d ", ans[i]);
     79             return true;
     80         }
     81         int c = R[0];
     82         for(int i = R[0]; i != 0; i = R[i])
     83               if(S[i] < S[c])c = i;
     84         Remove(c);
     85         for(int i = D[c]; i != c; i = D[i])
     86         {
     87             ans[d] = Row[i];
     88             for(int j = R[i]; j != i; j = R[j])Remove(Col[j]);
     89             if(Dance(d+1))return true;
     90             for(int j = L[i]; j != i; j = L[j])resume(Col[j]);
     91         }
     92         resume(c);
     93         return false;
     94     }
     95 }dlx;
     96 
     97 int main()
     98 {
     99     int n, m, c, tmp;
    100     while(scanf("%d%d", &n, &m)!=EOF)
    101     {
    102         dlx.init(n, m);
    103         for(int i = 1; i <= n; i++)
    104         {
    105             scanf("%d", &c);
    106             for(int j = 0; j < c; j++)
    107             {
    108                 scanf("%d", &tmp);
    109                 dlx.link(i, tmp);
    110             }
    111         }
    112         if(!dlx.Dance(0))printf("NO
    ");
    113     }
    114 
    115     return 0;
    116 }
  • 相关阅读:
    HDU dp递推 母牛的故事 *
    自制权限框架(二)注解
    自制权限框架(二)注解
    自制权限框架(二)注解
    自制权限框架(二)注解
    iOS Sprite Kit教程之申请和下载证书
    iOS Sprite Kit教程之申请和下载证书
    iOS Sprite Kit教程之申请和下载证书
    白领创业做起了小龙虾的生意,如今公司日销售额达30万元
    她将小镇上的童装生意做到了线上,这位夫妻因此获得了大丰收
  • 原文地址:https://www.cnblogs.com/Penn000/p/6527991.html
Copyright © 2011-2022 走看看