結果

問題 No.1479 Matrix Eraser
ユーザー milanis48663220milanis48663220
提出日時 2021-05-24 23:26:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,230 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 138,364 KB
実行使用メモリ 21,720 KB
最終ジャッジ日時 2024-04-21 08:51:42
合計ジャッジ時間 10,785 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
21,720 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

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 FordFolkerson{
    public:
        FordFolkerson(int n){
            N = n;
            G = vector<vector<edge>>(n, vector<edge>());
            used = vector<bool>(n, false);
        }

        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){
                clear_used();
                int f = dfs(s, t, INF);
                if(f == 0){
                    break;
                }
                flow += f;
            }
            return flow;
        }

    private:
        vector<vector<edge>> G;
        vector<bool> used;
        int N;
        void clear_used(){
            for(int i = 0; i < N; i++) used[i] = false;
        }

        int dfs(int v, int t, int f){
            if(v == t) return f;
            used[v] = true;
            for(int i = 0; i < G[v].size(); i++){
                edge &e = G[v][i];
                if(!used[e.to] && e.cap > 0){
                    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;
        }
};

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();
    FordFolkerson 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