結果

問題 No.1479 Matrix Eraser
ユーザー firiexpfiriexp
提出日時 2021-04-16 22:26:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 3,000 ms
コード長 3,631 bytes
コンパイル時間 1,610 ms
コンパイル使用メモリ 126,888 KB
実行使用メモリ 64,560 KB
最終ジャッジ日時 2023-09-16 01:18:40
合計ジャッジ時間 9,156 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
38,280 KB
testcase_01 AC 17 ms
38,140 KB
testcase_02 AC 17 ms
38,252 KB
testcase_03 AC 17 ms
38,428 KB
testcase_04 AC 16 ms
38,316 KB
testcase_05 AC 17 ms
38,264 KB
testcase_06 AC 16 ms
38,268 KB
testcase_07 AC 46 ms
41,020 KB
testcase_08 AC 68 ms
43,132 KB
testcase_09 AC 129 ms
48,616 KB
testcase_10 AC 273 ms
56,412 KB
testcase_11 AC 160 ms
50,428 KB
testcase_12 AC 54 ms
41,892 KB
testcase_13 AC 67 ms
43,216 KB
testcase_14 AC 56 ms
42,224 KB
testcase_15 AC 24 ms
39,136 KB
testcase_16 AC 59 ms
42,812 KB
testcase_17 AC 340 ms
59,540 KB
testcase_18 AC 339 ms
59,580 KB
testcase_19 AC 333 ms
59,776 KB
testcase_20 AC 328 ms
59,796 KB
testcase_21 AC 333 ms
59,780 KB
testcase_22 AC 339 ms
59,556 KB
testcase_23 AC 334 ms
59,664 KB
testcase_24 AC 339 ms
59,732 KB
testcase_25 AC 337 ms
59,724 KB
testcase_26 AC 344 ms
59,664 KB
testcase_27 AC 164 ms
44,620 KB
testcase_28 AC 162 ms
44,660 KB
testcase_29 AC 160 ms
44,476 KB
testcase_30 AC 164 ms
44,660 KB
testcase_31 AC 165 ms
44,844 KB
testcase_32 AC 72 ms
47,736 KB
testcase_33 AC 73 ms
47,720 KB
testcase_34 AC 70 ms
47,804 KB
testcase_35 AC 72 ms
47,884 KB
testcase_36 AC 71 ms
47,672 KB
testcase_37 AC 54 ms
41,164 KB
testcase_38 AC 138 ms
45,324 KB
testcase_39 AC 216 ms
64,560 KB
testcase_40 AC 18 ms
38,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template<class T, bool directed>
class Dinic {
    void bfs(int s){
        fill(level.begin(),level.end(), -1);
        queue<int> Q;
        level[s] = 0;
        Q.emplace(s);
        while(!Q.empty()){
            int v = Q.front(); Q.pop();
            for (auto &&e : G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    Q.emplace(e.to);
                }
            }
        }
    }

    T dfs(int v, int t, T f){
        if(v == t) return f;
        for(int &i = iter[v]; i < G[v].size(); i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                T d = dfs(e.to, t, min(f,  e.cap));
                if(d == 0) continue;
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
        return 0;
    }
public:
    struct edge {
        int to{}; T cap; int rev{};
        edge() = default;
        edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
    };

    vector<vector<edge>> G;
    vector<int> level, iter;
    Dinic() = default;
    explicit Dinic(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, int cap){
        G[from].emplace_back(to, cap, G[to].size());
        G[to].emplace_back(from, directed ? 0 : cap,  G[from].size()-1);
    }


    T flow(int s, int t, T lim = INF<T>){
        T ret = 0;
        while(true) {
            bfs(s);
            if(level[t] < 0 || lim == 0) break;
            fill(iter.begin(),iter.end(), 0);
            while(true){
                T f = dfs(s, t, lim);
                if(f == 0) break;
                ret += f;
                lim -= f;
            }
        }
        return ret;
    }
};
int main() {
    int h, w;
    cin >> h >> w;
    vector<vector<int>> v(h, vector<int>(w, 0));
    vector<vector<pair<int, int>>> id(h, vector<pair<int, int>>(w));
    vector<vector<int>> r(500001), c(500001);
    vector<vector<pair<int, int>>> a(500001);
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> v[i][j];
            if(v[i][j] && (r[v[i][j]].empty() || r[v[i][j]].back() != i)) r[v[i][j]].emplace_back(i);
            if(v[i][j]) {
                id[i][j].first = r[v[i][j]].size()-1;
                a[v[i][j]].emplace_back(i, j);
            }
        }
    }
    for (int j = 0; j < w; ++j) {
        for (int i = 0; i < h; ++i) {
            if(v[i][j] && (c[v[i][j]].empty() || c[v[i][j]].back() != j)) c[v[i][j]].emplace_back(j);
            if(v[i][j]) id[i][j].second = c[v[i][j]].size()-1;
        }
    }
    int ans = 0;
    for (int i = 0; i <= 500000; ++i) {
        if(a[i].empty()) continue;
        int S = r[i].size(), T = c[i].size();
        if(S == 0) continue;
        if(S == 1 || T == 1){
            ans += 1;
            continue;
        }

        Dinic<int, true> G(S+T+2);
        for (int j = 0; j < S; ++j) G.add_edge(0, j+1, 1);
        for (int j = 0; j < T; ++j) G.add_edge(S+1+j, S+T+1, 1);
        for (auto &&[j, k] : a[i]) {
            G.add_edge(id[j][k].first+1, S+1+id[j][k].second, 1);

        }
        ans += G.flow(0, S+T+1, min(S, T));

    }
    cout << ans << "\n";
    return 0;
}
0