DancingLinks [HUST_1017]

Source: Internet
Author: User

Because the output is less than \ n WA for half a day. In addition, the efficiency of the two writing methods is different. To be studied, DancingLinks is used to optimize the search. The core is the resume operation, which can quickly restore the deleted Node 1. 1468 ms [cpp]/* http://acm.hust.edu.cn/problem.php? Id = 1017 * // * DLX */# include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> using namespace std; const int MAXN = 200000; const int MAXM = 1111; struct DLX {int L [MAXN], R [MAXN], U [MAXN], D [MAXN], C [MAXN], Col [MAXN], row [MAXN]; int S [MAXM], Ans [MAXM]; int tot, len; void build (int N, int M) {memset (S, 0, MAXM * sizeof (int); // construct the head and col head for (int I = 0; I <= M; I ++) {U [I] = D [I] = I; L [I] = I-1; R [I] = I + 1; C [I] = I;} L [0] = M, R [M] = 0; tot = M; for (int I = 1; I <= N; I ++) {int K, T; scanf ("% d", & K ); for (int j = 1; j <= K; j ++) {scanf ("% d", & T); int node = ++ tot; C [node] = Col [node] = T; Row [node] = I; S [T] ++; // maintain the number of nodes in the column linked list // maintain the UD pointer D [node] = D [C [T]; D [C [T] = node; U [D [node] = node; U [D [C [T] = C [T]; C [T] = node; // update to the end // maintain two LR pointers if (j = 1) L [node] = node + K-1; else L [node] = node-1; if (j = = K) R [node] = node-K + 1; else R [node] = node + 1 ;}} for (int I = 1; I <= M; I ++) {C [I] = I;} // restore the column header pointer} void debug () {for (int I = R [0]; I! = 0; I = R [I]) {printf ("C % d:", I); int r = 1; for (int j = D [I]; j! = I; j = D [j]) {int ri = Row [j]; // cout <"ri" <ri <"" <r <endl; while (ri> r) {r ++; printf ("0");} printf ("1"); r ++; // printf ("j: % d \ n", j ); // system ("pause");} printf ("\ n") ;}} void remove (const 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 [C [j] -- ;}} void resume (const int & c) {L [R [c] = c; R [L [c] = c; for (int I = D [c]; I! = C; I = D [I]) {for (int j = R [I]; j! = I; j = R [j]) {U [D [j] = j; D [U [j] = j; S [C [j] ++ ;}} bool dfs (const int & k) {if (R [0] = 0) {len = k; return true;} int s (0x7fffffff), c; for (int I = R [0]; I! = 0; I = R [I]) {if (S [I] <s) {c = I; s = S [I] ;}} remove (c ); for (int I = D [c]; I! = C; I = D [I]) {Ans [k] = Row [I]; for (int j = R [I]; j! = I; j = R [j]) {remove (C [j]);} if (dfs (k + 1) {return true ;} for (int j = R [I]; j! = I; j = R [j]) {resume (C [j]) ;}} resume (c); return false;} void solve () {if (dfs (0) {printf ("% d", len); for (int I = 0; I <len; I ++) {printf ("% d", Ans [I]) ;}puts ("") ;}else {puts ("NO") ;}} dlx; int main () {int N, M; while (~ Scanf ("% d", & N, & M) {dlx. build (N, M); dlx. solve ();} return 0;} 2.328 ms [cpp]/* http://acm.hust.edu.cn/problem.php? Id = 1017 * // * DLX */# include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> using namespace std; const int MAXN = 2000000; const int MAXM = 1111; struct DLX {int L [MAXN], R [MAXN], U [MAXN], D [MAXN], C [MAXN], Col [MAXN], row [MAXN]; int S [MAXM], Ans [MAXM]; int tot, len; void build (int N, int M) {memset (S, 0, MAXM * sizeof (int); // construct the head and col head for (int I = 0; I <= M; I ++) {U [I] = D [I] = I; L [I] = I-1; R [I] = I + 1; C [I] = I;} L [0] = M, R [M] = 0; tot = M; for (int I = 1; I <= N; I ++) {int K, T; scanf ("% d", & K ); for (int j = 1; j <= K; j ++) {scanf ("% d", & T); int node = ++ tot; C [node] = Col [node] = T; Row [node] = I; S [T] ++; // maintain the number of nodes in the column linked list // maintain the UD pointer D [node] = D [C [T]; D [C [T] = node; U [D [node] = node; U [D [C [T] = C [T]; C [T] = node; // update to the end // maintain two LR pointers if (j = 1) L [node] = node + K-1; else L [node] = node-1; if (j = K) R [node] = node-K + 1; else R [node] = node + 1 ;}} for (int I = 1; I <= M; I ++) {C [I] = I;} // restore the column header pointer} void debug () {for (int I = R [0]; I! = 0; I = R [I]) {printf ("C % d:", I); int r = 1; for (int j = D [I]; j! = I; j = D [j]) {int ri = Row [j]; // cout <"ri" <ri <"" <r <endl; while (ri> r) {r ++; printf ("0");} printf ("1"); r ++; // printf ("j: % d \ n", j ); // system ("pause");} printf ("\ n") ;}} void remove (const 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 [C [j] -- ;}} void resume (const int & c) {for (int I = U [c]; I! = C; I = U [I]) {for (int j = L [I]; j! = I; j = L [j]) {U [D [j] = j; D [U [j] = j; S [C [j] ++ ;}l [R [c] = c; R [L [c] = c ;} bool dfs (const int & k) {if (R [0] = 0) {len = k; return true;} int s (0x7fffffff), c; for (int I = R [0]; I! = 0; I = R [I]) {if (S [I] <s) {c = I; s = S [I] ;}} remove (c ); for (int I = D [c]; I! = C; I = D [I]) {Ans [k] = Row [I]; for (int j = R [I]; j! = I; j = R [j]) {remove (C [j]);} if (dfs (k + 1) {return true ;} for (int j = L [I]; j! = I; j = L [j]) {resume (C [j]) ;}} resume (c); return false;} void solve () {if (dfs (0) {printf ("% d", len); for (int I = 0; I <len; I ++) {printf ("% d", Ans [I]) ;}puts ("") ;}else {puts ("NO") ;}} dlx; int main () {int N, M; www.2cto.com while (scanf ("% d", & N, & M )! = EOF) {dlx. build (N, M); dlx. solve ();} return 0 ;}

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.