結果

問題 No.1479 Matrix Eraser
ユーザー tabrtabr
提出日時 2021-04-16 20:11:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 3,000 ms
コード長 2,564 bytes
コンパイル時間 3,041 ms
コンパイル使用メモリ 217,664 KB
実行使用メモリ 23,628 KB
最終ジャッジ日時 2023-09-15 20:24:26
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,612 KB
testcase_01 AC 7 ms
14,712 KB
testcase_02 AC 7 ms
14,812 KB
testcase_03 AC 8 ms
15,004 KB
testcase_04 AC 8 ms
14,856 KB
testcase_05 AC 8 ms
14,712 KB
testcase_06 AC 10 ms
14,596 KB
testcase_07 AC 23 ms
15,860 KB
testcase_08 AC 34 ms
16,504 KB
testcase_09 AC 65 ms
18,284 KB
testcase_10 AC 118 ms
20,936 KB
testcase_11 AC 78 ms
18,836 KB
testcase_12 AC 26 ms
16,164 KB
testcase_13 AC 32 ms
16,396 KB
testcase_14 AC 27 ms
16,112 KB
testcase_15 AC 12 ms
15,108 KB
testcase_16 AC 31 ms
16,132 KB
testcase_17 AC 145 ms
21,972 KB
testcase_18 AC 148 ms
21,992 KB
testcase_19 AC 145 ms
22,032 KB
testcase_20 AC 142 ms
21,936 KB
testcase_21 AC 149 ms
21,992 KB
testcase_22 AC 146 ms
22,000 KB
testcase_23 AC 145 ms
22,004 KB
testcase_24 AC 152 ms
22,104 KB
testcase_25 AC 145 ms
22,000 KB
testcase_26 AC 143 ms
21,980 KB
testcase_27 AC 88 ms
19,056 KB
testcase_28 AC 89 ms
18,880 KB
testcase_29 AC 89 ms
18,960 KB
testcase_30 AC 90 ms
18,940 KB
testcase_31 AC 90 ms
19,204 KB
testcase_32 AC 60 ms
20,176 KB
testcase_33 AC 60 ms
19,656 KB
testcase_34 AC 59 ms
19,524 KB
testcase_35 AC 59 ms
19,520 KB
testcase_36 AC 59 ms
19,592 KB
testcase_37 AC 24 ms
17,936 KB
testcase_38 AC 87 ms
17,764 KB
testcase_39 AC 134 ms
23,628 KB
testcase_40 AC 8 ms
14,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

struct matching {
    vector<vector<int>> g;
    vector<int> pa;
    vector<int> pb;
    vector<int> was;
    int n, m;
    int res;
    int iter;

    matching(int _n, int _m) : n(_n), m(_m) {
        pa.assign(n, -1);
        pb.assign(m, -1);
        was.resize(n);
        g.resize(n);
        res = 0;
        iter = 0;
    }

    void add(int from, int to) {
        g[from].push_back(to);
    }

    bool dfs(int v) {
        was[v] = iter;
        for (int u : g[v]) {
            if (pb[u] == -1) {
                pa[v] = u;
                pb[u] = v;
                return true;
            }
        }
        for (int u : g[v]) {
            if (was[pb[u]] != iter && dfs(pb[u])) {
                pa[v] = u;
                pb[u] = v;
                return true;
            }
        }
        return false;
    }

    int solve() {
        while (true) {
            iter++;
            int add = 0;
            for (int i = 0; i < n; i++) {
                if (pa[i] == -1 && dfs(i)) {
                    add++;
                }
            }
            if (add == 0) {
                break;
            }
            res += add;
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    int m = 500001;
    vector<vector<pair<int, int>>> b(m);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
            b[a[i][j]].emplace_back(i, j);
        }
    }
    int ans = 0;
    for (int i = m - 1; i > 0; i--) {
        if (b[i].empty()) {
            continue;
        }
        vector<int> s, t;
        for (auto [x, y] : b[i]) {
            s.emplace_back(x);
            t.emplace_back(y);
        }
        sort(s.begin(), s.end());
        s.resize(unique(s.begin(), s.end()) - s.begin());
        sort(t.begin(), t.end());
        t.resize(unique(t.begin(), t.end()) - t.begin());
        matching mt((int)s.size(), (int)t.size());
        for (auto [x, y] : b[i]) {
            int from = (int)(lower_bound(s.begin(), s.end(), x) - s.begin());
            int to = (int)(lower_bound(t.begin(), t.end(), y) - t.begin());
            mt.add(from, to);
        }
        ans += mt.solve();
    }
    cout << ans << '\n';
    return 0;
}
0