-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
81 lines (79 loc) · 2.02 KB
/
helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <vector>
using namespace std;
#define FOR(i, way, to) for(int i = way[to] ; i != to ; i = way[i])
namespace DLX {
int col_size, sz;
vector<int> s, l, r, u, d, row, col, current;
void Init(int n) {
s.clear();
l.clear();
r.clear();
u.clear();
d.clear();
row.clear();
col.clear();
current.clear();
col_size = n;
for (int i = 0; i <= n; ++ i) {
u.emplace_back(i);
d.emplace_back(i);
l.emplace_back(i - 1);
r.emplace_back(i + 1);
}
s.resize(n + 1, 0);
row.resize(n + 1, 0);
col.resize(n + 1, 0);
r[n] = 0, l[0] = n, sz = n + 1;
}
void AddRow(int rr, vector<int> &sol) {
int tmp = sz;
for (auto to : sol) {
l.emplace_back(sz - 1);
r.emplace_back(sz + 1);
d.emplace_back(to);
u.emplace_back(u[to]);
d[u[to]] = sz, u[to] = sz;
row.emplace_back(rr);
col.emplace_back(to);
s[to] ++, sz ++;
}
r[sz - 1] = tmp, l[tmp] = sz - 1;
}
void Remove(int c) {
l[r[c]] = l[c];
r[l[c]] = r[c];
FOR(i, d, c) FOR(j, r, i) {
u[d[j]] = u[j];
d[u[j]] = d[j];
--s[col[j]];
}
}
void Restore(int c) {
FOR(i, u, c) FOR(j, l, i) {
++s[col[j]], u[d[j]] = d[u[j]] = j;
}
l[r[c]] = r[l[c]] = c;
}
void Dfs(int floor) {
if(r[0] == 0) return;
int c = r[0];
FOR(i, r, 0) {
if (s[i] <= s[c]) c = i;
if (s[i] == 0) return;
}
Remove(c);
FOR(i, d, c) {
FOR(j, r, i) Remove(col[j]);
current.emplace_back(row[i]);
Dfs(floor + 1);
if (r[0] == 0) return;
current.pop_back();
FOR(j, l, i) Restore(col[j]);
}
Restore(c);
}
vector<int> Solver() {
return Dfs(0), current;
}
}
#undef FOR