This documentation is automatically generated by online-judge-tools/verification-helper
グリッドは市松に考えると二部グラフ!!!(素振り1000回!)
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/Graph/Flow/Dinic.hpp"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
/*
* AtCoder Library Practice Contest-D Maxflow
* https://atcoder.jp/contests/practice2/submissions/48902875
*/
void solve() {
using namespace zawa;
int n, m; std::cin >> n >> m;
std::vector<std::string> s(n);
for (auto& v : s) std::cin >> v;
constexpr int dx[4]{ 1, 0, -1, 0 };
constexpr int dy[4]{ 0, 1, 0, -1 };
auto f{[&](int x, int y) -> int {
return x * m + y;
}};
auto sign{[&](int x, int y) -> int {
return (x % 2) ^ (y % 2);
}};
auto in{[&](int x, int y) -> bool {
return 0 <= x and x < n and 0 <= y and y < m;
}};
Dinic<int> maxflow(n * m + 2);
std::vector edges(n * m + 2, std::vector<u32>{});
for (int i{} ; i < n ; i++) for (int j{} ; j < m ; j++) {
if (s[i][j] == '#') continue;
if (sign(i, j) == 0) {
maxflow.addEdge(n * m, f(i, j), 1);
for (int d{} ; d < 4 ; d++) {
int ni{i + dx[d]}, nj{j + dy[d]};
if (!in(ni, nj)) continue;
if (s[ni][nj] == '#') continue;
edges[f(i, j)].push_back(maxflow.addEdge(f(i, j), f(ni, nj), 1));
}
}
else {
maxflow.addEdge(f(i, j), n * m + 1, 1);
}
}
int ans{maxflow.flow(n * m, n * m + 1)};
std::cout << ans << '\n';
std::vector<std::string> t(n, std::string(m, '.'));
auto g{[&](int v) -> std::pair<int, int> {
return std::pair{ v / m, v % m };
}};
for (int v{} ; v < n + m - 1 ; v++) {
for (int i{std::max(0, v - m + 1)} ; i < std::min(n, v + 1) ; i++) {
int j{v - i};
if (s[i][j] == '#') {
t[i][j] = '#';
continue;
}
if (sign(i, j) % 2 == 1) continue;
for (auto e : edges[f(i, j)]) {
if (maxflow.residual(e) == 1) continue;
auto [x, y]{ g(maxflow.to(e)) };
assert(in(x, y));
assert(s[x][y] == '.');
assert(t[x][y] == '.');
int dx{ x - i }, dy{ y - j };
if (dx == -1 and dy == 0) {
t[x][y] = 'v';
t[i][j] = '^';
}
else if (dx == 1 and dy == 0) {
t[x][y] = '^';
t[i][j] = 'v';
}
else if (dx == 0 and dy == -1) {
t[x][y] = '>';
t[i][j] = '<';
}
else if (dx == 0 and dy == 1) {
t[x][y] = '<';
t[i][j] = '>';
}
else {
assert(false);
}
}
}
}
for (const auto& v : t) std::cout << v << '\n';
}
int main() {
#ifdef ATCODER
solve();
#else
std::cout << "Hello World" << '\n';
#endif
}
#line 1 "Test/Manual/practice2_d.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
#line 2 "Src/Template/IOSetting.hpp"
#line 2 "Src/Template/TypeAlias.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#line 4 "Src/Template/IOSetting.hpp"
#include <iostream>
#include <iomanip>
namespace zawa {
void SetFastIO() {
std::cin.tie(nullptr)->sync_with_stdio(false);
}
void SetPrecision(u32 dig) {
std::cout << std::fixed << std::setprecision(dig);
}
} // namespace zawa
#line 2 "Src/Graph/Flow/Dinic.hpp"
#line 2 "Src/Utility/U32Pair.hpp"
#line 4 "Src/Utility/U32Pair.hpp"
#include <functional>
#line 7 "Src/Utility/U32Pair.hpp"
namespace zawa {
class U32Pair {
private:
static constexpr u32 SHIFT{32};
static constexpr u32 MASK{static_cast<u32>((1LL << SHIFT) - 1)};
u64 value_{};
public:
constexpr U32Pair() {}
constexpr U32Pair(u32 first, u32 second) {
value_ = (static_cast<u64>(first) << SHIFT) | second;
}
constexpr u32 first() const noexcept {
return static_cast<u32>(value_ >> SHIFT);
}
constexpr u32 second() const noexcept {
return static_cast<u32>(value_ & MASK);
}
constexpr u64 combined() const noexcept {
return value_;
}
constexpr U32Pair& operator=(const U32Pair& rhs) {
value_ = rhs.value_;
return *this;
}
friend constexpr bool operator==(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ == rhs.value_;
}
friend constexpr bool operator!=(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ != rhs.value_;
}
friend constexpr bool operator<(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ < rhs.value_;
}
friend constexpr bool operator<=(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ <= rhs.value_;
}
friend constexpr bool operator>(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ > rhs.value_;
}
friend constexpr bool operator>=(const U32Pair& lhs, const U32Pair& rhs) {
return lhs.value_ >= rhs.value_;
}
friend std::ostream& operator<<(std::ostream& os, const U32Pair& pair) {
os << '(' << pair.first() << ',' << pair.second() << ')';
return os;
}
};
struct U32PairHash {
usize operator()(const U32Pair& pair) const noexcept {
return std::hash<u64>{}(pair.combined());
}
};
} // namespace zawa
#line 5 "Src/Graph/Flow/Dinic.hpp"
#include <algorithm>
#include <cassert>
#include <limits>
#include <type_traits>
#include <vector>
#include <queue>
namespace zawa {
template <class Cap>
class Dinic {
private:
static_assert(std::is_signed_v<Cap>, "Cap must be signed");
usize n_{}, m_{};
static constexpr u32 invalid() noexcept {
return std::numeric_limits<u32>::max();
}
public:
inline usize size() const noexcept {
return n_;
}
inline usize edgeNumber() const noexcept {
return m_;
}
private:
struct Edge {
u32 to{}, rev{};
Cap residual{};
Edge() = default;
Edge(u32 to, u32 rev, const Cap& residual)
: to{to}, rev{rev}, residual{residual} {}
};
std::vector<std::vector<Edge>> g_;
std::vector<U32Pair> edges_;
std::vector<u32> label_, cur_;
bool dualStep(u32 s, u32 t) {
std::fill(label_.begin(), label_.end(), invalid());
label_[s] = 0;
std::queue<u32> queue{ { s } };
while (queue.size()) {
u32 v{queue.front()};
queue.pop();
for (const Edge& e : g_[v]) if (e.residual > 0) {
if (label_[e.to] > label_[v] + 1) {
label_[e.to] = label_[v] + 1;
if (e.to == t) return true;
queue.emplace(e.to);
}
}
}
return false;
}
bool admissible(u32 v, const Edge& e) const noexcept {
return e.residual > 0 and label_[v] + 1 == label_[e.to];
}
inline void flow(Edge& e, Cap f) {
e.residual -= f;
g_[e.to][e.rev].residual += f;
}
Cap dfs(u32 v, u32 t, Cap up) {
if (v == t) return up;
Cap res{};
for (u32& i{cur_[v]} ; i < g_[v].size() ; i++) {
if (!admissible(v, g_[v][i])) continue;
Cap f{dfs(g_[v][i].to, t, std::min(g_[v][i].residual, up - res))};
if (f == 0) continue;
flow(g_[v][i], f);
res += f;
if (res == up) return res;
}
return res;
}
Cap primalStep(u32 s, u32 t) {
std::fill(cur_.begin(), cur_.end(), 0u);
cur_[t] = g_[t].size();
Cap res{};
while (true) {
Cap f{dfs(s, t, std::numeric_limits<Cap>::max())};
if (f == 0) break;
res += f;
}
return res;
}
const Edge& edge(u32 i) const noexcept {
return g_[edges_[i].first()][edges_[i].second()];
}
const Edge& reverse(u32 i) const noexcept {
const Edge& e{edge(i)};
return g_[e.to][e.rev];
}
public:
Dinic() = default;
Dinic(u32 n, u32 m = 0u)
: n_{n}, m_{m}, g_(n), edges_{}, label_(n), cur_(n) {
g_.shrink_to_fit();
label_.shrink_to_fit();
cur_.shrink_to_fit();
edges_.reserve(m);
}
u32 addEdge(u32 u, u32 v, const Cap& cap) {
assert(u < size());
assert(v < size());
u32 id{static_cast<u32>(g_[u].size())};
u32 revId{u == v ? id + 1 : static_cast<u32>(g_[v].size())};
u32 res{static_cast<u32>(edges_.size())};
edges_.emplace_back(u, id);
g_[u].emplace_back(v, revId, cap);
g_[v].emplace_back(u, id, Cap{});
m_++;
return res;
}
const Cap& flowed(u32 id) const noexcept {
assert(id < edgeNumber());
return reverse(id).residual;
}
const Cap& residual(u32 id) const noexcept {
assert(id < edgeNumber());
return edge(id).residual;
}
const Cap& capacity(u32 id) const noexcept {
assert(id < edgeNumber());
return edge(id).residual + reverse(id).residual;
}
const u32& from(u32 id) const noexcept {
assert(id < edgeNumber());
return edges_[id].first();
}
const u32& to(u32 id) const noexcept {
assert(id < edgeNumber());
return edge(id).to;
}
Cap flow(u32 s, u32 t) {
assert(s < size());
assert(t < size());
Cap res{};
while (dualStep(s, t)) {
res += primalStep(s, t);
}
return res;
}
std::vector<bool> cut(u32 s) {
std::vector<bool> res(size());
res[s] = true;
std::queue<u32> queue{ { s } };
while (queue.size()) {
u32 v{queue.front()};
queue.pop();
for (const auto& e : g_[v]) if (e.residual > 0 and !res[e.to]) {
res[e.to] = true;
queue.emplace(e.to);
}
}
return res;
}
};
} // namespace zawa
#line 5 "Test/Manual/practice2_d.test.cpp"
#line 9 "Test/Manual/practice2_d.test.cpp"
#include <string>
#include <utility>
#line 12 "Test/Manual/practice2_d.test.cpp"
/*
* AtCoder Library Practice Contest-D Maxflow
* https://atcoder.jp/contests/practice2/submissions/48902875
*/
void solve() {
using namespace zawa;
int n, m; std::cin >> n >> m;
std::vector<std::string> s(n);
for (auto& v : s) std::cin >> v;
constexpr int dx[4]{ 1, 0, -1, 0 };
constexpr int dy[4]{ 0, 1, 0, -1 };
auto f{[&](int x, int y) -> int {
return x * m + y;
}};
auto sign{[&](int x, int y) -> int {
return (x % 2) ^ (y % 2);
}};
auto in{[&](int x, int y) -> bool {
return 0 <= x and x < n and 0 <= y and y < m;
}};
Dinic<int> maxflow(n * m + 2);
std::vector edges(n * m + 2, std::vector<u32>{});
for (int i{} ; i < n ; i++) for (int j{} ; j < m ; j++) {
if (s[i][j] == '#') continue;
if (sign(i, j) == 0) {
maxflow.addEdge(n * m, f(i, j), 1);
for (int d{} ; d < 4 ; d++) {
int ni{i + dx[d]}, nj{j + dy[d]};
if (!in(ni, nj)) continue;
if (s[ni][nj] == '#') continue;
edges[f(i, j)].push_back(maxflow.addEdge(f(i, j), f(ni, nj), 1));
}
}
else {
maxflow.addEdge(f(i, j), n * m + 1, 1);
}
}
int ans{maxflow.flow(n * m, n * m + 1)};
std::cout << ans << '\n';
std::vector<std::string> t(n, std::string(m, '.'));
auto g{[&](int v) -> std::pair<int, int> {
return std::pair{ v / m, v % m };
}};
for (int v{} ; v < n + m - 1 ; v++) {
for (int i{std::max(0, v - m + 1)} ; i < std::min(n, v + 1) ; i++) {
int j{v - i};
if (s[i][j] == '#') {
t[i][j] = '#';
continue;
}
if (sign(i, j) % 2 == 1) continue;
for (auto e : edges[f(i, j)]) {
if (maxflow.residual(e) == 1) continue;
auto [x, y]{ g(maxflow.to(e)) };
assert(in(x, y));
assert(s[x][y] == '.');
assert(t[x][y] == '.');
int dx{ x - i }, dy{ y - j };
if (dx == -1 and dy == 0) {
t[x][y] = 'v';
t[i][j] = '^';
}
else if (dx == 1 and dy == 0) {
t[x][y] = '^';
t[i][j] = 'v';
}
else if (dx == 0 and dy == -1) {
t[x][y] = '>';
t[i][j] = '<';
}
else if (dx == 0 and dy == 1) {
t[x][y] = '<';
t[i][j] = '>';
}
else {
assert(false);
}
}
}
}
for (const auto& v : t) std::cout << v << '\n';
}
int main() {
#ifdef ATCODER
solve();
#else
std::cout << "Hello World" << '\n';
#endif
}