結果

問題 No.1479 Matrix Eraser
ユーザー tokusakuraitokusakurai
提出日時 2021-04-16 20:46:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,157 bytes
コンパイル時間 2,563 ms
コンパイル使用メモリ 216,496 KB
実行使用メモリ 23,768 KB
最終ジャッジ日時 2023-09-15 21:56:12
合計ジャッジ時間 37,191 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,892 KB
testcase_01 AC 8 ms
14,844 KB
testcase_02 AC 8 ms
14,576 KB
testcase_03 AC 7 ms
14,912 KB
testcase_04 AC 7 ms
14,756 KB
testcase_05 AC 7 ms
14,844 KB
testcase_06 AC 7 ms
14,904 KB
testcase_07 AC 274 ms
15,828 KB
testcase_08 AC 453 ms
16,428 KB
testcase_09 AC 850 ms
18,196 KB
testcase_10 AC 1,907 ms
20,964 KB
testcase_11 AC 930 ms
18,732 KB
testcase_12 AC 99 ms
15,980 KB
testcase_13 AC 269 ms
16,440 KB
testcase_14 AC 114 ms
16,292 KB
testcase_15 AC 20 ms
15,088 KB
testcase_16 AC 220 ms
16,216 KB
testcase_17 AC 2,417 ms
21,956 KB
testcase_18 AC 2,438 ms
21,952 KB
testcase_19 AC 2,422 ms
21,976 KB
testcase_20 AC 2,435 ms
21,900 KB
testcase_21 AC 2,425 ms
22,020 KB
testcase_22 AC 2,425 ms
21,900 KB
testcase_23 AC 2,421 ms
21,904 KB
testcase_24 AC 2,427 ms
22,028 KB
testcase_25 AC 2,442 ms
22,016 KB
testcase_26 AC 2,432 ms
22,016 KB
testcase_27 AC 46 ms
18,980 KB
testcase_28 AC 45 ms
18,776 KB
testcase_29 AC 45 ms
18,944 KB
testcase_30 AC 47 ms
18,908 KB
testcase_31 AC 48 ms
19,104 KB
testcase_32 AC 26 ms
18,664 KB
testcase_33 AC 28 ms
18,552 KB
testcase_34 AC 27 ms
18,840 KB
testcase_35 AC 28 ms
18,564 KB
testcase_36 AC 27 ms
18,792 KB
testcase_37 AC 25 ms
17,924 KB
testcase_38 AC 33 ms
17,868 KB
testcase_39 TLE -
testcase_40 AC 7 ms
14,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

struct Bipartite_Matching{
    vector<vector<int>> es;
    vector<int> d, match;
    vector<bool> used, used2;
    const int n, m;

    Bipartite_Matching(int n, int m) : es(n), d(n), match(m), used(n), used2(n), n(n), m(m) {}

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

    void _bfs(){
        fill(begin(d), end(d), -1);
        queue<int> que;
        for(int i = 0; i < n; i++){
            if(!used[i]) que.emplace(i), d[i] = 0;
        }
        while(!que.empty()){
            int i = que.front(); que.pop();
            for(auto &e: es[i]){
                int j = match[e];
                if(j != -1 && d[j] == -1){
                    que.emplace(j), d[j] = d[i]+1;
                }
            }
        }
    }

    bool _dfs(int now){
        used2[now] = true;
        for(auto &e: es[now]){
            int u = match[e];
            if(u == -1 || (!used2[u] && d[u] == d[now]+1 && _dfs(u))){
                match[e] = now, used[now] = true;
                return true;
            }
        }
        return false;
    }

    int bipartite_matching(){
        fill(begin(match), end(match), -1), fill(begin(used), end(used), false);
        int ret = 0;
        while(true){
            _bfs();
            fill(begin(used2), end(used2), false);
            int flow = 0;
            for(int i = 0; i < n; i++){
                if(!used[i] && _dfs(i)) flow++;
            }
            if(flow == 0) break;
            ret += flow;
        }
        return ret;
    }
};

int main(){
    int H, W; cin >> H >> W;

    vector<vector<int>> A(H, vector<int>(W));
    rep(i, H){
        rep(j, W){
            cin >> A[i][j];
        }
    }

    int MAX = 500000;

    vector<vector<pii>> ps(MAX+1);

    rep(i, H){
        rep(j, W){
            ps[A[i][j]].eb(i, j);
        }
    }

    vector<int> cx(H, 0), cy(W, 0);
    int sx = 0, sy = 0;

    ll ans = 0;
    Bipartite_Matching G(H, W); 

    rep3(i, MAX, 1){
        if(ps[i].empty()) continue;

        each(e, ps[i]){
            auto [x, y] = e;
            G.add_edge(x, y);
        }

        ans += G.bipartite_matching();

        rep(i, H) G.es[i].clear();
    }

    cout << ans << '\n';
}
0