HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover

时间限制:15秒 内存限制:128兆

自定评测 5584 次提交 2975 次通过


题目描述

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

题目链接:http://acm.hust.edu.cn/problem/show/1017

精确覆盖入门题。

Dancing Links 就是一种加快搜索速度的方法,采用四向链表。


  1 /* ***********************************************
2 Author :kuangbin
3 Created Time :2014/5/25 22:55:25
4 File Name :E:\2014ACM\专题学习\DLX\HUST1017.cpp
5 ************************************************ */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 const int maxnode = 100010;
21 const int MaxM = 1010;
22 const int MaxN = 1010;
23 struct DLX
24 {
25 int n,m,size;
26 int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
27 int H[MaxN], S[MaxM];
28 int ansd, ans[MaxN];
29 void init(int _n,int _m)
30 {
31 n = _n;
32 m = _m;
33 for(int i = 0;i <= m;i++)
34 {
35 S[i] = 0;
36 U[i] = D[i] = i;
37 L[i] = i-1;
38 R[i] = i+1;
39 }
40 R[m] = 0; L[0] = m;
41 size = m;
42 for(int i = 1;i <= n;i++)
43 H[i] = -1;
44 }
45 void Link(int r,int c)
46 {
47 ++S[Col[++size]=c];
48 Row[size] = r;
49 D[size] = D[c];
50 U[D[c]] = size;
51 U[size] = c;
52 D[c] = size;
53 if(H[r] < 0)H[r] = L[size] = R[size] = size;
54 else
55 {
56 R[size] = R[H[r]];
57 L[R[H[r]]] = size;
58 L[size] = H[r];
59 R[H[r]] = size;
60 }
61 }
62 void remove(int c)
63 {
64 L[R[c]] = L[c]; R[L[c]] = R[c];
65 for(int i = D[c];i != c;i = D[i])
66 for(int j = R[i];j != i;j = R[j])
67 {
68 U[D[j]] = U[j];
69 D[U[j]] = D[j];
70 --S[Col[j]];
71 }
72 }
73 void resume(int c)
74 {
75 for(int i = U[c];i != c;i = U[i])
76 for(int j = L[i];j != i;j = L[j])
77 ++S[Col[U[D[j]]=D[U[j]]=j]];
78 L[R[c]] = R[L[c]] = c;
79 }
80 //d为递归深度
81 bool Dance(int d)
82 {
83 if(R[0] == 0)
84 {
85 ansd = d;
86 return true;
87 }
88 int c = R[0];
89 for(int i = R[0];i != 0;i = R[i])
90 if(S[i] < S[c])
91 c = i;
92 remove(c);
93 for(int i = D[c];i != c;i = D[i])
94 {
95 ans[d] = Row[i];
96 for(int j = R[i]; j != i;j = R[j])remove(Col[j]);
97 if(Dance(d+1))return true;
98 for(int j = L[i]; j != i;j = L[j])resume(Col[j]);
99 }
100 resume(c);
101 return false;
102 }
103 };
104
105 DLX g;
106 int main()
107 {
108 //freopen("in.txt","r",stdin);
109 //freopen("out.txt","w",stdout);
110 int n,m;
111 while(scanf("%d%d",&n,&m) == 2)
112 {
113 g.init(n,m);
114 for(int i = 1;i <= n;i++)
115 {
116 int num,j;
117 scanf("%d",&num);
118 while(num--)
119 {
120 scanf("%d",&j);
121 g.Link(i,j);
122 }
123 }
124 if(!g.Dance(0))printf("NO\n");
125 else
126 {
127 printf("%d",g.ansd);
128 for(int i = 0;i < g.ansd;i++)
129 printf(" %d",g.ans[i]);
130 printf("\n");
131 }
132 }
133 return 0;
134 }

HUST 1017 - Exact cover (Dancing Links 模板题),布布扣,bubuko.com

时间: 2024-10-10 11:16:05

HUST 1017 - Exact cover (Dancing Links 模板题)的相关文章

HUST 1017 Exact cover (Dancing links)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 6110 次提交 3226 次通过 题目描述 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 o

Dancing Link --- 模板题 HUST 1017 - Exact cover

1017 - Exact cover Problem's Link:   http://acm.hust.edu.cn/problem/show/1017 Mean: 略 analyse: A Time complexity: O(n) Source code:  #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vecto

HUST 1017 Exact cover DLX

题意:一个n×m行的矩阵,每个格子可能是 0 或者1  现在让你选择 几列  使得 每一列 有且只有一个1.(精确覆盖模板题) 解题思路: dancing links 模板 关于dancing links  献上几篇必看论文 :http://par.buaa.edu.cn/acm-icpc/filepool/r/35/ 板子用的kuangbin的板子(还是适牛的..) 解题思路: 1 // File Name: hust1017.cpp 2 // Author: darkdream 3 // C

hustoj 1017 - Exact cover dancing link

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 5851 Solved: 3092 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 e

(简单) HUST 1017 Exact cover , DLX+精确覆盖。

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. DLX精确覆盖的模板题...... 代码如下: //HUST 1

[DLX] hust 1017 Exact cover

题意: 给你N个包,要拿到M个东西(编号1~M每个只能有一个) 然后每个包里有k个东西,每个东西都有编号. 思路: 舞蹈连模板题 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostream" #include"queue" #include"map" #include"vector

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 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 e

Dancing Links [Kuangbin带你飞] 模版及题解

学习资料: http://www.cnblogs.com/grenet/p/3145800.html http://blog.csdn.net/mu399/article/details/7627862 2份模版 第一份精确覆盖 from POJ 3074 const int N = 9; const int MAXN = N * N * N + 10; const int MAXM = N * N * 4 + 10; const int MAXNODE = MAXN * 4 + MAXM +

ZOJ 3209 Treasure Map (Dancing Links)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luck