結果

問題 No.1479 Matrix Eraser
ユーザー chocoruskchocorusk
提出日時 2021-04-16 22:25:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 2,305 bytes
コンパイル時間 3,505 ms
コンパイル使用メモリ 200,252 KB
実行使用メモリ 22,976 KB
最終ジャッジ日時 2023-09-16 01:17:33
合計ジャッジ時間 10,000 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
15,192 KB
testcase_01 AC 8 ms
15,124 KB
testcase_02 AC 7 ms
15,004 KB
testcase_03 AC 8 ms
15,256 KB
testcase_04 AC 7 ms
15,088 KB
testcase_05 AC 7 ms
15,136 KB
testcase_06 AC 7 ms
15,000 KB
testcase_07 AC 27 ms
16,092 KB
testcase_08 AC 42 ms
16,540 KB
testcase_09 AC 85 ms
18,360 KB
testcase_10 AC 159 ms
20,372 KB
testcase_11 AC 100 ms
18,800 KB
testcase_12 AC 34 ms
16,216 KB
testcase_13 AC 44 ms
16,656 KB
testcase_14 AC 36 ms
16,272 KB
testcase_15 AC 13 ms
15,252 KB
testcase_16 AC 37 ms
16,316 KB
testcase_17 AC 193 ms
21,396 KB
testcase_18 AC 193 ms
21,348 KB
testcase_19 AC 191 ms
21,456 KB
testcase_20 AC 199 ms
21,468 KB
testcase_21 AC 198 ms
21,456 KB
testcase_22 AC 195 ms
21,256 KB
testcase_23 AC 197 ms
21,468 KB
testcase_24 AC 194 ms
21,388 KB
testcase_25 AC 195 ms
21,300 KB
testcase_26 AC 194 ms
21,400 KB
testcase_27 AC 207 ms
18,156 KB
testcase_28 AC 209 ms
18,068 KB
testcase_29 AC 207 ms
17,868 KB
testcase_30 AC 210 ms
17,848 KB
testcase_31 AC 208 ms
18,124 KB
testcase_32 AC 137 ms
18,324 KB
testcase_33 AC 136 ms
18,276 KB
testcase_34 AC 137 ms
18,328 KB
testcase_35 AC 137 ms
18,216 KB
testcase_36 AC 136 ms
18,360 KB
testcase_37 AC 44 ms
16,972 KB
testcase_38 AC 179 ms
17,260 KB
testcase_39 AC 174 ms
22,976 KB
testcase_40 AC 7 ms
15,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct BipartiteMatching {
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;

  BipartiteMatching(int n) : graph(n), alive(n, 1), used(n, 0), match(n, -1), timestamp(0) {}

  void add_edge(int u, int v) {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int idx) {
    used[idx] = timestamp;
    for(auto &to : graph[idx]) {
      int to_match = match[to];
      if(alive[to] == 0) continue;
      if(to_match == -1 || (used[to_match] != timestamp && dfs(to_match))) {
        match[idx] = to;
        match[to] = idx;
        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;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      if(i < match[i]) {
        cout << i << "-" << match[i] << endl;
      }
    }
  }
};

vector<P> v[500050];
int main()
{
    int h, w; cin>>h>>w;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            int a; cin>>a;
            v[a].push_back({i, j});
        }
    }
    int ans=0;
    for(int i=1; i<=500000; i++){
        if(v[i].empty()) continue;
        map<int, int> mp;
        for(auto p:v[i]){
            mp[p.first]=0;
            mp[p.second+h]=0;
        }
        int c=0;
        for(auto &p:mp){
            p.second=c++;
        }
      	BipartiteMatching g(mp.size());
        for(auto p:v[i]){
            g.add_edge(mp[p.first], mp[p.second+h]);
        }
        ans+=g.bipartite_matching();
    }
    cout<<ans<<endl;
    return 0;
}
0