HUST1017----Exact cover
来源:互联网 发布:linux vi 滚动几行 编辑:程序博客网 时间:2024/06/05 22:35
1017 - Exact cover
Time Limit: 15s Memory Limit: 128MB
Special Judge Submissions: 6023 Solved: 3188
- 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 73 1 4 72 1 43 4 5 73 3 5 64 2 3 6 72 2 7
- Sample Output
3 2 4 6
- Hint
- Source
dupeng
精确覆盖模板题
/************************************************************************* > File Name: hust1017.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2015年01月05日 星期一 19时17分39秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 1010;int k;int U[N * N], D[N * N], L[N * N], R[N * N];int C[N * N], ans[N];int cntr[N], cntc[N];int head;int row[N][N]; //表示第i行第j个为1的列是多少int col[N][N]; //表示第i列第j个为1的行是多少int n, m;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];cntc[C[j]]--;}}}void resume(int c){L[R[c]] = c;R[L[c]] = c;for (int i = D[c]; i != c; i = D[i]){for (int j = R[i]; j != i; j = R[j]){U[D[j]] = j;D[U[j]] = j;cntc[C[j]]++;}}}bool dance(){if (R[head] == head){return true;}int c;int mins = 0x3f3f3f3f;for (int i = R[head]; i != head; i = R[i]){if (mins > cntc[i]){c = i;mins = cntc[i];}}remove(c);for (int i = D[c]; i != c; i = D[i]){ans[k++] = (i - 1) / m;for (int j = R[i]; j != i; j = R[j]){remove(C[j]);}if (dance()){return true;}for (int j = L[i]; j != i; j = L[j]){resume(C[j]);}k--;}resume(c);return false;}bool build(){head = 0;for (int i = 0; i < m; ++i){R[i] = i + 1;L[i + 1] = i;}R[m] = 0;L[0] = m;for (int i = 1; i <= n; ++i){for (int j = 1; j < cntr[i]; ++j){R[row[i][j]] = row[i][j + 1];L[row[i][j + 1]] = row[i][j];}R[row[i][cntr[i]]] = row[i][1];L[row[i][1]] = row[i][cntr[i]];}for (int i = 1; i <= m; ++i){for (int j = 1; j < cntc[i]; ++j){D[col[i][j]] = col[i][j + 1];U[col[i][j + 1]] = col[i][j];}D[col[i][cntc[i]]] = i;D[i] = col[i][1];U[i] = col[i][cntc[i]];U[col[i][1]] = i; if (cntc[i] == 0){return false;}}return true;}int main(){int tmp;while (~scanf("%d%d", &n, &m)){for (int i = 1; i <= m; ++i){cntc[i] = 0;}for (int i = 1; i <= n; ++i){scanf("%d", &cntr[i]);for (int j = 1; j <= cntr[i]; ++j){scanf("%d", &tmp);row[i][j] = tmp + i * m;cntc[tmp]++;col[tmp][cntc[tmp]] = tmp + i * m;C[tmp + i * m] = tmp;}}if (build()){k = 0;if (dance()){printf("%d", k);for (int i = 0; i < k; ++i){printf(" %d", ans[i]);}printf("\n");}else{printf("NO\n");}}else{printf("NO\n");}}return 0;}
1 0
- HUST1017----Exact cover
- Exact cover(HUST1017)
- 【hust1017】【DLX】Exact cover
- hust1017 Exact cover DLX模板
- HUST1017--Exact cover(Dancing Links)
- HUST1017 Exact cover(Dancing Links)Kuangbin模板带解释
- hust1017-Exact cover 舞蹈链之精确覆盖(裸的)
- HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题
- 【DLX】Exact cover
- HUST 1017 Exact cover
- HUST 1017 Exact cover
- HUST 1017 Exact cover
- hust 1017 Exact cover
- hust 1017 Exact cover (DLX)
- [DLX] hust 1017 Exact cover
- hust 1017 - Exact cover (DLX)
- HUST 1017 Exact Cover [DLX]
- hust1017
- 恶补web之七:html DOM知识
- 协程系列索引
- java(1)
- poj 1141 Brackets Sequence(区间dp)
- qwt使用
- HUST1017----Exact cover
- ORACLE的Copy命令和create table,insert into的比较
- Net常见面试题整理(2)——装箱和拆箱
- 去掉UItableview header(footer)view黏性(sticky)
- CentOS 下用的是lnmp 的包配置Nginx 下的CI伪静态(搞爽了)
- ubuntu设置时区,网上同步时间
- uva 11817 - Tunnelling the Earth(几何)
- HR喜欢这样的简历(转自豆瓣)
- hdoj1014