結果

問題 No.1479 Matrix Eraser
ユーザー ningenMeningenMe
提出日時 2021-04-16 22:49:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 3,000 ms
コード長 6,807 bytes
コンパイル時間 3,302 ms
コンパイル使用メモリ 235,320 KB
実行使用メモリ 24,340 KB
最終ジャッジ日時 2023-09-16 01:57:54
合計ジャッジ時間 10,786 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
14,896 KB
testcase_01 AC 8 ms
14,564 KB
testcase_02 AC 8 ms
14,696 KB
testcase_03 AC 7 ms
14,700 KB
testcase_04 AC 7 ms
14,856 KB
testcase_05 AC 7 ms
14,792 KB
testcase_06 AC 7 ms
14,716 KB
testcase_07 AC 39 ms
15,736 KB
testcase_08 AC 63 ms
16,116 KB
testcase_09 AC 124 ms
17,848 KB
testcase_10 AC 227 ms
19,996 KB
testcase_11 AC 142 ms
18,128 KB
testcase_12 AC 50 ms
15,772 KB
testcase_13 AC 62 ms
16,460 KB
testcase_14 AC 50 ms
15,856 KB
testcase_15 AC 15 ms
15,124 KB
testcase_16 AC 54 ms
16,044 KB
testcase_17 AC 267 ms
20,988 KB
testcase_18 AC 267 ms
20,932 KB
testcase_19 AC 266 ms
20,828 KB
testcase_20 AC 262 ms
20,788 KB
testcase_21 AC 270 ms
20,792 KB
testcase_22 AC 268 ms
20,936 KB
testcase_23 AC 270 ms
21,292 KB
testcase_24 AC 269 ms
20,952 KB
testcase_25 AC 271 ms
20,792 KB
testcase_26 AC 272 ms
21,140 KB
testcase_27 AC 164 ms
17,776 KB
testcase_28 AC 160 ms
17,976 KB
testcase_29 AC 164 ms
17,968 KB
testcase_30 AC 164 ms
17,952 KB
testcase_31 AC 166 ms
17,964 KB
testcase_32 AC 74 ms
24,176 KB
testcase_33 AC 74 ms
23,984 KB
testcase_34 AC 73 ms
24,340 KB
testcase_35 AC 74 ms
24,156 KB
testcase_36 AC 75 ms
24,308 KB
testcase_37 AC 24 ms
16,728 KB
testcase_38 AC 176 ms
17,068 KB
testcase_39 AC 255 ms
22,616 KB
testcase_40 AC 7 ms
14,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title Zarts - 座標圧縮
 * @docs md/util/Zarts.md
 */
template<class T> class Zarts{
    vector<T> value;
    map<T,int> key;
    size_t sz;
public:
    vector<int> compressed;
    Zarts(const vector<T> & ar, int light_flag = 0, T pre=-1) : compressed(ar.size()) {
        if(!light_flag) {
            for (auto &e : ar) key[e];
            int cnt=0;
            for (auto &e : key) e.second = cnt++;
            for (int i=0;i<ar.size();++i) compressed[i]=key[ar[i]];
            value.resize(key.size());
            for (auto &e : key) value[e.second] = e.first;
            sz = cnt;
        }
        else {
            vector<pair<int,int>> ord(ar.size());
            for(int i=0;i<ar.size();++i) ord[i]={ar[i],i};
            sort(ord.begin(),ord.end());
            int cnt=-1;
            for(int i=0;i<ar.size();++i) {
                if(pre < ord[i].first) cnt++;
                compressed[ord[i].second] = cnt;
                pre = ord[i].first;
            }
            sz = cnt+1;
        }
    }
    T get_value(int key) {
        return value[key];
    }
    int get_key(T value) {
        assert(key.count(value));
        return key[value];
    }
    size_t size() {
        return sz;
    }
};

/*
 * @title Dinic - Dinicフロー
 * @docs md/graph/Dinic.md
 */
template <class T> class Dinic {
	struct info {
		int to, rev;
		T cap;
	};
	T ini, inf;
	vector<vector<info>> edge;
	vector<int> level, iter;

	inline void bfs(int start) {
		for (int i = 0; i < level.size(); ++i) level[i] = -1;
		queue<int> q;
		level[start] = 0;
		q.push(start);
		while (q.size()) {
			int from = q.front();
			q.pop();
			for (auto& e : edge[from]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[from] + 1;
					q.push(e.to);
				}
			}
		}
	}

	inline T dfs(int from, int goal, T flow) {
		if (from == goal) return flow;
		for (int& i = iter[from]; i < edge[from].size(); ++i) {
			auto& e = edge[from][i];
			if (e.cap <= 0 || level[from] >= level[e.to]) continue;
			T dflow = dfs(e.to, goal, min(flow, e.cap));
			if (dflow <= 0) continue;
			e.cap -= dflow;
			edge[e.to][e.rev].cap += dflow;
			return dflow;
		}
		return ini;
	}

public:
	Dinic(int N, T ini, T inf) : edge(N), level(N), iter(N), ini(ini), inf(inf) {
		// do nothing
	}

	inline void make_edge(int from, int to, T cap) {
		edge[from].push_back({ to, (int)edge[to].size(), cap });
		edge[to].push_back({ from, (int)edge[from].size() - 1, ini });
	}

	inline T maxflow(int start, int goal) {
		T maxflow = ini;
		while (1) {
			bfs(start);
			if (level[goal] < 0) return maxflow;
			for (int i = 0; i < iter.size(); ++i) iter[i] = 0;
			T flow;
			while ((flow = dfs(start, goal, inf))>0) maxflow += flow;
		}
	}
};

//verify https://atcoder.jp/contests/arc085/tasks/arc085_c

int solve(vector<pair<int,int>> vp) {
    int n = vp.size();
    vector<int> Y(n),X(n);
    for(int i=0;i<n;++i) Y[i] = vp[i].first;
    for(int i=0;i<n;++i) X[i] = vp[i].second;
    Zarts<int> ZY(Y),ZX(X);
    int H = ZY.size();
    int W = ZX.size();
    int inf = 1000000;
    Dinic<int> di(H+W+2,0,inf);
    int S = H+W;
    int G = H+W+1;
    for(int i=0;i<H;++i) {
        di.make_edge(S,i,1);
    }
    for(int i=0;i<W;++i) {
        di.make_edge(i+H,G,1);
    }
    for(int i=0;i<n;++i) {
        int y = ZY.compressed[i];
        int x = ZX.compressed[i];
        di.make_edge(y,x+H,inf);
    }
    return di.maxflow(S,G);
}

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int H,W; cin >> H >> W;
    int M = 500010;
    vector<vector<pair<int,int>>> vvp(M);
    for(int i=0;i<H;++i) {
        for(int j=0;j<W;++j) {
            int a; cin >> a;
            vvp[a].push_back({i,j});
        }
    }
    int ans = 0;
    for(int n=1;n<M;++n) {
        if(vvp[n].empty()) continue;
        int cnt = solve(vvp[n]);
        ans += cnt;
    }
    cout << ans << endl;
    return 0;
}
0