結果

問題 No.1479 Matrix Eraser
ユーザー 👑 jupirojupiro
提出日時 2021-04-16 20:45:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,112 ms / 3,000 ms
コード長 1,992 bytes
コンパイル時間 1,308 ms
コンパイル使用メモリ 127,056 KB
実行使用メモリ 22,496 KB
最終ジャッジ日時 2023-09-15 21:50:34
合計ジャッジ時間 15,694 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,620 KB
testcase_01 AC 7 ms
14,612 KB
testcase_02 AC 8 ms
14,672 KB
testcase_03 AC 7 ms
14,620 KB
testcase_04 AC 7 ms
14,900 KB
testcase_05 AC 7 ms
14,880 KB
testcase_06 AC 7 ms
14,864 KB
testcase_07 AC 107 ms
15,696 KB
testcase_08 AC 174 ms
16,088 KB
testcase_09 AC 332 ms
17,636 KB
testcase_10 AC 717 ms
20,020 KB
testcase_11 AC 361 ms
18,312 KB
testcase_12 AC 50 ms
15,796 KB
testcase_13 AC 109 ms
16,060 KB
testcase_14 AC 55 ms
15,852 KB
testcase_15 AC 13 ms
15,116 KB
testcase_16 AC 91 ms
16,224 KB
testcase_17 AC 917 ms
20,980 KB
testcase_18 AC 915 ms
21,136 KB
testcase_19 AC 922 ms
20,984 KB
testcase_20 AC 911 ms
20,980 KB
testcase_21 AC 915 ms
20,808 KB
testcase_22 AC 913 ms
20,832 KB
testcase_23 AC 910 ms
20,808 KB
testcase_24 AC 908 ms
20,844 KB
testcase_25 AC 920 ms
21,184 KB
testcase_26 AC 915 ms
21,156 KB
testcase_27 AC 41 ms
17,852 KB
testcase_28 AC 40 ms
18,300 KB
testcase_29 AC 41 ms
18,004 KB
testcase_30 AC 40 ms
18,008 KB
testcase_31 AC 42 ms
18,020 KB
testcase_32 AC 25 ms
17,432 KB
testcase_33 AC 25 ms
17,504 KB
testcase_34 AC 25 ms
17,372 KB
testcase_35 AC 25 ms
17,396 KB
testcase_36 AC 25 ms
17,512 KB
testcase_37 AC 23 ms
16,748 KB
testcase_38 AC 43 ms
16,952 KB
testcase_39 AC 1,112 ms
22,496 KB
testcase_40 AC 8 ms
14,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0