結果

問題 No.1479 Matrix Eraser
ユーザー AhykwAhykw
提出日時 2021-04-16 22:44:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 3,000 ms
コード長 4,047 bytes
コンパイル時間 3,489 ms
コンパイル使用メモリ 242,904 KB
実行使用メモリ 36,696 KB
最終ジャッジ日時 2023-09-16 01:49:34
合計ジャッジ時間 10,916 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
26,416 KB
testcase_01 AC 13 ms
26,392 KB
testcase_02 AC 13 ms
26,456 KB
testcase_03 AC 14 ms
26,304 KB
testcase_04 AC 14 ms
26,452 KB
testcase_05 AC 13 ms
26,312 KB
testcase_06 AC 14 ms
26,556 KB
testcase_07 AC 40 ms
27,476 KB
testcase_08 AC 60 ms
28,416 KB
testcase_09 AC 114 ms
30,536 KB
testcase_10 AC 204 ms
33,500 KB
testcase_11 AC 132 ms
31,012 KB
testcase_12 AC 48 ms
27,936 KB
testcase_13 AC 61 ms
28,496 KB
testcase_14 AC 48 ms
27,888 KB
testcase_15 AC 22 ms
26,772 KB
testcase_16 AC 54 ms
28,104 KB
testcase_17 AC 246 ms
34,904 KB
testcase_18 AC 248 ms
34,632 KB
testcase_19 AC 244 ms
34,852 KB
testcase_20 AC 245 ms
34,636 KB
testcase_21 AC 244 ms
34,836 KB
testcase_22 AC 245 ms
34,792 KB
testcase_23 AC 246 ms
34,752 KB
testcase_24 AC 243 ms
34,880 KB
testcase_25 AC 243 ms
34,752 KB
testcase_26 AC 246 ms
34,896 KB
testcase_27 AC 182 ms
31,724 KB
testcase_28 AC 182 ms
31,916 KB
testcase_29 AC 182 ms
31,760 KB
testcase_30 AC 182 ms
31,812 KB
testcase_31 AC 184 ms
31,732 KB
testcase_32 AC 83 ms
36,400 KB
testcase_33 AC 82 ms
36,376 KB
testcase_34 AC 82 ms
36,360 KB
testcase_35 AC 83 ms
36,696 KB
testcase_36 AC 84 ms
36,504 KB
testcase_37 AC 28 ms
28,416 KB
testcase_38 AC 198 ms
30,472 KB
testcase_39 AC 222 ms
36,140 KB
testcase_40 AC 14 ms
26,408 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;
    }
};

template<typename T>
T bipartite_matching(const vector<Pin>& edge) {
    map<ll, ll> m1, m2;
    for(auto [s, t]: edge) {
        if (m1.find(s) == m1.end()) m1[s] = m1.size();
        if (m2.find(t) == m2.end()) m2[t] = m2.size();
    }

    ll X = m1.size(), Y = m2.size();
    ll start = X + Y, goal = X + Y + 1;
    Dinic<T> g(X + Y + 2);

    for (auto [s, t]: edge) {
        g.add_edge(m1[s], X + m2[t], 1);
    }

    for (auto [_, s]: m1) {
        g.add_edge(start, s, 1);
    }

    for (auto [_, t]: m2) {
        g.add_edge(X + t, goal, 1);
    }

    return g(start, goal);
}

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;
        ans += bipartite_matching<ll>(e);
    }

    print(ans);
}
0