結果

問題 No.1479 Matrix Eraser
ユーザー KudeKude
提出日時 2021-04-16 21:36:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,655 ms / 3,000 ms
コード長 2,014 bytes
コンパイル時間 6,609 ms
コンパイル使用メモリ 265,996 KB
実行使用メモリ 22,452 KB
最終ジャッジ日時 2023-09-15 23:49:01
合計ジャッジ時間 26,458 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,800 KB
testcase_01 AC 7 ms
14,796 KB
testcase_02 AC 7 ms
14,788 KB
testcase_03 AC 6 ms
14,820 KB
testcase_04 AC 6 ms
14,884 KB
testcase_05 AC 5 ms
14,892 KB
testcase_06 AC 6 ms
14,624 KB
testcase_07 AC 103 ms
15,584 KB
testcase_08 AC 185 ms
16,104 KB
testcase_09 AC 456 ms
17,720 KB
testcase_10 AC 1,055 ms
20,136 KB
testcase_11 AC 571 ms
18,224 KB
testcase_12 AC 143 ms
15,956 KB
testcase_13 AC 169 ms
16,416 KB
testcase_14 AC 146 ms
15,916 KB
testcase_15 AC 24 ms
15,216 KB
testcase_16 AC 140 ms
16,212 KB
testcase_17 AC 1,358 ms
21,036 KB
testcase_18 AC 1,359 ms
21,028 KB
testcase_19 AC 1,358 ms
20,928 KB
testcase_20 AC 1,354 ms
21,120 KB
testcase_21 AC 1,354 ms
21,116 KB
testcase_22 AC 1,349 ms
21,200 KB
testcase_23 AC 1,345 ms
21,156 KB
testcase_24 AC 1,362 ms
21,140 KB
testcase_25 AC 1,371 ms
20,960 KB
testcase_26 AC 1,348 ms
21,136 KB
testcase_27 AC 115 ms
17,988 KB
testcase_28 AC 115 ms
18,144 KB
testcase_29 AC 117 ms
18,036 KB
testcase_30 AC 115 ms
18,096 KB
testcase_31 AC 116 ms
18,060 KB
testcase_32 AC 80 ms
18,388 KB
testcase_33 AC 80 ms
18,228 KB
testcase_34 AC 79 ms
18,280 KB
testcase_35 AC 79 ms
18,184 KB
testcase_36 AC 79 ms
18,256 KB
testcase_37 AC 19 ms
14,696 KB
testcase_38 AC 56 ms
17,032 KB
testcase_39 AC 1,655 ms
22,452 KB
testcase_40 AC 7 ms
14,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;


// https://ei1333.github.io/algorithm/bipartite-matching.html
struct Bipartite_Matching
{
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;
 
  Bipartite_Matching(int n)
  {
    timestamp = 0;
    graph.resize(n);
    alive.assign(n, 1);
    used.assign(n, 0);
    match.assign(n, -1);
  }
 
  void add_edge(int u, int v)
  {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
 
  bool dfs(int v)
  {
    used[v] = timestamp;
    for(int i = 0; i < graph[v].size(); i++) {
      int u = graph[v][i], w = match[u];
      if(alive[u] == 0) continue;
      if(w == -1 || (used[w] != timestamp && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return (true);
      }
    }
    return (false);
  }
 
  int bipartite_matching()
  {
    int ret = 0;
    for(int i = 0; i < graph.size(); i++) {
      if(alive[i] == 0) continue;
      if(match[i] == -1) {
        ++timestamp;
        ret += dfs(i);
      }
    }
    return (ret);
  } 
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<vector<P>> d(500001);
    rep(i, h) rep(j, w) {
        int a;
        cin >> a;
        if (a) d[a].emplace_back(i, j);
    }
    int ans = 0;
    for(auto& ps: d) if (!ps.empty()) {
        Bipartite_Matching bm(h + w);
        for(auto [i, j]: ps) {
            bm.add_edge(i, h + j);
        }
        ans += bm.bipartite_matching();
    }
    cout << ans << '\n';
}
0