結果

問題 No.1479 Matrix Eraser
ユーザー jutamajutama
提出日時 2021-04-16 23:29:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 632 ms / 3,000 ms
コード長 1,692 bytes
コンパイル時間 4,214 ms
コンパイル使用メモリ 196,168 KB
最終ジャッジ日時 2025-01-20 20:47:47
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bitset>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace atcoder;

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

void input() {
    
    
}


void solve() {
    
    
    int H, W; cin >> H >> W;
    unordered_map<int, unordered_set<int>> A;
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            int a; cin >> a;
            A[a].insert(i * W + j);
        }
    }
    
    int ans = 0;
    for(auto [a, as] : A) {
        if(a == 0) continue;
        
        int acc = 0, arc = 0;
        unordered_map<int, int> ac, ar;
        for(auto b : as) {
            if(ac.count(b/W) == 0) ac[b/W] = ++acc;
            if(ar.count(b%W) == 0) ar[b%W] = ++arc;
        }
        
        int Gs = acc + arc;
        atcoder::mf_graph<int> G(Gs + 2);
        for(int i = 0; i < acc; i++) {
            G.add_edge(Gs, i, 1);
        }
        for(int j = 0; j < arc; j++) {
            G.add_edge(acc + j, Gs+1, 1);
        }
        for(auto b : as) {
            G.add_edge(ac[b/W]-1, acc + ar[b%W]-1, 1);
        }
        ans += G.flow(Gs, Gs+1);
    }
    
    cout << ans << endl;
    
    
    
    
}

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

int main() {
    
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
    std::cin.tie(0);
    ios::sync_with_stdio(false);
    
    input();
    solve();
    
    return 0;
}
0