結果

問題 No.1479 Matrix Eraser
ユーザー Ogtsn99Ogtsn99
提出日時 2021-04-23 17:04:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 380 ms / 3,000 ms
コード長 5,758 bytes
コンパイル時間 2,579 ms
コンパイル使用メモリ 226,404 KB
実行使用メモリ 29,528 KB
最終ジャッジ日時 2023-09-17 11:13:01
合計ジャッジ時間 12,755 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
14,796 KB
testcase_01 AC 8 ms
14,636 KB
testcase_02 AC 8 ms
14,816 KB
testcase_03 AC 8 ms
14,800 KB
testcase_04 AC 8 ms
14,784 KB
testcase_05 AC 8 ms
14,800 KB
testcase_06 AC 8 ms
14,816 KB
testcase_07 AC 41 ms
15,672 KB
testcase_08 AC 65 ms
16,176 KB
testcase_09 AC 129 ms
18,028 KB
testcase_10 AC 239 ms
20,608 KB
testcase_11 AC 152 ms
18,588 KB
testcase_12 AC 51 ms
15,840 KB
testcase_13 AC 64 ms
16,344 KB
testcase_14 AC 53 ms
15,928 KB
testcase_15 AC 17 ms
15,084 KB
testcase_16 AC 56 ms
16,208 KB
testcase_17 AC 284 ms
22,064 KB
testcase_18 AC 283 ms
21,952 KB
testcase_19 AC 281 ms
21,960 KB
testcase_20 AC 285 ms
21,944 KB
testcase_21 AC 281 ms
21,916 KB
testcase_22 AC 283 ms
21,948 KB
testcase_23 AC 282 ms
21,924 KB
testcase_24 AC 288 ms
21,732 KB
testcase_25 AC 285 ms
21,960 KB
testcase_26 AC 289 ms
21,804 KB
testcase_27 AC 357 ms
20,976 KB
testcase_28 AC 350 ms
20,912 KB
testcase_29 AC 380 ms
20,576 KB
testcase_30 AC 353 ms
20,744 KB
testcase_31 AC 350 ms
21,008 KB
testcase_32 AC 281 ms
29,528 KB
testcase_33 AC 287 ms
29,040 KB
testcase_34 AC 280 ms
29,360 KB
testcase_35 AC 278 ms
29,084 KB
testcase_36 AC 279 ms
28,976 KB
testcase_37 AC 26 ms
19,692 KB
testcase_38 AC 355 ms
18,852 KB
testcase_39 AC 253 ms
22,560 KB
testcase_40 AC 8 ms
14,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define rep(i, n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i, n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define miele(v) min_element(v.begin(), v.end())
#define maele(v) max_element(v.begin(), v.end())
#define SUM(v) accumulate(v.begin(), v.end(), 0LL)
#define lb(a, key) lower_bound(a.begin(),a.end(),key)
#define ub(a, key) upper_bound(a.begin(),a.end(),key)
#define COUNT(a, key) count(a.begin(), a.end(), key)
#define BITCOUNT(x) __builtin_popcount(x)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
using P = pair<int, int>;
using WeightedGraph = vector<vector<P>>;
using UnWeightedGraph = vector<vector<int>>;
using Real = long double;
using Point = complex<Real>; //Point and Vector2d is the same!
// p.real() or real(p) -> x軸, p.imag() or imag(p) -> y軸
using Vector2d = complex<Real>;
const int MOD = 1000000007;
const long long INF = 1LL << 60;
const double EPS = 1e-15;
const double PI = 3.14159265358979323846;

template<typename T>
int getIndexOfLowerBound(vector<T> &v, T x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
}

template<typename T>
int getIndexOfUpperBound(vector<T> &v, T x) {
    return upper_bound(v.begin(), v.end(), x) - v.begin();
}

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

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

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

istream &operator>>(istream &is, Point &p) {
    Real a, b;
    is >> a >> b;
    p = Point(a, b);
    return is;
}

template<typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p_var) {
    is >> p_var.first >> p_var.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}

template<typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    os << pair_var.first << ' ' << pair_var.second;
    return os;
}

template<typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
    for (int i = 0; i < vec.size(); i++)
        os << vec[i] << ' ';
    return os;
}

template<typename T, typename U>
ostream &operator<<(ostream &os, vector<pair<T, U>> &vec) {
    for (int i = 0; i < vec.size(); i++)
        os << vec[i] << '\n';
    return os;
}

template<typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &df) {
    for (auto &vec : df) os << vec;
    return os;
}

template<typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    repi(itr, map_var) {
        os << *itr << ' ';
        itr++;
        itr--;
    }
    return os;
}

template<typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    repi(itr, set_var) {
        os << *itr << ' ';
        itr++;
        itr--;
    }
    return os;
}

void print() { cout << endl; }

template<class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << " ";
    print(forward<Tail>(tail)...);
}

bool isOk(int mid, int c) {
    if(3 * mid >= c) return true;
    else return false;
}

template <class T>
unordered_map <T, int> compression(vector <T> tmp){
    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
    unordered_map <int, int> ma;
    for(int i=0;i<tmp.size();++i) ma[tmp[i]] = i;
    return ma;
}

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

void add_edge(vector <vector<edge>> &G, 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 dfs(vector <vector<edge>> &G, vector <bool> &used, 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(G, used, e.to, t, min(f,e.cap));
            if(d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(vector <vector<edge>> &G, vector <bool> &used, int s, int t){
    int flow = 0;
    for(;;){
        vector <bool> tmp(used.size());
        used = tmp;
        int f = dfs(G, used, s,t,INF);
        if(f == 0) return flow;
        flow += f;
    }
}

signed main(void) { cin.tie(0); ios::sync_with_stdio(false);
    int h, w; cin>>h>>w;
    vector <vector<pair <int, int>>> data(500001);

    rep(i, h) {
        rep(j, w) {
            int a;
            cin>>a;
            data[a].pb({i, j});
        }
    }

    int ans = 0;
    for (int i = 1; i < 500001; ++i) {
        int sz = data[i].size();
        if(sz == 0) continue;
        vector<int> row_tmp(sz), col_tmp(sz);
        for (int j = 0; j < sz; ++j) {
            row_tmp[j] = data[i][j].F;
            col_tmp[j] = data[i][j].S;
        }

        unordered_map <int, int> row_map = compression(row_tmp);
        unordered_map <int, int> col_map = compression(col_tmp);
        int rs = row_map.size(), cs = col_map.size();

        vector <vector<edge>> G(rs + cs +2);
        vector <bool> used(rs + cs + 2);
        int start = rs + cs;
        int goal = start+1;

        for (auto p : row_map) {
            add_edge(G, start, p.S, 1);
        }

        for (auto p : col_map) {
            add_edge(G, p.S + rs, goal, 1);
        }

        for (auto p : data[i]) {
            add_edge(G, row_map[p.F], col_map[p.S] + rs, 1);
        }

        ans += max_flow(G, used, start, goal);
    }
    print(ans);
}
0