标签:
精确覆盖的模板题, 把每一个格子当成一列就可以。
S忘记初始化TLE N次, 哭晕在厕所......
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 const int maxn = 10205; 20 const int maxNode = 520005; 21 struct DLX { 22 int L[maxNode], R[maxNode], U[maxNode], D[maxNode], row[maxNode], col[maxNode]; 23 int S[maxn], H[maxn], deep, sz, n, m, num; 24 void remove(int c) { 25 L[R[c]] = L[c]; 26 R[L[c]] = R[c]; 27 for(int i = D[c]; i!=c; i = D[i]) 28 for(int j = R[i]; j!=i; j = R[j]) { 29 U[D[j]] = U[j]; 30 D[U[j]] = D[j]; 31 S[col[j]]--; 32 } 33 } 34 void resume(int c) { 35 for(int i = U[c]; i!=c; i = U[i]) 36 for(int j = L[i]; j!=i; j = L[j]) { 37 S[col[j]]++; 38 D[U[j]] = j; 39 U[D[j]] = j; 40 } 41 R[L[c]] = c; 42 L[R[c]] = c; 43 } 44 void dfs(int d) { 45 if(d>=deep) 46 return ; 47 if(R[0] == 0) { 48 deep = min(d, deep); 49 return ; 50 } 51 int c = R[0]; 52 for(int i = R[0]; i!=0; i = R[i]) 53 if(S[c]>S[i]) 54 c = i; 55 remove(c); 56 for(int i = D[c]; i!=c; i = D[i]) { 57 for(int j = R[i]; j!=i; j = R[j]) 58 remove(col[j]); 59 dfs(d+1); 60 for(int j = L[i]; j!=i; j = L[j]) 61 resume(col[j]); 62 } 63 resume(c); 64 return ; 65 } 66 void add(int r, int c) { 67 sz++; 68 col[sz] = c; 69 S[c]++; 70 U[sz] = U[c]; 71 D[sz] = c; 72 D[U[c]] = sz; 73 U[c] = sz; 74 if(~H[r]) { 75 R[sz] = H[r]; 76 L[sz] = L[H[r]]; 77 L[R[sz]] = sz; 78 R[L[sz]] = sz; 79 } else { 80 H[r] = L[sz] = R[sz] = sz; 81 } 82 } 83 void init(){ 84 mem1(H); 85 for(int i = 0; i<=n; i++) { 86 R[i] = i+1; 87 L[i] = i-1; 88 D[i] = U[i] = i; 89 } 90 R[n] = 0; 91 L[0] = n; 92 mem(S); 93 sz = n; 94 deep = inf; 95 } 96 void solve() { 97 scanf("%d%d%d", &n, &m, &num); 98 n *= m; 99 init(); 100 int x1, y1, x2, y2; 101 int cnt = 1; 102 while(num--) { 103 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 104 for(int i = x1+1; i<=x2; i++) { 105 for(int j = y1+1; j<=y2; j++) { 106 add(cnt, (i-1)*m+j); 107 } 108 } 109 cnt++; 110 } 111 dfs(0); 112 if(deep == inf) { 113 puts("-1"); 114 } else { 115 printf("%d\n", deep); 116 } 117 } 118 }dlx; 119 int main() 120 { 121 int t; 122 cin>>t; 123 while(t--) { 124 dlx.solve(); 125 } 126 }
标签:
原文地址:http://www.cnblogs.com/yohaha/p/5037130.html