hust 1017 - Exact cover (DLX)
2015-10-09 15:30
351 查看
1017 - Exact cover
时间限制:15秒 内存限制:128兆自定评测 6414
次提交 3374 次通过
题目描述
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.
输入
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.
输出
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".
样例输入
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
复习一下dancing link
/*====================================================== # Author: whai # Last modified: 2015-10-08 15:25 # Filename: hust1017_v1.cpp ======================================================*/ #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <set> #include <map> using namespace std; #define LL __int64 #define PB push_back #define P pair<int, int> #define X first #define Y second const int MAXN = 1010; const int MAXM = 1010; const int N = MAXN * MAXM; struct DLX { int n, m, size; int U , D , R , L , row , col ; int H[MAXN], S[MAXM]; int ans[MAXN], ans_cnt; //ans_cnt表示覆盖所需要的数量,ans保存覆盖所需要的部分 void init(int _n, int _m) { ans_cnt = -1; 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; } void dance(int d) { //剪枝下 //if(ans_cnt != -1 && ans_cnt <= d) return; //改,根据题目而定,如果要求用最小的东西覆盖,则把这个加上 if (R[0] == 0) { //if(ans_cnt == -1) ans_cnt = d; //改,根据题目而定,如果要求用最小的东西覆盖,则把这个加上 //else if(d < ans_cnt) ans_cnt = d; //改,根据题目而定,如果要求用最小的东西覆盖,则把这个加上 ans_cnt = 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); if (ans_cnt != -1) return ; //改,根据题目而定,如果要求用最小的东西覆盖,则把这个去掉 for (int j = L[i]; j != i; j = L[j]) resume(col[j]); } resume(c); } }dlx; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { dlx.init(n, m); for(int i = 1; i <= n; ++i) { int c, j; scanf("%d", &c); while(c--) { scanf("%d", &j); dlx.link(i, j); } } dlx.dance(0); if(dlx.ans_cnt == -1) puts("NO"); else { printf("%d", dlx.ans_cnt); for(int i = 0; i < dlx.ans_cnt; ++i) { printf(" %d", dlx.ans[i]); } puts(""); } } return 0; }
相关文章推荐
- Tomcat 自启动环境参数的设置及 Linux 脚本的启动顺序
- i.MX6 Gigabit Ethernet
- Struts2 使用通配符动态请求Action
- vector动态数组的应用
- ubuntu12.04编译llvm-3.2错误记录
- HDU 5119 Happy Matt Friends
- LeetCode 204-Count Primes
- 数值 转换 成 带千位符的数值,且转成大写
- Struts2 使用通配符动态请求Action
- android meta-data 使用
- 安卓在4.3的系统中提供了低功耗蓝牙Bluetooth Low Energy
- 使用Java的Graphics类进行绘图的方法详解
- HDU 4902 Nice boat 线段树 区间更新
- AndroidManifest.xml文件详解(uses-feature)
- chcon可实现对文件的SEAndroid安全标签的修改
- javascript常用对象
- jquery ajax post格式
- 【FAQ】Gerrit创建xxx/xxx这种多目录层次类型的project报错?
- 五种创建UIImage的类方法
- Kubernetes管理基本教程