#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
vector grid(N, vector<char>(M));
for (auto& v : grid) {
for (auto& x : v) {
std::cin >> x;
}
}
const int V = N * M + 2;
const int src = N * M + 0;
const int dst = N * M + 1;
struct Edge {
int to, cap, rev;
};
vector<vector<Edge>> graph(V);
const auto add_edge = [&](const int u, const int v, const int f) {
graph[u].push_back(Edge{v, f, (int)graph[v].size()});
graph[v].push_back(Edge{u, 0, (int)graph[u].size() - 1});
};
int ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (grid[i][j] == '#') {
ans += 1;
if (i + 1 < N and grid[i + 1][j] == '#') {
ans -= 1;
const int u = i * M + j;
const int v = u + M;
add_edge(u, dst, 1);
add_edge(v, u, 1);
}
if (j + 1 < M and grid[i][j + 1] == '#') {
ans -= 1;
const int u = i * M + j;
const int v = u + 1;
add_edge(src, u, 1);
add_edge(u, v, 1);
}
}
}
}
vector<char> done(V);
const auto dfs = RecLambda([&](auto&& dfs, const int u) -> bool {
if (u == dst) return true;
if (done[u]) return false;
done[u] = true;
for (auto& [v, c, r] : graph[u]) {
if (c == 0) continue;
if (dfs(v)) {
c -= 1;
graph[v][r].cap += 1;
return true;
}
}
return false;
});
while (true) {
std::fill(done.begin(), done.end(), false);
if (dfs(src)) {
ans += 1;
} else {
break;
}
}
std::cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
716 KB |
Output is correct |
3 |
Correct |
5 ms |
716 KB |
Output is correct |
4 |
Correct |
6 ms |
716 KB |
Output is correct |
5 |
Correct |
8 ms |
716 KB |
Output is correct |
6 |
Correct |
9 ms |
844 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
716 KB |
Output is correct |
3 |
Correct |
5 ms |
716 KB |
Output is correct |
4 |
Correct |
6 ms |
716 KB |
Output is correct |
5 |
Correct |
8 ms |
716 KB |
Output is correct |
6 |
Correct |
9 ms |
844 KB |
Output is correct |
7 |
Correct |
3 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
5 ms |
588 KB |
Output is correct |
20 |
Correct |
11 ms |
844 KB |
Output is correct |
21 |
Correct |
15 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
572 KB |
Output is correct |
23 |
Correct |
1 ms |
580 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
23 ms |
844 KB |
Output is correct |
26 |
Correct |
30 ms |
1316 KB |
Output is correct |
27 |
Correct |
23 ms |
1612 KB |
Output is correct |
28 |
Correct |
47 ms |
1868 KB |
Output is correct |