HUST 1017 Exact cover (DLX)
Description
There is an N * M matrix with only 0 s and 1 s, (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 1 s 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 shoshould just output any of them. if there are no selection, just output "NO ".
Sample Input
6 73 1 4 72 1 43 4 5 73 3 5 64 2 3 6 72 2 7
Sample Output
3 2 4 6
DLX: precise coverage and repeated coverage. This question is precisely covered. Learning materials; click to open the link. After reading the article for one afternoon, add the bin God template.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) typedef long LL; typedef pair
Pil; const int maxnnode = 100100; const int maxn = 1005; const int mod = 1000000007; struct DLX {int n, m, size; int U [maxnnode], D [maxnnode], L [maxnnode], R [maxnnode], Row [maxnnode], Col [maxnnode]; int H [maxn], S [maxn]; int ansd, ans [maxn]; void init (int a, int B) {n = a; m = B; REPF (I, 0, m) {S [I] = 0; U [I] = D [I] = I; L [I] = I-1; R [I] = I + 1;} R [m] = 0; L [0] = m; size = m; REPF (I, 1, n) H [I] =-1;} void link (int r, int c) {++ S [Col [++ s Ize] = c]; Row [size] = r; D [size] = D [c]; U [D [c] = size; U [size] = c; D [c] = size; if (H [r] <0) H [r] = L [size] = R [size] = size; else {R [size] = R [H [r]; L [R [H [r] = size; L [size] = H [r]; R [H [r] = size ;}} void remove (int c) {L [R [c] = L [c]; R [L [c] = R [c]; for (int I = D [c]; I! = C; I = D [I]) {for (int j = R [I]; j! = I; j = R [j]) {U [D [j] = U [j]; D [U [j] = D [j]; -- S [Col [j] ;}} void resume (int c) {for (int I = U [c]; I! = C; I = U [I]) {for (int j = L [I]; j! = I; j = L [j]) + + S [Col [U [D [j] = D [U [j] = j];} L [R [c] = R [L [c] = c;} bool Dance (int d) {if (R [0] = 0) {ansd = d; return true;} int c = R [0]; for (int I = R [0]; I! = 0; I = R [I]) {if (S [I]