Hust 1017 exact cover DLX

Source: Internet
Author: User

A matrix of n × m rows. Each grid may be 0 or 1. Now let you select several columns so that each column has only one 1. (precise coverage of template questions)

Solution:

Dancing links Template

About the dancing links presented several must-see: http://par.buaa.edu.cn/acm-icpc/filepool/r/35/

The kuangbin board used by the Board (or suitable for cattle ..)

Solution:

1 // file name: hust1017.cpp 2 // Author: darkdream 3 // created time: saturday, October 04, 2014, 57 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # Include <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long 25 const int maxnode = 100010; 26 const int maxm = 1010; 27 const int maxn = 1010; 28 using namespace STD; 29 struct DLX 30 {31 int n, m, size; 32 int L [maxnode], R [maxnode], U [maxnode], d [maxnode], row [maxnode], Col [maxnode]; 33 int anSd, ANS [maxn]; 34 int H [maxn], s [maxm]; 35 void Init (INT _ n, int _ m) 36 {37 n = _ n; 38 m = _ m; 39 for (INT I = 0; I <= m; I ++) 40 {41 s [I] = 0; // s represents the color of each column 42 U [I] = d [I] = I; 43 L [I] = I-1; 44 R [I] = I + 1; 45} 46 R [m] = 0; L [0] = m; 47 size = m; 48 for (INT I = 1; I <= N; I ++) 49 H [I] =-1; // H [I] indicates the header 50} 51 void Link (INT R, int c) 52 {53 + + s [col [++ size] = C]; 54 row [size] = r; 55 d [size] = d [c]; 56 U [d [c] = size; 57 U [size] = C; 58 D [c] = size; 59 If (H [R] <0) H [R] = L [size] = R [size] = size; 60 else 61 {62 R [size] = R [H [R]; 63 l [R [H [R] = size; 64 L [size] = H [R]; 65 R [H [R] = size; // insert to the right of H [R] 66} 67} 68 void remove (INT c) // column as the unit, horizontal 69 {70 L [R [c] = L [c]; R [L [c] = R [c]; 71 for (INT I = d [c]; I! = C; I = d [I]) 72 for (Int J = R [I]; J! = I; j = R [J]) 73 {74 U [d [J] = U [J]; 75 d [U [J] = d [J]; 76 -- s [col [J]; 77} // delete a column, row K 78} 79 void resume (int c) 80 {81 L [R [c] = R [L [c] = C; 82 For (INT I = U [c]; I! = C; I = U [I]) 83 for (Int J = L [I]; J! = I; j = L [J]) 84 {85 U [d [J] = J; 86 d [U [J] = J; 87 + + s [col [J]; 88} 89} 90 bool Dance (INT d) 91 {92 If (R [0] = 0) 93 {94 anSd = D; // The Answer length is 95 return true; 96} 97 int c = R [0]; 98 for (INT I = R [c]; I! = 0; I = R [I]) 99 If (s [I] <s [c]) 100 c = I; 101 remove (C ); 102 for (INT I = d [c]; I! = C; I = d [I]) 103 {104 ans [d] = row [I]; 105 for (Int J = R [I]; J! = I; j = R [J]) Remove (COL [J]); 106 If (Dance (D + 1) return true; 107 for (Int J = L [I]; J! = I; j = L [J]) Resume (COL [J]); 108} 109 resume (c); 110 return false; 111} 112}; 113 114 DLX g; 115 int main () {116 int n, m; 117 while (scanf ("% d", & N, & M )! = EOF) 118 {119G. init (n, m); 120 int num, J; 121 for (INT I = 1; I <= N; I ++) 122 {123 scanf ("% d ", & num); 124 while (Num --) 125 {126 scanf ("% d", & J); 127G. link (I, j); 128} 129} 130 If (! G. dance (0) printf ("NO \ n"); 131 else {132 printf ("% d", G. anSd); 133 for (INT I = 0; I <G. anSd; I ++) 134 printf ("% d", G. ans [I]); 135 printf ("\ n"); 136} 137 138 return 0; 139}
View code

 

Hust 1017 exact cover DLX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.