-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdag_path_cover.cpp
More file actions
194 lines (174 loc) · 4.99 KB
/
Copy pathdag_path_cover.cpp
File metadata and controls
194 lines (174 loc) · 4.99 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// DAG の最小パス被覆
//
// verified
// ABC 237 Ex - Hakata
// https://atcoder.jp/contests/abc237/tasks/abc237_h
//
#include <bits/stdc++.h>
using namespace std;
// DAG min path-cover by Hopcroft-Karp
struct DagPathCover {
const int NOT_MATCHED = -1;
// input
int V;
vector<vector<int>> list; // left to right
// results
vector<int> lr, rl;
// intermediate results
vector<vector<int>> rlist; // right to left
vector<bool> seen, matched;
vector<int> level;
// constructor
DagPathCover(int V) : V(V), list(V), rlist(V) {}
void add_edge(int from, int to) {
assert(from >= 0 && from < V);
assert(to >= 0 && to < V);
list[from].emplace_back(to);
rlist[to].emplace_back(from);
}
// getter, debugger
vector<int> &operator [] (int i) { return list[i]; }
const vector<int> &operator [] (int i) const { return list[i]; }
constexpr int size() const { return V; }
friend ostream& operator << (ostream& s, const DagPathCover& G) {
s << endl;
for (int i = 0; i < (int)G.list.size(); ++i) {
s << i << ": ";
for (int j = 0; j < (int)G.list[i].size(); ++j) {
s << G.list[i][j];
if (j + 1 != (int)G.list[i].size()) s << ", ";
}
s << endl;
}
return s;
}
// solver
void hobfs() {
queue<int> que;
for (int left = 0; left < V; ++left) {
level[left] = -1;
if (!matched[left]) {
que.push(left);
level[left] = 0;
}
}
level[V] = V;
while (!que.empty()) {
int left = que.front();
que.pop();
for (int right : list[left]) {
int next = rl[right];
if (next == NOT_MATCHED) next = V;
if (level[next] == -1) {
level[next] = level[left] + 1;
que.push(next);
}
}
}
}
bool hodfs(int left) {
if (left == V) return true;
if (seen[left]) return false;
seen[left] = true;
for (int right : list[left]) {
int next = rl[right];
if (next == NOT_MATCHED) next = V;
if (level[next] > level[left] && hodfs(next)) {
lr[left] = right;
rl[right] = left;
return true;
}
}
return false;
}
int solve() {
seen.assign(V, false);
matched.assign(V, false);
level.assign(V + 1, -1);
lr.assign(V, -1);
rl.assign(V, -1);
int max_matching = 0;
while (true) {
hobfs();
seen.assign(V, false);
bool finished = true;
for (int left = 0; left < V; ++left) {
if (!matched[left] && hodfs(left)) {
matched[left] = true;
++max_matching;
finished = false;
}
}
if (finished) break;
}
for (int r = 0; r < V; r++) {
if (rl[r] != NOT_MATCHED) lr[rl[r]] = r;
}
return V - max_matching;
}
// max stable set
vector<int> get_stable_set() {
vector<int> res;
for (int v = 0; v < V; v++) {
if (rl[v] == NOT_MATCHED) res.emplace_back(v);
}
return res;
}
// min path cover
vector<vector<int>> get_path_cover() {
auto srcs = get_stable_set();
vector<vector<int>> res;
for (auto s : srcs) {
vector<int> path;
int v = s;
while (v != NOT_MATCHED) {
path.emplace_back(v);
v = lr[v];
}
res.emplace_back(path);
}
return res;
}
};
//------------------------------//
// Examples
//------------------------------//
// ABC 237 Ex - Hakata
void ABC_237_Ex() {
string S;
cin >> S;
int N = S.size();
vector<string> ss;
for (int l = 0; l < N; l++) for (int r = l+1; r <= N; r++) {
bool ok = true;
for (int i = l, j = r-1; i < j; i++, j--) if (S[i] != S[j]) ok = false;
if (ok) ss.emplace_back(S.substr(l, r-l));
}
sort(ss.begin(), ss.end());
ss.erase(unique(ss.begin(), ss.end()), ss.end());
int V = (int)ss.size();
// does i include j ?
auto check = [&](int i, int j) -> bool {
for (int k = 0; k <= (int)ss[i].size() - (int)ss[j].size(); k++) {
bool same = true;
for (int l = 0; l < (int)ss[j].size(); l++) {
if (ss[i][l+k] != ss[j][l]) same = false;
}
if (same) return true;
}
return false;
};
DagPathCover G(V);
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (i == j) continue;
if (check(i, j)) G.add_edge(i, j);
}
}
int res = G.solve();
cout << res << endl;
}
int main() {
ABC_237_Ex();
}