時間限制:15秒 內存限制:128兆
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
本人智商奇低,看了三天才學會,模板驗證題。
順便一提這是正式轉入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 }