結果

問題 No.1479 Matrix Eraser
ユーザー outlineoutline
提出日時 2021-04-18 15:29:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,575 bytes
コンパイル時間 2,094 ms
コンパイル使用メモリ 157,528 KB
実行使用メモリ 23,484 KB
最終ジャッジ日時 2023-09-17 07:46:26
合計ジャッジ時間 47,728 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 350 ms
4,868 KB
testcase_08 AC 585 ms
6,256 KB
testcase_09 AC 1,126 ms
9,332 KB
testcase_10 AC 2,524 ms
14,144 KB
testcase_11 AC 1,218 ms
10,216 KB
testcase_12 AC 131 ms
5,396 KB
testcase_13 AC 345 ms
5,924 KB
testcase_14 AC 150 ms
5,508 KB
testcase_15 AC 19 ms
4,376 KB
testcase_16 AC 280 ms
5,724 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 71 ms
8,216 KB
testcase_28 AC 71 ms
8,244 KB
testcase_29 AC 72 ms
8,028 KB
testcase_30 AC 72 ms
8,248 KB
testcase_31 AC 71 ms
8,296 KB
testcase_32 AC 30 ms
7,784 KB
testcase_33 AC 31 ms
7,692 KB
testcase_34 AC 30 ms
7,740 KB
testcase_35 AC 30 ms
7,724 KB
testcase_36 AC 30 ms
7,948 KB
testcase_37 AC 16 ms
4,376 KB
testcase_38 AC 62 ms
7,100 KB
testcase_39 TLE -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct HopcroftKarp {
    vector<vector<int>> graph;
    vector<int> dist, match;
    vector<bool> used, visited;
    
    HopcroftKarp(int n, int m) : graph(n), match(m, -1), used(n) {}

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

    void bfs(){
        dist.assign(graph.size(), -1);
        queue<int> que;
        for(int i = 0; i < (int)graph.size(); ++i){
            if(!used[i]){
                que.emplace(i);
                dist[i] = 0;
            }
        }
        while(!que.empty()){
            int from = que.front();
            que.pop();
            for(auto& to : graph[from]){
                int v = match[to];
                if(v >= 0 && dist[v] == -1){
                    dist[v] = dist[from] + 1;
                    que.emplace(v);
                }
            }
        }
    }

    bool dfs(int s){
        visited[s] = true;
        for(auto& u : graph[s]){
            int v = match[u];
            if(v < 0 || (!visited[v] && dist[v] == dist[s] + 1 && dfs(v))){
                match[u] = s;
                used[s] = true;
                return true;
            }
        }
        return false;
    }

    int bipartite_matching(){
        int ret = 0;
        while(true){
            bfs();
            visited.assign(graph.size(), false);
            int flow = 0;
            for(int i = 0; i < (int)graph.size(); ++i){
                if(!used[i] && dfs(i))  ++flow;
            }
            if(flow == 0)   return ret;
            ret += flow;
        }
    }

    void output(){
        for(int i = 0; i < (int)match.size(); ++i){
            if(~match[i]){
                cout << match[i] << '-' << i << '\n';
            }
        }
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int H, W;
    cin >> H >> W;
    vector A(H, vector<int>(W));
    vector<int> vals;
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            cin >> A[i][j];
            if(A[i][j]) vals.emplace_back(A[i][j]);
        }
    }
    sort(begin(vals), end(vals));
    vals.erase(unique(begin(vals), end(vals)), end(vals));
    int sz = vals.size();
    vector<vector<pair<int, int>>> ps(sz);
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            if(!A[i][j])    continue;
            int k = lower_bound(begin(vals), end(vals), A[i][j]) - begin(vals);
            ps[k].emplace_back(i, j);
        }
    }

    int ans = 0;
    for(int i = 0; i < sz; ++i){
        HopcroftKarp g(H, W);
        for(auto& [x, y] : ps[i])   g.add_edge(x, y);
        ans += g.bipartite_matching();
    }

    cout << ans << endl;
    
    return 0;
}
0