舞蹈链..Exact cover&Treasure Map等 . DLX-精确覆盖
2015-07-28 21:11
369 查看
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/A
Description
There is an N*M matrix with only 0s and 1s, (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 1s 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 should just output any of them. If there are no selection, just output “NO”.
Sample Input
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
Sample Output
3 2 4 6
跟着bin神的专题刷的…
舞蹈链的第一个题呀!直接用上舞蹈链的精确覆盖的模板就可以了, 因为这个并没有什么构图, 就是给定的01矩阵求覆盖.
学习舞蹈链,可以看bin神给的blog…模板我也是用bin神的.
bin神的模板跟大白书上面有略有不同, 不过看懂了之后不难理解.
/content/3695284.html
/article/8858426.html
上面两位大牛写的很清晰易懂..
通俗地说一下舞蹈链的应用就是:
行是自己拥有策略
列是要完成的任务目标
这样就构成了一个01矩阵
最终要完成把所有的任务目标填为1
如果使精确覆盖就是每一列的1不可以重复,重复覆盖即可以重复的.(两者的模板略有不同, 主要是选中列后对列的删除操作不一样)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/B
第二题是问你是否可以用给定的矩形将这个大的区域(矩形)填满. 因为数据很小, 把每一个小方格看做为一个任务目标(列), 一个矩形对应着一行,有多少个小方格,就在小方格的位置上置1,其余为0, 构图好了之后, 就直接用模板搜.
Description
There is an N*M matrix with only 0s and 1s, (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 1s 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 should just output any of them. If there are no selection, just output “NO”.
Sample Input
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
Sample Output
3 2 4 6
跟着bin神的专题刷的…
舞蹈链的第一个题呀!直接用上舞蹈链的精确覆盖的模板就可以了, 因为这个并没有什么构图, 就是给定的01矩阵求覆盖.
学习舞蹈链,可以看bin神给的blog…模板我也是用bin神的.
bin神的模板跟大白书上面有略有不同, 不过看懂了之后不难理解.
/content/3695284.html
/article/8858426.html
上面两位大牛写的很清晰易懂..
通俗地说一下舞蹈链的应用就是:
行是自己拥有策略
列是要完成的任务目标
这样就构成了一个01矩阵
最终要完成把所有的任务目标填为1
如果使精确覆盖就是每一列的1不可以重复,重复覆盖即可以重复的.(两者的模板略有不同, 主要是选中列后对列的删除操作不一样)
#include <cmath> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <iostream> #define LL long long #define pb push_back #define lb lower_bound #define eps 1e-8 #define INF 0x3f3f3f3f using namespace std; const int N = 1000; const int maxnode = N*N+N+50; struct DLX { int n,m,size; /// 上下右左指针 row 行 col 列 结点编号 int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode]; int H[N+50],S[N+50];/// H 行头 S 每列结点计数 int ansd,ans[N+50];/// ansd 答案计数 ans 记录答案 void init(int _n,int _m) { n = _n; m = _m; for(int i = 0;i <= m;i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i-1; R[i] = i+1; } R[m] = 0; L[0] = m; size = m; for(int i = 1;i <= n;i++)H[i] = -1; } void Link(int r,int c) { ++S[Col[++size]=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; printf("%d", ansd); for(int i=0; i<ansd; i++) printf(" %d", ans[i]); puts(""); return true; } int c = R[0]; for(int i = R[0];i != 0;i = R[i]) if(S[i] < S[c]) c = i; remove(c); for(int i = D[c];i != c;i = D[i]) { ans[d] = Row[i]; for(int j = R[i];j != i;j = R[j])remove(Col[j]); if(Dance(d+1))return true; for(int j = L[i];j != i;j = L[j])resume(Col[j]); } resume(c); return false; } }; DLX dlx; int n, m; void solve() { while(~scanf("%d %d", &n, &m)) { dlx.init(n, m); int c, wh; for(int i=0; i<n; i++) { scanf("%d", &c); for(int j=0; j<c; j++) { scanf("%d", &wh); dlx.Link(i+1, wh); } } if(dlx.Dance(0) == false) puts("NO"); } } int main(void) { #ifdef DK freopen("/home/dk/桌面/1.in","r",stdin); #endif // DK solve(); return 0; }
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/B
第二题是问你是否可以用给定的矩形将这个大的区域(矩形)填满. 因为数据很小, 把每一个小方格看做为一个任务目标(列), 一个矩形对应着一行,有多少个小方格,就在小方格的位置上置1,其余为0, 构图好了之后, 就直接用模板搜.
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<string> #include<stack> #include<queue> #include<vector> #include<map> #include<set> #include<iostream> #define pb push_back #define INF 0x3f3f3f3f using namespace std; typedef unsigned long long ULL; typedef long long LL; const int mod = 1000000007; const int N = 35; const int M = 505; const int maxnode = N*N*N*2 + M + 50; struct DLX { int n,m,size; int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode]; int H[N*N],S[N*N]; int ansd,ans[N*N]; void init(int _n,int _m) { n = _n; m = _m; for(int i = 0;i <= m;i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i-1; R[i] = i+1; } R[m] = 0; L[0] = m; size = m; for(int i = 1;i <= n;i++)H[i] = -1; ansd = -1; } void Link(int r,int c) { ++S[Col[++size]=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; } void Dance(int d) { if(ansd != -1 && ansd <= d) return ;/// 这里对超过最小答案的情况进行剪枝...不然会光荣超时.. if(R[0] == 0) { if(ansd == -1) ansd = d; ansd = min(ansd, d); return ; } int c = R[0]; for(int i = R[0];i != 0;i = R[i]) if(S[i] < S[c]) c = i; remove(c); for(int i = D[c];i != c;i = D[i]) { ans[d] = Row[i]; for(int j = R[i];j != i;j = R[j])remove(Col[j]); Dance(d+1); for(int j = L[i];j != i;j = L[j])resume(Col[j]); } resume(c); } }; DLX dlx; void solve() { int cas, n, m, p;; scanf("%d", &cas); while(cas--) { scanf("%d %d %d", &n, &m, &p); dlx.init(p, n*m); for(int i=1; i<=p; i++) { int x1, x2, y1, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); for(int j=x1+1; j<=x2; j++) for(int k=y1+1; k<=y2; k++) dlx.Link(i, (j-1)*m+k); } dlx.Dance(0); printf("%d\n", dlx.ansd); } } int main(void) { #ifdef DK freopen("/home/dk/桌面/1.in","r",stdin); #endif // DK solve(); return 0; }
相关文章推荐
- Codeforces Round #292 (Div. 1) C. Drazil and Park
- 3. Java面向对象之泛型-指定多个泛型
- 《iOS核心动画高级技巧》part1-CALayer
- 3. Java面向对象之泛型-指定多个泛型
- 自定义控件(三)自定义圆角Button
- HDU 5317 RGCDQ
- cmd命令中遇到文件路径中含有空格
- 扩展欧几里得算法(两行代码求最大公约数)
- 2015 Multi-University Training Contest 3 hdu 5326 Work
- ACM/ICPC World Finals 2010 D Castles
- hdu 1004 Let the Balloon Rise(水)
- 壁纸 - 精选热门高清图片和主题专辑
- jdk配置
- 笔试/面试题记录
- Cocos2d-JS开发常见问题汇总
- cocos2d-x 入门游戏:许特尔图_3 of 4
- TCP block方式时,接收端接收数据太慢,导致发送端阻塞
- Java替换字符或十进制数的字符串
- 上传项目到Github
- 在虚拟机中部署dpdk