結果

問題 No.1479 Matrix Eraser
ユーザー milanis48663220milanis48663220
提出日時 2021-05-25 20:55:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 833 ms / 3,000 ms
コード長 3,842 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 141,916 KB
実行使用メモリ 73,756 KB
最終ジャッジ日時 2024-04-22 12:28:17
合計ジャッジ時間 19,789 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 60 ms
11,508 KB
testcase_08 AC 114 ms
17,380 KB
testcase_09 AC 292 ms
33,800 KB
testcase_10 AC 637 ms
61,212 KB
testcase_11 AC 419 ms
39,384 KB
testcase_12 AC 102 ms
13,824 KB
testcase_13 AC 117 ms
17,556 KB
testcase_14 AC 87 ms
14,428 KB
testcase_15 AC 16 ms
6,940 KB
testcase_16 AC 99 ms
15,756 KB
testcase_17 AC 815 ms
73,756 KB
testcase_18 AC 833 ms
73,664 KB
testcase_19 AC 825 ms
73,696 KB
testcase_20 AC 815 ms
73,736 KB
testcase_21 AC 814 ms
73,648 KB
testcase_22 AC 820 ms
73,664 KB
testcase_23 AC 820 ms
73,660 KB
testcase_24 AC 809 ms
73,640 KB
testcase_25 AC 817 ms
73,680 KB
testcase_26 AC 832 ms
73,692 KB
testcase_27 AC 678 ms
26,420 KB
testcase_28 AC 621 ms
26,408 KB
testcase_29 AC 694 ms
26,428 KB
testcase_30 AC 709 ms
26,420 KB
testcase_31 AC 632 ms
26,404 KB
testcase_32 AC 84 ms
13,184 KB
testcase_33 AC 85 ms
13,184 KB
testcase_34 AC 83 ms
13,184 KB
testcase_35 AC 84 ms
13,184 KB
testcase_36 AC 84 ms
13,056 KB
testcase_37 AC 16 ms
6,944 KB
testcase_38 AC 437 ms
73,712 KB
testcase_39 AC 582 ms
73,720 KB
testcase_40 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const int INF = 100000000;

struct edge {int to, cap, rev;};

class Dinic{
    public:
        Dinic(int n){
            N = n;
            G = vector<vector<edge>>(n, vector<edge>());
            level = vector<int>(n, -1);
            iter = vector<int>(n, 0);
        }

        void add_edge(int from, int to, int cap){
            G[from].push_back((edge{to, cap, (int)G[to].size()}));
            G[to].push_back((edge{from, 0, (int)G[from].size()-1}));
        }

        int max_flow(int s, int t){
            int flow = 0;
            while(true){
                bfs(s);
                if(level[t] < 0) return flow;
                clear_iter();
                int f;
                while(f = dfs(s, t, INF) > 0){
                    flow += f;
                }
            }
        }

    private:
        vector<vector<edge>> G;
        vector<int> level;
        vector<int> iter;
        int N;
        void clear_level(){
            for(int i = 0; i < N; i++) level[i] = -1;
        }
        void clear_iter(){
            for(int i = 0; i < N; i++) iter[i] = 0;
        }

        int dfs(int v, int t, int 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]){
                    int d = dfs(e.to, t, min(f, e.cap));
                    if(d > 0){
                        e.cap -= d;
                        G[e.to][e.rev].cap += d;
                        return d;
                    }
                }
            }
            return 0;
        }

        void bfs(int s){
            clear_level();
            queue<int> que;
            level[s] = 0;
            que.push(s);
            while(!que.empty()){
                int v = que.front();
                que.pop();
                for(edge e : G[v]){
                    if(e.cap > 0 && level[e.to] < 0){
                        level[e.to] = level[v]+1;
                        que.push(e.to);
                    }
                }
            }
        }
};

using P = pair<int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    map<P, int> idx;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
            if(a[i][j] == 0) continue;
            // row
            if(idx.count(P(i, a[i][j])) == 0) idx[P(i, a[i][j])] = idx.size();
            // col
            if(idx.count(P(h+j, a[i][j])) == 0) idx[P(h+j, a[i][j])] = idx.size();
        }
    }
    int m = idx.size();
    Dinic mf(2+m);
    int s = 0, t = m+1;
    vector<bool> used(m+1);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(a[i][j] == 0) continue;
            int v1 = idx[P(i, a[i][j])]+1;
            int v2 = idx[P(h+j, a[i][j])]+1;
            if(!used[v1]) mf.add_edge(s, v1, 1);
            if(!used[v2]) mf.add_edge(v2, t, 1);
            used[v1] = true;
            used[v2] = true;
            mf.add_edge(v1, v2, 1);
        }
    }
    cout << mf.max_flow(s, t) << endl;
}
0