結果

問題 No.1479 Matrix Eraser
ユーザー se1ka2se1ka2
提出日時 2021-04-16 20:45:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 3,000 ms
コード長 4,794 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 100,380 KB
実行使用メモリ 43,456 KB
最終ジャッジ日時 2023-09-15 21:50:42
合計ジャッジ時間 8,475 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
26,848 KB
testcase_01 AC 13 ms
26,800 KB
testcase_02 AC 13 ms
26,820 KB
testcase_03 AC 13 ms
26,872 KB
testcase_04 AC 13 ms
26,808 KB
testcase_05 AC 13 ms
26,812 KB
testcase_06 AC 13 ms
26,796 KB
testcase_07 AC 33 ms
29,304 KB
testcase_08 AC 50 ms
30,556 KB
testcase_09 AC 101 ms
33,544 KB
testcase_10 AC 210 ms
38,312 KB
testcase_11 AC 116 ms
34,444 KB
testcase_12 AC 38 ms
29,264 KB
testcase_13 AC 51 ms
30,540 KB
testcase_14 AC 40 ms
29,344 KB
testcase_15 AC 19 ms
27,460 KB
testcase_16 AC 45 ms
29,824 KB
testcase_17 AC 258 ms
40,060 KB
testcase_18 AC 256 ms
40,084 KB
testcase_19 AC 255 ms
40,132 KB
testcase_20 AC 256 ms
40,140 KB
testcase_21 AC 253 ms
40,180 KB
testcase_22 AC 252 ms
40,048 KB
testcase_23 AC 256 ms
40,116 KB
testcase_24 AC 256 ms
40,056 KB
testcase_25 AC 258 ms
40,052 KB
testcase_26 AC 251 ms
40,120 KB
testcase_27 AC 201 ms
30,964 KB
testcase_28 AC 201 ms
31,148 KB
testcase_29 AC 203 ms
30,900 KB
testcase_30 AC 201 ms
31,028 KB
testcase_31 AC 202 ms
31,000 KB
testcase_32 AC 109 ms
36,460 KB
testcase_33 AC 108 ms
36,464 KB
testcase_34 AC 105 ms
36,276 KB
testcase_35 AC 108 ms
36,476 KB
testcase_36 AC 110 ms
36,492 KB
testcase_37 AC 51 ms
29,576 KB
testcase_38 AC 229 ms
30,008 KB
testcase_39 AC 154 ms
43,456 KB
testcase_40 AC 14 ms
26,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <map>
using namespace std;

template <typename T>
struct Edge
{
    int to;
    T cap;
    int rev;
    int id;
};

template <typename T>
struct Flow
{
    int n;
    int source;
    int sink;
    T current_flow;
    std::vector<int> d;
    std::vector<int> nx;
    std::vector<std::vector<Edge<T>>> g;
    std::vector<std::pair<int, int>> epos;
    
    Flow(){}
    
    Flow(int n, int source, int sink) : n(n), source(source), sink(sink), current_flow(0){
        d.resize(n);
        nx.resize(n);
        g.resize(n);
    }
    
    void add_edge(int from, int to, T cap){
        epos.push_back(std::pair<int, int>(from, (int)g[from].size()));
        g[from].push_back((Edge<T>){to, cap, (int)g[to].size(), (int)epos.size() - 1});
        g[to].push_back((Edge<T>){from, 0, (int)g[from].size() - 1, -1});
    }
    
    T delete_edge(int id){
        Edge<T> &e = g[epos[id].first][epos[id].second];
        Edge<T> &re = g[e.to][e.rev];
        int u = re.to, v = e.to;
        T delete_f = re.cap;
        e.cap = 0;
        re.cap = 0;
        T reverse_f = delete_f - add_flow(u, v, delete_f);
        add_flow(u, source, reverse_f);
        add_flow(sink, v, reverse_f);
        current_flow -= reverse_f;
        return current_flow;
    }
    
    void bfs(int s){
        fill(d.begin(), d.end(), -1);
        std::queue<int> que;
        d[s] = 0;
        que.push(s);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(Edge<T> &e : g[u]){
                int v = e.to;
                Edge<T> &re = g[v][e.rev];
                if(re.cap > 0 && d[v] == -1){
                    d[v] = d[u] + 1;
                    que.push(v);
                }
            }
        }
    }
    
    T dfs(int u, int s, T f){
        if(u == s) return f;
        for(int &i = nx[u]; i < (int)g[u].size(); i++){
            Edge<T> &e = g[u][i];
            int v = e.to;
            if(d[v] >= d[u] || e.cap == 0) continue;
            if(f == -1) f = e.cap;
            else f = std::min(f, e.cap);
            T fl = dfs(v, s, f);
            if(fl > 0){
                e.cap -= fl;
                g[e.to][e.rev].cap += fl;
                return fl;
            }
        }
        return 0;
    }
    
    T add_flow(int r, int s, T max_f){
        T res = 0;
        while(true){
            if(max_f == 0) return res;
            bfs(s);
            if(d[r] == -1) return res;
            for(int i = 0; i < n; i++) nx[i] = 0;
            for(T f; (f = dfs(r, s, max_f)) > 0;){
                res += f;
                if(max_f != -1) max_f -= f;
            }
        }
    }
    
    T max_flow(T max_f = -1){
        if(max_f != -1){
            max_f -= current_flow;
            if(max_f < 0){
                current_flow -= add_flow(sink, source, -max_f);
                return current_flow;
            }
        }
        current_flow += add_flow(source, sink, max_f);
        return current_flow;
    }
    
    int get_flow(int id){
        Edge<T> &e = g[epos[id].first][epos[id].second];
        Edge<T> &re = g[e.to][e.rev];
        return re.cap;
    }
};

struct BipartiteMatching
{
    int p;
    int q;
    Flow<int> g;
    
    BipartiteMatching(){}
    
    BipartiteMatching(int p, int q) : p(p), q(q){
        g.n = p + q + 2;
        g.source = p + q;
        g.sink = p + q + 1;
        g.current_flow = 0;
        g.d.resize(g.n);
        g.nx.resize(g.n);
        g.g.resize(g.n);
        for(int i = 0; i < p; i++) g.add_edge(p + q, i, 1);
        for(int i = p; i < p + q; i++) g.add_edge(i, p + q + 1, 1);
    }
    
    void add_edge(int u, int v){
        g.add_edge(u, v + p, 1);
    }
    
    int delete_edge(int id){
        return g.delete_edge(id + p + q);
    }
    
    int max_matching(){
        return g.max_flow();
    }
    
    bool is_used(int id){
        return g.get_flow(id + p + q);
    }
};

vector<int> x[500005], y[500005];

int main()
{
    int h, w;
    cin >> h >> w;
    int a[502][502];
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
            x[a[i][j]].push_back(i);
            y[a[i][j]].push_back(j);
        }
    }
    int ans = 0;
    for(int k = 500000; k > 0; k--){
        int m = x[k].size();
        if(m <= 1){
            ans += m;
            continue;
        }
        map<int, int> mpx, mpy;
        int nx = 0, ny = 0;
        for(int i = 0; i < m; i++){
            if(!mpx.count(x[k][i])) mpx[x[k][i]] = nx++;
            if(!mpy.count(y[k][i])) mpy[y[k][i]] = ny++;
        }
        BipartiteMatching g(nx, ny);
        for(int i = 0; i < m; i++) g.add_edge(mpx[x[k][i]], mpy[y[k][i]]);
        ans += g.max_matching();
    }
    cout << ans << endl;
}
0