結果

問題 No.1479 Matrix Eraser
ユーザー Today03Today03
提出日時 2024-05-24 09:32:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 414 ms / 3,000 ms
コード長 3,681 bytes
コンパイル時間 3,269 ms
コンパイル使用メモリ 245,436 KB
実行使用メモリ 23,972 KB
最終ジャッジ日時 2024-05-24 09:33:11
合計ジャッジ時間 14,177 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
14,996 KB
testcase_01 AC 7 ms
14,940 KB
testcase_02 AC 7 ms
14,972 KB
testcase_03 AC 8 ms
14,948 KB
testcase_04 AC 8 ms
14,980 KB
testcase_05 AC 7 ms
14,944 KB
testcase_06 AC 8 ms
14,936 KB
testcase_07 AC 53 ms
15,900 KB
testcase_08 AC 92 ms
16,496 KB
testcase_09 AC 173 ms
18,496 KB
testcase_10 AC 325 ms
21,076 KB
testcase_11 AC 206 ms
18,936 KB
testcase_12 AC 66 ms
16,296 KB
testcase_13 AC 86 ms
16,720 KB
testcase_14 AC 69 ms
16,316 KB
testcase_15 AC 21 ms
15,200 KB
testcase_16 AC 78 ms
16,428 KB
testcase_17 AC 395 ms
22,148 KB
testcase_18 AC 387 ms
22,180 KB
testcase_19 AC 394 ms
22,176 KB
testcase_20 AC 395 ms
22,188 KB
testcase_21 AC 388 ms
22,388 KB
testcase_22 AC 384 ms
22,176 KB
testcase_23 AC 401 ms
22,140 KB
testcase_24 AC 392 ms
22,168 KB
testcase_25 AC 390 ms
22,280 KB
testcase_26 AC 397 ms
22,268 KB
testcase_27 AC 280 ms
18,904 KB
testcase_28 AC 279 ms
19,056 KB
testcase_29 AC 284 ms
19,144 KB
testcase_30 AC 283 ms
19,144 KB
testcase_31 AC 280 ms
19,068 KB
testcase_32 AC 131 ms
23,972 KB
testcase_33 AC 129 ms
23,972 KB
testcase_34 AC 131 ms
23,844 KB
testcase_35 AC 130 ms
23,968 KB
testcase_36 AC 132 ms
23,968 KB
testcase_37 AC 50 ms
18,168 KB
testcase_38 AC 414 ms
18,108 KB
testcase_39 AC 380 ms
23,708 KB
testcase_40 AC 8 ms
14,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

template <typename Cap>
struct max_flow {
    max_flow(int n) {
        this->n = n;
        G = vector<vector<tuple<int, int, Cap>>>(n);
    }
    void add_edge(int u, int v, Cap c) {
        G[u].push_back(make_tuple(v, G[v].size(), c));
        G[v].push_back(make_tuple(u, (int)G[u].size() - 1, 0));
    }
    Cap get_max_flow(int start, int goal) {
        Cap ret = 0;
        while (true) {
            vector<int> dst = calculate_distance(start);
            if (dst[goal] < 0) {
                return ret;
            }
            vector<int> removed(n, 0);
            while (true) {
                Cap add = flowing(start, goal, numeric_limits<Cap>::max(), removed, dst);
                if (add == 0) {
                    break;
                }
                ret += add;
            }
        }
        return ret;
    }

private:
    int n;
    vector<vector<tuple<int, int, Cap>>> G;
    vector<int> calculate_distance(int start) {
        vector<int> dst(n, -1);
        dst[start] = 0;
        queue<int> que;
        que.push(start);
        while (!que.empty()) {
            int now = que.front();
            que.pop();
            for (auto [nxt, _, cap] : G[now]) {
                if (cap > 0 && dst[nxt] == -1) {
                    dst[nxt] = dst[now] + 1;
                    que.push(nxt);
                }
            }
        }
        return dst;
    }
    Cap flowing(int now, int goal, Cap limit, vector<int> &removed, vector<int> &dst) {
        if (now == goal) {
            return limit;
        }
        while (removed[now] < (int)G[now].size()) {
            auto [nxt, rev, cap] = G[now][removed[now]];
            if (cap > 0 && dst[now] < dst[nxt]) {
                Cap flow = flowing(nxt, goal, min(limit, cap), removed, dst);
                if (flow > 0) {
                    get<2>(G[now][removed[now]]) -= flow;
                    get<2>(G[nxt][rev]) += flow;
                    return flow;
                }
            }
            removed[now]++;
        }
        return 0;
    }
};

const int MX = 5e5 + 5;

int main() {
    int H, W;
    cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
        }
    }

    vector<vector<pair<int, int>>> P(MX);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            P[A[i][j]].push_back({i, j});
        }
    }

    int ans = 0;
    for (int i = 1; i < MX; i++) {
        if (!P[i].empty()) {
            map<int, int> mp;
            {
                vector<int> idx;
                for (auto [x, y] : P[i]) {
                    idx.push_back(x);
                    idx.push_back(H + y);
                }
                idx.push_back(H + W);
                idx.push_back(H + W + 1);
                for (int x : set<int>(idx.begin(), idx.end())) {
                    mp[x] = mp.size();
                }
            }
            max_flow<ll> mf(mp.size());
            set<int> used;
            for (auto [x, y] : P[i]) {
                mf.add_edge(mp[x], mp[H + y], 1);
                if (used.count(x) == 0) {
                    mf.add_edge(mp[H + W], mp[x], 1);
                    used.insert(x);
                }
                if (used.count(H + y) == 0) {
                    mf.add_edge(mp[H + y], mp[H + W + 1], 1);
                    used.insert(H + y);
                }
            }
            ans += mf.get_max_flow(mp[H + W], mp[H + W + 1]);
        }
    }

    cout << ans << endl;
}
0