結果
| 問題 |
No.1479 Matrix Eraser
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-17 04:17:07 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 471 ms / 3,000 ms |
| コード長 | 12,693 bytes |
| コンパイル時間 | 6,212 ms |
| コンパイル使用メモリ | 169,856 KB |
| 実行使用メモリ | 30,720 KB |
| 最終ジャッジ日時 | 2024-07-03 10:04:20 |
| 合計ジャッジ時間 | 15,735 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
ソースコード
#include <bits/stdc++.h>
#ifndef ATCODER_MAXFLOW_HPP
#define ATCODER_MAXFLOW_HPP 1
#ifndef ATCODER_INTERNAL_QUEUE_HPP
#define ATCODER_INTERNAL_QUEUE_HPP 1
namespace atcoder {
namespace internal {
template <class T> struct simple_queue {
std::vector<T> payload;
int pos = 0;
void reserve(int n) { payload.reserve(n); }
int size() const { return int(payload.size()) - pos; }
bool empty() const { return pos == int(payload.size()); }
void push(const T& t) { payload.push_back(t); }
T& front() { return payload[pos]; }
void clear() {
payload.clear();
pos = 0;
}
void pop() { pos++; }
};
} // namespace internal
} // namespace atcoder
#endif // ATCODER_INTERNAL_QUEUE_HPP
namespace atcoder {
template <class Cap> struct mf_graph {
public:
mf_graph() : _n(0) {}
mf_graph(int n) : _n(n), g(n) {}
int add_edge(int from, int to, Cap cap) {
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
assert(0 <= cap);
int m = int(pos.size());
pos.push_back({from, int(g[from].size())});
int from_id = int(g[from].size());
int to_id = int(g[to].size());
if (from == to) to_id++;
g[from].push_back(_edge{to, to_id, cap});
g[to].push_back(_edge{from, from_id, 0});
return m;
}
struct edge {
int from, to;
Cap cap, flow;
};
edge get_edge(int i) {
int m = int(pos.size());
assert(0 <= i && i < m);
auto _e = g[pos[i].first][pos[i].second];
auto _re = g[_e.to][_e.rev];
return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
}
std::vector<edge> edges() {
int m = int(pos.size());
std::vector<edge> result;
for (int i = 0; i < m; i++) {
result.push_back(get_edge(i));
}
return result;
}
void change_edge(int i, Cap new_cap, Cap new_flow) {
int m = int(pos.size());
assert(0 <= i && i < m);
assert(0 <= new_flow && new_flow <= new_cap);
auto& _e = g[pos[i].first][pos[i].second];
auto& _re = g[_e.to][_e.rev];
_e.cap = new_cap - new_flow;
_re.cap = new_flow;
}
Cap flow(int s, int t) {
return flow(s, t, std::numeric_limits<Cap>::max());
}
Cap flow(int s, int t, Cap flow_limit) {
assert(0 <= s && s < _n);
assert(0 <= t && t < _n);
assert(s != t);
std::vector<int> level(_n), iter(_n);
internal::simple_queue<int> que;
auto bfs = [&]() {
std::fill(level.begin(), level.end(), -1);
level[s] = 0;
que.clear();
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (auto e : g[v]) {
if (e.cap == 0 || level[e.to] >= 0) continue;
level[e.to] = level[v] + 1;
if (e.to == t) return;
que.push(e.to);
}
}
};
auto dfs = [&](auto self, int v, Cap up) {
if (v == s) return up;
Cap res = 0;
int level_v = level[v];
for (int& i = iter[v]; i < int(g[v].size()); i++) {
_edge& e = g[v][i];
if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
Cap d =
self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
if (d <= 0) continue;
g[v][i].cap += d;
g[e.to][e.rev].cap -= d;
res += d;
if (res == up) break;
}
return res;
};
Cap flow = 0;
while (flow < flow_limit) {
bfs();
if (level[t] == -1) break;
std::fill(iter.begin(), iter.end(), 0);
while (flow < flow_limit) {
Cap f = dfs(dfs, t, flow_limit - flow);
if (!f) break;
flow += f;
}
}
return flow;
}
std::vector<bool> min_cut(int s) {
std::vector<bool> visited(_n);
internal::simple_queue<int> que;
que.push(s);
while (!que.empty()) {
int p = que.front();
que.pop();
visited[p] = true;
for (auto e : g[p]) {
if (e.cap && !visited[e.to]) {
visited[e.to] = true;
que.push(e.to);
}
}
}
return visited;
}
private:
int _n;
struct _edge {
int to, rev;
Cap cap;
};
std::vector<std::pair<int, int>> pos;
std::vector<std::vector<_edge>> g;
};
} // namespace atcoder
#endif // ATCODER_MAXFLOW_HPP
template <typename T>
std::vector<T> Compress(std::vector<T> v) {
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
return v;
}
template <typename T>
T Uncompress(const std::vector<T>& v, T x) {
return std::lower_bound(v.begin(), v.end(), x) - v.begin();
}
template <typename T>
std::function<T(T)> Uncompressor(const std::vector<T>& v) {
return [&v](T i) -> T { return Uncompress(v, i); };
}
#include <boost/hana/functional/fix.hpp>
template <typename T, typename = void>
struct is_dereferenceable : std::false_type {};
template <typename T>
struct is_dereferenceable<T, std::void_t<decltype(*std::declval<T>())>>
: std::true_type {};
template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
decltype(std::end(std::declval<T>()))>>
: std::true_type {};
template <typename T, typename = void>
struct is_applicable : std::false_type {};
template <typename T>
struct is_applicable<T, std::void_t<decltype(std::tuple_size<T>::value)>>
: std::true_type {};
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args);
template <typename T>
void debug(const T& v) {
if constexpr (is_dereferenceable<T>::value) {
std::cerr << "{";
if (v) {
debug(*v);
} else {
std::cerr << "nil";
}
std::cerr << "}";
} else if constexpr (is_iterable<T>::value &&
!std::is_same<T, std::string>::value) {
std::cerr << "{";
for (auto it = std::begin(v); it != std::end(v); ++it) {
if (it != std::begin(v)) std::cerr << ", ";
debug(*it);
}
std::cerr << "}";
} else if constexpr (is_applicable<T>::value) {
std::cerr << "{";
std::apply([](const auto&... args) { debug(args...); }, v);
std::cerr << "}";
} else {
std::cerr << v;
}
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
debug(value);
std::cerr << ", ";
debug(args...);
}
#if DEBUG
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")\n"; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
std::cin >> value;
read_from_cin(args...);
}
#define rd(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
#define ints(...) rd(int, __VA_ARGS__);
#define strings(...) rd(string, __VA_ARGS__);
// Strings used for yes/no questions. Defined as variables so that it can be
// adjusted for each contest site.
const char *yes_str = "Yes", *no_str = "No";
template <typename T>
void write_to_cout(const T& value) {
if constexpr (std::is_same<T, bool>::value) {
std::cout << (value ? yes_str : no_str);
} else if constexpr (is_iterable<T>::value &&
!std::is_same<T, std::string>::value) {
for (auto it = std::begin(value); it != std::end(value); ++it) {
if (it != std::begin(value)) std::cout << " ";
std::cout << *it;
}
} else {
std::cout << value;
}
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
write_to_cout(value);
std::cout << ' ';
write_to_cout(args...);
}
#define wt(...) \
do { \
write_to_cout(__VA_ARGS__); \
cout << '\n'; \
} while (0)
#define all(x) (x).begin(), (x).end()
#define eb(...) emplace_back(__VA_ARGS__)
#define pb(...) push_back(__VA_ARGS__)
#define dispatch(_1, _2, _3, name, ...) name
#define as_i64(x) \
( \
[] { \
static_assert( \
std::is_integral< \
typename std::remove_reference<decltype(x)>::type>::value, \
"rep macro supports std integral types only"); \
}, \
static_cast<int64_t>(x))
#define rep3(i, a, b) for (int64_t i = as_i64(a); i < as_i64(b); ++i)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(_loop_variable_, n)
#define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep3(i, a, b) for (int64_t i = as_i64(b) - 1; i >= as_i64(a); --i)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep1(n) rrep2(_loop_variable_, n)
#define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define each3(k, v, c) for (auto&& [k, v] : c)
#define each2(e, c) for (auto&& e : c)
#define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__)
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
for (T& vi : v) is >> vi;
return is;
}
template <typename T, typename U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
bool chmax(T& a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
bool chmin(T& a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
auto max(T a, U b) {
return a > b ? a : b;
}
template <typename T, typename U>
auto min(T a, U b) {
return a < b ? a : b;
}
template <typename T>
int64_t sz(const T& v) {
return std::size(v);
}
template <typename T>
int64_t popcount(T i) {
return std::bitset<std::numeric_limits<T>::digits>(i).count();
}
template <typename T>
bool hasbit(T s, int i) {
return std::bitset<std::numeric_limits<T>::digits>(s)[i];
}
template <typename T, typename U>
auto div_floor(T n, U d) {
if (d < 0) {
n = -n;
d = -d;
}
if (n < 0) {
return -((-n + d - 1) / d);
}
return n / d;
};
template <typename T, typename U>
auto div_ceil(T n, U d) {
if (d < 0) {
n = -n;
d = -d;
}
if (n < 0) {
return -(-n / d);
}
return (n + d - 1) / d;
}
template <typename T>
bool even(T x) {
return x % 2 == 0;
}
std::array<std::pair<int64_t, int64_t>, 4> adjacent(int64_t i, int64_t j) {
return {{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}}};
}
bool inside(int64_t i, int64_t j, int64_t I, int64_t J) {
return 0 <= i && i < I && 0 <= j && j < J;
}
const int64_t big = std::numeric_limits<int64_t>::max() / 4;
using i64 = int64_t;
using i32 = int32_t;
template <typename T>
using low_priority_queue =
std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;
void Main();
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout << std::fixed << std::setprecision(20);
Main();
return 0;
}
const auto& Fix = boost::hana::fix;
using namespace std;
#define int i64
void Main() {
ints(h, w);
map<int, V<pair<int, int>>> m;
rep(i, h) rep(j, w) {
ints(a);
if (a) m[a].eb(i, j);
}
int ans = 0;
each(_, e, m) {
V<int> rs, cs;
each(r, c, e) {
rs.pb(r);
cs.pb(c);
}
V<int> crs = Compress(rs), ccs = Compress(cs);
atcoder::mf_graph<int> g(sz(crs) + sz(ccs) + 2);
int s = sz(crs) + sz(ccs), t = s + 1;
rep(i, sz(crs)) g.add_edge(s, i, 1);
rep(i, sz(ccs)) g.add_edge(sz(crs) + i, t, 1);
each(r, c, e) {
int ri = Uncompress(crs, r);
int ci = Uncompress(ccs, c);
g.add_edge(ri, sz(crs) + ci, 1);
}
ans += g.flow(s, t);
}
wt(ans);
}