結果
問題 | No.1479 Matrix Eraser |
ユーザー |
![]() |
提出日時 | 2021-04-16 20:11:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 249 ms / 3,000 ms |
コード長 | 2,564 bytes |
コンパイル時間 | 2,553 ms |
コンパイル使用メモリ | 212,312 KB |
最終ジャッジ日時 | 2025-01-20 18:27:11 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
#include <bits/stdc++.h>using namespace std;#ifdef tabr#include "library/debug.cpp"#else#define debug(...)#endifstruct matching {vector<vector<int>> g;vector<int> pa;vector<int> pb;vector<int> was;int n, m;int res;int iter;matching(int _n, int _m) : n(_n), m(_m) {pa.assign(n, -1);pb.assign(m, -1);was.resize(n);g.resize(n);res = 0;iter = 0;}void add(int from, int to) {g[from].push_back(to);}bool dfs(int v) {was[v] = iter;for (int u : g[v]) {if (pb[u] == -1) {pa[v] = u;pb[u] = v;return true;}}for (int u : g[v]) {if (was[pb[u]] != iter && dfs(pb[u])) {pa[v] = u;pb[u] = v;return true;}}return false;}int solve() {while (true) {iter++;int add = 0;for (int i = 0; i < n; i++) {if (pa[i] == -1 && dfs(i)) {add++;}}if (add == 0) {break;}res += add;}return res;}};int main() {ios::sync_with_stdio(false);cin.tie(0);int h, w;cin >> h >> w;vector<vector<int>> a(h, vector<int>(w));int m = 500001;vector<vector<pair<int, int>>> b(m);for (int i = 0; i < h; i++) {for (int j = 0; j < w; j++) {cin >> a[i][j];b[a[i][j]].emplace_back(i, j);}}int ans = 0;for (int i = m - 1; i > 0; i--) {if (b[i].empty()) {continue;}vector<int> s, t;for (auto [x, y] : b[i]) {s.emplace_back(x);t.emplace_back(y);}sort(s.begin(), s.end());s.resize(unique(s.begin(), s.end()) - s.begin());sort(t.begin(), t.end());t.resize(unique(t.begin(), t.end()) - t.begin());matching mt((int)s.size(), (int)t.size());for (auto [x, y] : b[i]) {int from = (int)(lower_bound(s.begin(), s.end(), x) - s.begin());int to = (int)(lower_bound(t.begin(), t.end(), y) - t.begin());mt.add(from, to);}ans += mt.solve();}cout << ans << '\n';return 0;}