結果
問題 | No.1479 Matrix Eraser |
ユーザー |
|
提出日時 | 2021-04-16 20:45:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 906 ms / 3,000 ms |
コード長 | 1,992 bytes |
コンパイル時間 | 1,345 ms |
コンパイル使用メモリ | 123,220 KB |
最終ジャッジ日時 | 2025-01-20 19:02:44 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
#include <iostream>#include <string>#include <algorithm>#include <vector>#include <queue>#include <utility>#include <tuple>#include <cmath>#include <numeric>#include <set>#include <map>#include <array>#include <complex>#include <iomanip>#include <cassert>#include <random>using ll = long long;using std::cin;using std::cout;using std::endl;template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }const int inf = (int)1e9 + 7;const long long INF = 1LL << 60;struct bipartite_matching {std::vector<std::vector<int>> g;std::vector<int> r, c, vis;bipartite_matching(int h = 0, int w = 0) : g(h), r(h, -1), c(w, -1), vis(h) {}void add(int i, int j) { g[i].push_back(j); }bool dfs(int i) {if (std::exchange(vis[i], true)) return false;for (int j : g[i]) if (c[j] == -1) return r[i] = j, c[j] = i, true;for (int j : g[i]) if (dfs(c[j])) return r[i] = j, c[j] = i, true;return false;}int run() {while (true) {fill(begin(vis), end(vis), false);bool updated = false;for (int i = 0; i < (int)r.size(); ++i) if (r[i] == -1) updated |= dfs(i);if (not updated) break;}return r.size() - count(begin(r), end(r), -1);}};void solve(){int H, W; cin >> H >> W;const int N = 500000;std::vector<std::vector<std::pair<int, int>>> vp(N + 1);for (int i = 0; i < H; ++i){for (int j = 0; j < W; ++j){int x; cin >> x;vp[x].emplace_back(i, j);}}int res = 0;for (int i = 1; i <= N; ++i){if(vp[i].empty())continue;bipartite_matching g(H, W);for(const auto &[h, w] : vp[i]){g.add(h, w);}res += g.run();}cout << res << "\n";}int main(){std::cin.tie(nullptr);std::ios::sync_with_stdio(false);int kkt = 1;//cin >> kkt;while(kkt--)solve();return 0;}