結果

問題 No.1479 Matrix Eraser
ユーザー outlineoutline
提出日時 2021-04-18 15:26:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,140 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 158,008 KB
実行使用メモリ 18,584 KB
最終ジャッジ日時 2023-09-17 07:45:37
合計ジャッジ時間 45,165 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 46 ms
4,912 KB
testcase_08 AC 122 ms
6,044 KB
testcase_09 AC 577 ms
9,244 KB
testcase_10 AC 2,334 ms
14,080 KB
testcase_11 AC 831 ms
10,256 KB
testcase_12 AC 66 ms
5,488 KB
testcase_13 AC 108 ms
6,088 KB
testcase_14 AC 70 ms
5,508 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 82 ms
5,700 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 119 ms
8,324 KB
testcase_28 AC 115 ms
8,216 KB
testcase_29 AC 117 ms
8,220 KB
testcase_30 AC 118 ms
8,224 KB
testcase_31 AC 122 ms
8,216 KB
testcase_32 AC 46 ms
13,044 KB
testcase_33 AC 46 ms
12,980 KB
testcase_34 AC 43 ms
13,132 KB
testcase_35 AC 44 ms
13,068 KB
testcase_36 AC 44 ms
12,972 KB
testcase_37 AC 16 ms
4,376 KB
testcase_38 AC 117 ms
7,264 KB
testcase_39 AC 119 ms
18,584 KB
testcase_40 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<typename flow_t>
struct Dinic{
    const flow_t INF;

    struct Edge{
        int to;
        flow_t cap;
        int rev;
        bool isrev;
        Edge(int to, flow_t cap, int rev, bool isrev) : to(to), cap(cap), rev(rev), isrev(isrev) {}
    };

    vector<vector<Edge>> graph;
    vector<int> min_cost;       // sからの距離(bfsで調べる)
    vector<int> iter;           // どこまで調べ終わったか(dfs時に使用)

    Dinic(int V) : INF(numeric_limits<flow_t>::max() / 2){
        graph.resize(V);
    }

    void add_edge(int from, int to, flow_t cap){
        graph[from].push_back(Edge(to, cap, graph[to].size(), false));
        graph[to].push_back(Edge(from, 0, graph[from].size() - 1, true));
    }

    // 最短路探索
    bool bfs(int s, int t){
        min_cost.assign(graph.size(), -1);
        queue<int> que;
        min_cost[s] = 0;
        que.push(s);
        while(!que.empty() && min_cost[t] == -1){
            int p = que.front();
            que.pop();
            for(auto &e : graph[p]){
                if(e.cap > 0 && min_cost[e.to] == -1){
                    min_cost[e.to] = min_cost[p] + 1;
                    que.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }

    flow_t dfs(int v, int t, flow_t flow){
        if(v == t)  return flow;
        for(int &i = iter[v]; i < (int)graph[v].size(); ++i){
            Edge &e = graph[v][i];
            if(e.cap > 0 && min_cost[v] < min_cost[e.to]){
                flow_t d = dfs(e.to, t, min(flow, e.cap));
                if(d > 0){
                    e.cap -= d;
                    graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    flow_t max_flow(int s, int t){
        flow_t flow = 0;
        // 最短路を求めて、そのパスに最大流を流す
        while(bfs(s, t)){
            iter.assign(graph.size(), 0);
            flow_t f = 0;
            while((f = dfs(s, t, INF)) > 0) flow += f;
        }
        return flow;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int H, W;
    cin >> H >> W;
    vector A(H, vector<int>(W));
    vector<int> vals;
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            cin >> A[i][j];
            if(A[i][j]) vals.emplace_back(A[i][j]);
        }
    }
    sort(begin(vals), end(vals));
    vals.erase(unique(begin(vals), end(vals)), end(vals));
    int sz = vals.size();
    vector<vector<pair<int, int>>> ps(sz);
    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            if(!A[i][j])    continue;
            int k = lower_bound(begin(vals), end(vals), A[i][j]) - begin(vals);
            ps[k].emplace_back(i, j);
        }
    }

    int ans = 0, s = H + W, t = s + 1, V = t + 1;
    for(int i = 0; i < sz; ++i){
        if(ps[i].size() <= 1) { ans += 1; continue; }
        Dinic<int> g(V);
        for(auto& [x, y] : ps[i])   g.add_edge(x, H + y, 1);
        for(int j = 0; j < H; ++j)  g.add_edge(s, j, 1);
        for(int j = 0; j < W; ++j)  g.add_edge(H + j, t, 1);
        ans += g.max_flow(s, t);
    }

    cout << ans << endl;
    
    return 0;
}
0