結果

問題 No.1479 Matrix Eraser
ユーザー tokusakuraitokusakurai
提出日時 2021-09-25 10:18:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 663 ms / 3,000 ms
コード長 3,196 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 215,860 KB
実行使用メモリ 22,464 KB
最終ジャッジ日時 2023-09-18 22:51:31
合計ジャッジ時間 14,592 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
14,612 KB
testcase_01 AC 7 ms
14,820 KB
testcase_02 AC 7 ms
14,732 KB
testcase_03 AC 7 ms
14,812 KB
testcase_04 AC 7 ms
14,736 KB
testcase_05 AC 7 ms
14,704 KB
testcase_06 AC 7 ms
14,728 KB
testcase_07 AC 20 ms
15,604 KB
testcase_08 AC 39 ms
16,440 KB
testcase_09 AC 117 ms
17,712 KB
testcase_10 AC 399 ms
20,028 KB
testcase_11 AC 143 ms
18,304 KB
testcase_12 AC 18 ms
15,812 KB
testcase_13 AC 32 ms
16,116 KB
testcase_14 AC 20 ms
15,860 KB
testcase_15 AC 10 ms
14,832 KB
testcase_16 AC 27 ms
16,120 KB
testcase_17 AC 590 ms
21,192 KB
testcase_18 AC 586 ms
21,076 KB
testcase_19 AC 581 ms
21,088 KB
testcase_20 AC 580 ms
20,860 KB
testcase_21 AC 583 ms
20,948 KB
testcase_22 AC 592 ms
21,072 KB
testcase_23 AC 606 ms
20,864 KB
testcase_24 AC 663 ms
21,064 KB
testcase_25 AC 604 ms
21,180 KB
testcase_26 AC 598 ms
20,964 KB
testcase_27 AC 48 ms
18,024 KB
testcase_28 AC 46 ms
18,040 KB
testcase_29 AC 46 ms
18,092 KB
testcase_30 AC 46 ms
18,204 KB
testcase_31 AC 47 ms
18,036 KB
testcase_32 AC 27 ms
17,668 KB
testcase_33 AC 26 ms
17,604 KB
testcase_34 AC 26 ms
17,584 KB
testcase_35 AC 27 ms
17,524 KB
testcase_36 AC 27 ms
17,848 KB
testcase_37 AC 25 ms
16,840 KB
testcase_38 AC 34 ms
16,716 KB
testcase_39 AC 59 ms
22,464 KB
testcase_40 AC 8 ms
14,932 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;

    int MAX = 500000;
    vector<vector<pii>> ps(MAX + 1);

    rep(i, H) {
        rep(j, W) {
            int A;
            cin >> A;
            ps[A].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;

        if (sz(ps[i]) == 1) {
            ans++;
            continue;
        }

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

        ans += G.bipartite_matching();

        each(e, ps[i]) G.es[e.first].clear();
    }

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