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 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
提示
来源
dupeng

本人智商奇低,看了三天才学会,模板验证题。

顺便一提这是正式转入C++后第一A。

  1 #include<iostream>
  2 #include<cstdio>
  3 using    namespace    std;
  4
  5 const    int    HEAD = 0;
  6 const    int    N = 1005;
  7 int    MAP[N][N];
  8 int    U[N * N],D[N * N],L[N * N],R[N * N],H[N * N],C[N * N],ANS[N * N];
  9
 10 void    ini(int col);
 11 bool    dancing(int k);
 12 void    output(void);
 13 void    remove(int c);
 14 void    resume(int c);
 15 int    main(void)
 16 {
 17     int    n,m,num,col;
 18     int    count,front,first;
 19
 20     while(cin >> n >> m)
 21     {
 22         ini(m);
 23
 24         count = m + 1;
 25         for(int i = 1;i <= n;i ++)
 26         {
 27             cin >> num;
 28             front = first = count;
 29             while(num --)
 30             {
 31                 cin >> col;
 32
 33                 U[count] = U[col];
 34                 D[count] = col;
 35                 L[count] = front;
 36                 R[count] = first;
 37
 38                 D[U[col]] = count;
 39                 U[col] = count;
 40                 R[front] = count;
 41
 42                 H[count] = i;
 43                 C[count] = col;
 44                 front = count;
 45                 count ++;
 46             }
 47             L[first] = count - 1;
 48         }
 49         if(!dancing(1))
 50             cout << "NO" << endl;
 51     }
 52
 53     return    0;
 54 }
 55
 56 void    ini(int col)
 57 {
 58     U[HEAD] = D[HEAD] = H[HEAD] = C[HEAD] = HEAD;
 59     R[HEAD] = 1;
 60     L[HEAD] = col;
 61
 62     int    front = HEAD;
 63     for(int i = 1;i <= col;i ++)
 64     {
 65         U[i] = D[i] = i;
 66         L[i] = front;
 67         R[i] = HEAD;
 68         R[front] = i;
 69         front = i;
 70
 71         C[i] = i;
 72         H[i] = 0;
 73     }
 74 }
 75
 76 bool    dancing(int k)
 77 {
 78     int    c = R[HEAD];
 79     if(c == HEAD)
 80     {
 81         output();
 82         return    true;
 83     }
 84
 85     remove(C[c]);
 86     for(int i = D[c];i != c;i = D[i])
 87     {
 88         ANS[k] = H[i];
 89         for(int j = R[i];j != i;j = R[j])
 90             remove(C[j]);
 91         if(dancing(k + 1))
 92             return    true;
 93         for(int j = L[i];j != i;j = L[j])
 94             resume(C[j]);
 95     }
 96     resume(C[c]);
 97
 98     return    false;
 99 }
100
101 void    output(void)
102 {
103     int    i,j;
104     for(i = 1;ANS[i];i ++);
105     cout << i - 1 << " ";
106     for(j = 1;j < i - 1;j ++)
107         cout << ANS[j] << " ";
108     cout << ANS[j] << endl;
109 }
110
111 void    remove(int c)
112 {
113     R[L[c]] = R[c];
114     L[R[c]] = L[c];
115
116     for(int i = D[c];i != c;i = D[i])
117         for(int j = R[i];j != i;j = R[j])
118         {
119             D[U[j]] = D[j];
120             U[D[j]] = U[j];
121         }
122 }
123
124 void    resume(int c)
125 {
126     R[L[c]] = c;
127     L[R[c]] = c;
128
129     for(int i = U[c];i != c;i = U[i])
130         for(int j = R[i];j != i;j = R[j])
131         {
132             D[U[j]] = j;
133             U[D[j]] = j;
134         }
135 }
时间: 2024-11-05 06:12:01

HUST 1017 Exact cover (Dancing links)的相关文章

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 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

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

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

[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 +

hust 1017 dancing links 精确覆盖模板题

最基础的dancing links的精确覆盖题目 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 #define N 1005 8 #define MAXN 1000100 9 10 struct DLX{ 11 int n , m , size;//size表示当前dlx表中有多少