結果

問題 No.1479 Matrix Eraser
ユーザー Ogtsn99Ogtsn99
提出日時 2021-04-23 17:04:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 348 ms / 3,000 ms
コード長 5,758 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 229,688 KB
実行使用メモリ 29,540 KB
最終ジャッジ日時 2024-07-04 07:13:26
合計ジャッジ時間 11,069 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
14,976 KB
testcase_01 AC 8 ms
14,860 KB
testcase_02 AC 8 ms
14,884 KB
testcase_03 AC 7 ms
14,856 KB
testcase_04 AC 8 ms
14,804 KB
testcase_05 AC 8 ms
14,832 KB
testcase_06 AC 8 ms
14,812 KB
testcase_07 AC 39 ms
15,708 KB
testcase_08 AC 61 ms
16,428 KB
testcase_09 AC 117 ms
18,032 KB
testcase_10 AC 214 ms
20,784 KB
testcase_11 AC 139 ms
18,788 KB
testcase_12 AC 47 ms
15,948 KB
testcase_13 AC 60 ms
16,328 KB
testcase_14 AC 48 ms
16,100 KB
testcase_15 AC 16 ms
15,148 KB
testcase_16 AC 56 ms
16,204 KB
testcase_17 AC 264 ms
21,856 KB
testcase_18 AC 261 ms
21,928 KB
testcase_19 AC 265 ms
21,892 KB
testcase_20 AC 262 ms
21,940 KB
testcase_21 AC 256 ms
21,852 KB
testcase_22 AC 259 ms
21,964 KB
testcase_23 AC 261 ms
21,848 KB
testcase_24 AC 262 ms
22,020 KB
testcase_25 AC 259 ms
22,096 KB
testcase_26 AC 256 ms
22,020 KB
testcase_27 AC 339 ms
21,032 KB
testcase_28 AC 333 ms
21,008 KB
testcase_29 AC 346 ms
20,708 KB
testcase_30 AC 348 ms
20,780 KB
testcase_31 AC 340 ms
21,048 KB
testcase_32 AC 270 ms
29,540 KB
testcase_33 AC 270 ms
29,288 KB
testcase_34 AC 269 ms
29,400 KB
testcase_35 AC 269 ms
29,160 KB
testcase_36 AC 266 ms
29,044 KB
testcase_37 AC 25 ms
19,256 KB
testcase_38 AC 346 ms
19,072 KB
testcase_39 AC 241 ms
22,784 KB
testcase_40 AC 9 ms
14,976 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