結果

問題 No.1479 Matrix Eraser
ユーザー AhykwAhykw
提出日時 2021-04-16 22:28:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 3,000 ms
コード長 4,040 bytes
コンパイル時間 3,620 ms
コンパイル使用メモリ 242,816 KB
実行使用メモリ 36,648 KB
最終ジャッジ日時 2023-09-16 01:24:31
合計ジャッジ時間 9,752 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
26,472 KB
testcase_01 AC 12 ms
26,664 KB
testcase_02 AC 13 ms
26,344 KB
testcase_03 AC 12 ms
26,608 KB
testcase_04 AC 12 ms
26,348 KB
testcase_05 AC 11 ms
26,540 KB
testcase_06 AC 11 ms
26,252 KB
testcase_07 AC 35 ms
27,532 KB
testcase_08 AC 52 ms
28,392 KB
testcase_09 AC 103 ms
30,212 KB
testcase_10 AC 186 ms
33,628 KB
testcase_11 AC 120 ms
31,308 KB
testcase_12 AC 44 ms
27,932 KB
testcase_13 AC 55 ms
28,456 KB
testcase_14 AC 45 ms
27,992 KB
testcase_15 AC 19 ms
27,020 KB
testcase_16 AC 49 ms
28,264 KB
testcase_17 AC 219 ms
34,720 KB
testcase_18 AC 222 ms
34,812 KB
testcase_19 AC 219 ms
34,796 KB
testcase_20 AC 222 ms
34,792 KB
testcase_21 AC 219 ms
34,868 KB
testcase_22 AC 222 ms
34,796 KB
testcase_23 AC 222 ms
34,724 KB
testcase_24 AC 227 ms
34,868 KB
testcase_25 AC 212 ms
34,732 KB
testcase_26 AC 218 ms
34,660 KB
testcase_27 AC 180 ms
31,732 KB
testcase_28 AC 180 ms
31,864 KB
testcase_29 AC 180 ms
31,860 KB
testcase_30 AC 181 ms
31,992 KB
testcase_31 AC 182 ms
31,700 KB
testcase_32 AC 80 ms
36,408 KB
testcase_33 AC 78 ms
36,396 KB
testcase_34 AC 77 ms
36,364 KB
testcase_35 AC 79 ms
36,648 KB
testcase_36 AC 80 ms
36,308 KB
testcase_37 AC 28 ms
28,640 KB
testcase_38 AC 197 ms
30,500 KB
testcase_39 AC 215 ms
36,184 KB
testcase_40 AC 12 ms
26,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

template <typename T>
void print(vector<T>& x) {
    int N = x.size();
    REP(i, N) {
        if (i > 0) cout << " ";
        cout << x[i];
    }
    cout << "\n";
}

// Dinic
template<typename T>
struct Dinic {

    struct Graph {
        int to, rev;
        T cap;

        Graph(int t, int r, const T &c) : to(t), rev(r), cap(c) {};
    };

    vector<vector<Graph>> g;
    vector<int> level;
    vector<int> iter;
    int sz;

    Dinic(int N) : sz(N) {
        g.resize(N);
        iter.resize(N);
        level.resize(N);
    }

    void add_edge(int from, int to, T cap) {
        g[from].eb(to, (int) g[to].size(), cap);
        g[to].eb(from, (int) g[from].size() - 1, 0);
    }

    T operator()(int start, int end) {
        T flow = 0;
        while(true) {
            bfs(start);
            if (level[end] < 0) break;

            iter.assign(sz, 0);

            T f;
            while(f = dfs(start, end, inf) > 0) {
                flow += f;
            }
        }
        return flow;
    }

private:
    void bfs(int s) {
        level.assign(sz, -1);

        level[s] = 0;
        queue<int> q;
        q.push(s);

        while(!q.empty()) {
            int v = q.front(); q.pop();
            for(auto &e: g[v]) {
                int t = e.to;
                if (e.cap > 0 && level[t] < 0) {
                    level[t] = level[v] + 1;
                    q.push(t);
                }
            }

        }
    }

    T dfs(int v, int t, T flow) {
        if (v == t || flow == 0) return flow;

        for (int &i = iter[v]; i < g[v].size(); ++i) {
            auto &e = g[v][i];
            if(e.cap <= 0 || level[v] >= level[e.to]) continue;
            if (T f = dfs(e.to, t, min(flow, e.cap)) > 0) {
                e.cap -= f;
                g[e.to][e.rev].cap += f;
                return f;
            }
        }

        return 0;
    }
};

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int h, w; cin >> h >> w;
    vector<vll> a(h, vll(w));
    REP(i, h) REP(j, w) cin >> a[i][j];

    vector<vector<Pin>> v(1e6);
    REP(i, h) REP(j, w) {
        if (a[i][j] == 0) continue;
        v[a[i][j]].eb(i, j);
    }

    ll ans = 0;
    for (auto &e: v) {
        if (e.empty()) continue;

        map<int, int> mi, mj;
        for(auto &e2: e) {
            if (mi.find(e2.fi) == mi.end()) mi[e2.fi] = mi.size();
            if (mj.find(e2.se) == mj.end()) mj[e2.se] = mj.size();
        }

        Dinic<ll> g(mi.size() + mj.size() + 2);

        for (auto &e2: e) {
            g.add_edge(mi[e2.fi], mi.size() + mj[e2.se], 1);
        }

        for (auto &p: mi) {
            g.add_edge(mi.size() + mj.size(), p.se, 1);
        }

        for (auto &p: mj) {
            g.add_edge(mi.size() + p.se, mi.size() + mj.size() + 1, 1);
        }

        ans += g(mi.size() + mj.size(), mi.size() + mj.size() + 1);
    }

    print(ans);
}
0