The dance chain has two models, precise coverage, and multiple overlays. Precise Coverage
HUST 1017
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <queue> # Include <stack> #include <set> #include <map> #include <queue>//#include <tr1/unordered_set > #include <tr1/unordered_map> #include <bitset>//#pragma comment (linker, "/stack
: 1024000000,1024000000 ") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo
R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 1000*100*2+30; struct sad{inT L[MAXN],R[MAXN],U[MAXN],D[MAXN];
int ROW[MAXN],COL[MAXN];
int H[MAXN],S[MAXN];
int si;
vector<int>ans;
void init (int m) {for (int i=0;i<=m;i++) {U[i] = d[i] = i;
L[i] = i-1;
R[i] = i+1;
S[i] = 0;
} l[0] = m;
R[m] = 0;
CLR (h,-1);
Si = m;
Ans.clear ();
} void link (int row,int col) {si++; Col[si] = col;
Row[si] = row;
s[col]++; U[SI] = U[col];
D[si] = col;
D[u[col]] = si;
U[col] = si;
if (H[row]! =-1) {R[si] = H[row];
L[SI] = L[h[row]];
R[l[si]] = si;
L[r[si]] = si;
} else H[row] = L[si] = R[si] = si;
} 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]
) {U[d[j]] = j;
D[U[J]] = j;
s[col[j]]++;
}} R[l[c]] = c;
L[R[C]] = c;
} bool Dfs () {if (r[0] = = 0) return true;
int c = 0; for (int i=r[0];i!=0;i=r[i]) {if (!c| |
S[c]>s[i]) c=i;
} Remove (c);
for (int i=d[c];i!=c;i=d[i]) {ans.push_back (row[i]);
for (int j=r[i];j!=i;j=r[j]) Remove (Col[j]);
if (Dfs ()) return true;
for (int j=l[i];j!=i;j=l[j]) Resume (Col[j]);
Ans.pop_back ();
} Resume (c);
return false;
}}DLX;
int main () {//Freopen ("Input.txt", "R", stdin);
int n,m; while (~SCANF ("%d%d ", &n,&m)) {dlx.init (M);
int A, B;
for (int i=1;i<=n;i++) {scanf ("%d", &a);
while (a--) {scanf ("%d", &b);
Dlx.link (I,B);
}} if (Dlx.dfs ()) {vector<int> ans = dlx.ans;
Sort (Ans.begin (), Ans.end ());
printf ("%d", ans.size ());
for (int i=0;i<ans.size (); i++) printf ("%d", ans[i]);
printf ("\ n");
} else printf ("no\n");
} return 0;
}
ZOJ 3209
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <queue> # Include <stack> #include <set> #include <map> #include <queue>//#include <tr1/unordered_set > #include <tr1/unordered_map> #include <bitset>//#pragma comment (linker, "/stack
: 1024000000,1024000000 ") using namespace std; #define Lson L, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define INF 1e9 #define DEBUG (a) cout << #a "=" &
lt;< (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned long long #define for (i,a,b) \ fo
R (i=a;a<b?i<=b:i>=b;a<b?i++:i--) const int MAXN = 30*30*500*2+30;
struct SAD { int L[MAXN], R[MAXN], U[MAXN], D[MAXN];
int ROW[MAXN], COL[MAXN];
int H[MAXN], S[MAXN];
int si;
int ans;
void init (int m) {for (int i=0;i<=m;i++) {U[i] = d[i] = i;
L[i] = i-1;
R[i] = i+1;
S[i] = 0;
} l[0] = m;
R[m] = 0;
CLR (h,-1);
Si=m;
Ans=inf;
} void link (int row,int col) {si++;
if (Si >= maxn) while (1);
Col[si] = col;
Row[si] = row;
s[col]++;
U[SI] = U[col];
D[si] = col;
D[u[col]] = si;
U[col] = si;
if (h[row]!=-1) {R[si] = H[row];
L[SI] = L[h[row]];
R[l[si]] = si;
L[r[si]] = si;
}else H[row] = L[si] = R[si] = si;
} 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]) {
U[D[J]] = j;
D[U[J]] = j;
s[col[j]]++;
}} R[l[c]] = c;
L[R[C]] = c;
} void Dfs (int cnt) {if (cnt >= ans) return;
if (r[0] = = 0) {ans = min (ans,cnt);
return;
} int c=r[0];
for (int i=r[0];i!=0;i=r[i]) if (s[c]>s[i]) c=i;
Remove (c);
For (int. I=d[c];i!=c;i=d[i]) {for (int j=r[i];j!=i;j=r[j]) Remove (Col[j]);
DFS (CNT+1);
for (int j=l[i];j!=i;j=l[j]) Resume (Col[j]);
} Resume (c);
}}DLX;
#define for (I,C,R) for (int i=c;i!=c;i=r[i]) int id[100][100]; int main () {//Freopen ("Input.txt", "R", stdin);
int T;
scanf ("%d", &t);
while (t--) {int n,m,p;
scanf ("%d%d%d", &n,&m,&p);
int si=0;
for (int i=0;i<n;i++) for (int j=0;j<m;j++) Id[i][j]=++si;
Dlx.init (SI);
Debug (SI);
int a,b,c,d;
for (int x=1;x<=p;x++) {scanf ("%d%d%d%d", &a,&b,&c,&d);
for (int. i=a;i<c;i++) for (int j=b;j<d;j++) Dlx.link (X,id[i][j]);
} dlx.dfs (0);
if (Dlx.ans = = inf) printf (" -1\n");
else printf ("%d\n", Dlx.ans);
} return 0;
}
POJ 3074
Sudoku One thing to note is that there are four restrictions, not three. They are rows, columns, blocks, and only one color per point.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <vector> #include <queue> #include <stack> #include <set> #include <map > #include <complex>/#include <tr1/unordered_set>//#include <tr1/unordered_map> #include < bitset>//#pragma comment (linker, "/stack:1024000000,1024000000") #define Lson L, M, rt<<1 #define Rson m+1, R, R
T<<1|1 #define INF (1e9+7) #define DEBUG (a) cout << #a "=" << (a) << Endl; #define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; } #define CLR (x, y) memset (x, y, sizeof x) #define LL Long long #define ULL unsigned LL #define FFOR (i,a,s) for (int i=a[s]
; i!=s;i=a[i]) using namespace std;
const int MAXN = 2E5;
/* struct sad{int L[MAXN],R[MAXN],U[MAXN],D[MAXN];
int ROW[MAXN],COL[MAXN];
int H[MAXN],S[MAXN]; int si;
Vector<int> ans;
void init (int m) {for (int i=0;i<=m;i++) {u[i]=d[i]=i;
L[i]=i-1;
r[i]=i+1;
s[i]=0;
} l[0]=m;
r[m]=0;
CLR (h,-1);
Ans.clear ();
Si=m;
} void link (int row,int col) {si++; Col[si] = col;
Row[si] = row;
s[col]++; U[SI] = U[col];
D[si] = col;
D[u[col]] = si;
U[col] = si;
if (H[row]! =-1) {R[si] = H[row];
L[SI] = L[h[row]];
R[l[si]] = si;
L[r[si]] = si;
}else H[row] = L[si] = R[si] = si;
} 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]) {U[d[j]] = j;
D[U[J]] = j;
s[col[j]]++;
}} R[l[c]] = l[r[c]] = c;
} bool Dfs () {if (r[0]==0) return true;
int c=r[0];
for (int i=r[0];i;i=r[i]) if (s[c]>s[i]) c=i;
Remove (c);
for (int i=d[c];i-c;i=d[i]) {ans.push_back (row[i]);
for (int j=r[i];j-i;j=r[j]) Remove (Col[j]);
if (Dfs ()) return true;
for (int j=l[i];j-i;j=l[j]) Resume (Col[j]);
Ans.pop_back ();
} Resume (c);
return false;
} void del (int c) {int i = h[c];
Remove (Col[i]);
for (int j=r[i];j-i;j=r[j]) Remove (Col[j]);
}}DLX;
*/struct sad{int L[MAXN],R[MAXN],U[MAXN],D[MAXN];
int ROW[MAXN],COL[MAXN];
int H[MAXN],S[MAXN];
int si; Vector<int>ans;
void init (int m) {for (int i=0;i<=m;i++) {U[i] = d[i] = i;
L[i] = i-1;
R[i] = i+1;
S[i] = 0;
} l[0] = m;
R[m] = 0;
CLR (h,-1);
Si = m;
Ans.clear ();
} void link (int row,int col) {si++; Col[si] = col;
Row[si] = row;
s[col]++; U[SI] = U[col];
D[si] = col;
D[u[col]] = si;
U[col] = si;
if (H[row]! =-1) {R[si] = H[row];
L[SI] = L[h[row]];
R[l[si]] = si;
L[r[si]] = si;
} else H[row] = L[si] = R[si] = si;
} 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]) {U[d[j]] = j;
D[U[J]] = j;
s[col[j]]++;
}} R[l[c]] = c;
L[R[C]] = c;
} bool Dfs () {if (r[0] = = 0) return true;
int c = 0; for (int i=r[0];i!=0;i=r[i]) {if (!c| |
S[c]>s[i]) c=i;
} Remove (c);
for (int i=d[c];i!=c;i=d[i]) {ans.push_back (row[i]);
for (int j=r[i];j!=i;j=r[j]) Remove (Col[j]);
if (Dfs ()) return true;
for (int j=l[i];j!=i;j=l[j]) Resume (Col[j]);
Ans.pop_back ();
} Resume (c);
return false;
} void del (int c) {int i = h[c];
Remove (Col[i]);
for (int j=r[i];j-i;j=r[j]) Remove (Col[j]);
}}DLX; int Hash (int c,int x,int y) {return c*81 + x*9 + y;
} void get (int v,int &c,int &x,int &y) {c = v/81;
x = v/9% 9;
y = v% 9;
} int col[20][20];
Char s[100];
int main () {//Freopen ("Input.txt", "R", stdin);
while (~SCANF ("%s", s)) {if (!strcmp (S, "end") is break;
for (int i=0;s[i];i++) {if (s[i]== '. ')
COL[I/9][I%9] =-1;
else col[i/9][i%9] = s[i]-' 0 ';
} dlx.init (36*9);
for (int x=0;x<9;x++) for (int y=0;y<9;y++) for (int c=0;c<9;c++) {
int h = Hash (c,x,y);
Dlx.link (H + 1, Hash (0,x,c) + 1);
Dlx.link (H + 1, Hash (1,y,c) + 1);
Dlx.link (H + 1, Hash (2,x/3+y/3*3,c) + 1);
Dlx.link (H +1, Hash (3,x,y) +1); } for (int x=0;x<9;x++) for (int y=0;y<9;y++) if (col[x][y]!=-1) {int c = Col[x][y]-1;
Dlx.del (Hash (c,x,y) + 1);
} Dlx.dfs ();
for (int i=0;i<dlx.ans.size (); i++) {int x,y,c;//debug (Dlx.ans[i]);
Get (Dlx.ans[i]-1, C, x, y);
Col[x][y] = c+1;
} for (int i=0;i<9;i++) for (int j=0;j<9;j++) printf ("%d", col[i][j]);
printf ("\ n");
} return 0;
}
ZOJ 3122
There is a problem with this problem.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set >
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment (linker, "/stack : 1024000000,1024000000 ")
#define Lson L, M, rt<<1
#define Rson m+1, R, rt<<1|1
#define INF ( 1E9+7)
#define DEBUG (a) cout << #a "=" << (a) << Endl;
#define Debugarry (A, n) for (int i = 0; I < (n); i++) {cout << #a "[" << i << "] =" << (a) [i] << Endl; }
#define CLR (x, y) memset (x, y, sizeof x)
#define LL Long long
#define ULL unsigned LL
#define FFOR (i , a,s) for (int i=a[s];i!=s;i=a[i])
using namespace