zoukankan      html  css  js  c++  java
  • HUST 1017 Exact cover (Dancing links)

    1017 - Exact cover

    时间限制:15秒 内存限制:128兆

    自定评测 6110 次提交 3226 次通过
    题目描述
    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.
    输入
    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.
    输出
    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".
    样例输入
    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
    
    样例输出
    3 2 4 6
    
    提示
    来源
    dupeng

    本人智商奇低,看了三天才学会,模板验证题。

    顺便一提这是正式转入C++后第一A。

      1 #include<iostream>
      2 #include<cstdio>
      3 using    namespace    std;
      4 
      5 const    int    HEAD = 0;
      6 const    int    N = 1005;
      7 int    MAP[N][N];
      8 int    U[N * N],D[N * N],L[N * N],R[N * N],H[N * N],C[N * N],ANS[N * N];
      9 
     10 void    ini(int col);
     11 bool    dancing(int k);
     12 void    output(void);
     13 void    remove(int c);
     14 void    resume(int c);
     15 int    main(void)
     16 {
     17     int    n,m,num,col;
     18     int    count,front,first;
     19 
     20     while(cin >> n >> m)
     21     {
     22         ini(m);
     23 
     24         count = m + 1;
     25         for(int i = 1;i <= n;i ++)
     26         {
     27             cin >> num;
     28             front = first = count;
     29             while(num --)
     30             {
     31                 cin >> col;
     32 
     33                 U[count] = U[col];
     34                 D[count] = col;
     35                 L[count] = front;
     36                 R[count] = first;
     37 
     38                 D[U[col]] = count;
     39                 U[col] = count;
     40                 R[front] = count;
     41 
     42                 H[count] = i;
     43                 C[count] = col;
     44                 front = count;
     45                 count ++;
     46             }
     47             L[first] = count - 1;
     48         }
     49         if(!dancing(1))
     50             cout << "NO" << endl;
     51     }
     52 
     53     return    0;
     54 }
     55 
     56 void    ini(int col)
     57 {
     58     U[HEAD] = D[HEAD] = H[HEAD] = C[HEAD] = HEAD;
     59     R[HEAD] = 1;
     60     L[HEAD] = col;
     61 
     62     int    front = HEAD;
     63     for(int i = 1;i <= col;i ++)
     64     {
     65         U[i] = D[i] = i;
     66         L[i] = front;
     67         R[i] = HEAD;
     68         R[front] = i;
     69         front = i;
     70 
     71         C[i] = i;
     72         H[i] = 0;
     73     }
     74 }
     75 
     76 bool    dancing(int k)
     77 {
     78     int    c = R[HEAD];
     79     if(c == HEAD)
     80     {
     81         output();
     82         return    true;
     83     }
     84 
     85     remove(C[c]);
     86     for(int i = D[c];i != c;i = D[i])
     87     {
     88         ANS[k] = H[i];
     89         for(int j = R[i];j != i;j = R[j])
     90             remove(C[j]);
     91         if(dancing(k + 1))
     92             return    true;
     93         for(int j = L[i];j != i;j = L[j])
     94             resume(C[j]);
     95     }
     96     resume(C[c]);
     97 
     98     return    false;
     99 }
    100 
    101 void    output(void)
    102 {
    103     int    i,j;
    104     for(i = 1;ANS[i];i ++);
    105     cout << i - 1 << " ";
    106     for(j = 1;j < i - 1;j ++)
    107         cout << ANS[j] << " ";
    108     cout << ANS[j] << endl;
    109 }
    110 
    111 void    remove(int c)
    112 {
    113     R[L[c]] = R[c];
    114     L[R[c]] = L[c];
    115 
    116     for(int i = D[c];i != c;i = D[i])
    117         for(int j = R[i];j != i;j = R[j])
    118         {
    119             D[U[j]] = D[j];
    120             U[D[j]] = U[j];
    121         }
    122 }
    123 
    124 void    resume(int c)
    125 {
    126     R[L[c]] = c;
    127     L[R[c]] = c;
    128 
    129     for(int i = U[c];i != c;i = U[i])
    130         for(int j = R[i];j != i;j = R[j])
    131         {
    132             D[U[j]] = j;
    133             U[D[j]] = j;
    134         }
    135 }
  • 相关阅读:
    【prufer编码】BZOJ1430 小猴打架
    【费马小定理】BZOJ3260 跳
    【欧拉函数】BZOJ2705: [SDOI2012]Longge的问题
    【卡特兰数】BZOJ1485: [HNOI2009]有趣的数列
    【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?
    【Floyd】BZOJ1491: [NOI2007]社交网络
    【转】对信息学竞赛中调试方法的建议
    【建图+拓扑判环】BZOJ3953: [WF2013]Self-Assembly
    【dfs判负环】BZOJ1489: [HNOI2009]最小圈
    【二分+最小树形图】UVA11865 比赛网络
  • 原文地址:https://www.cnblogs.com/xz816111/p/4332729.html
Copyright © 2011-2022 走看看