結果
問題 | No.1479 Matrix Eraser |
ユーザー |
![]() |
提出日時 | 2021-04-16 20:45:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 290 ms / 3,000 ms |
コード長 | 4,794 bytes |
コンパイル時間 | 1,372 ms |
コンパイル使用メモリ | 100,664 KB |
実行使用メモリ | 43,520 KB |
最終ジャッジ日時 | 2024-07-02 23:16:52 |
合計ジャッジ時間 | 8,531 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
#include <iostream>#include <queue>#include <map>using namespace std;template <typename T>struct Edge{int to;T cap;int rev;int id;};template <typename T>struct Flow{int n;int source;int sink;T current_flow;std::vector<int> d;std::vector<int> nx;std::vector<std::vector<Edge<T>>> g;std::vector<std::pair<int, int>> epos;Flow(){}Flow(int n, int source, int sink) : n(n), source(source), sink(sink), current_flow(0){d.resize(n);nx.resize(n);g.resize(n);}void add_edge(int from, int to, T cap){epos.push_back(std::pair<int, int>(from, (int)g[from].size()));g[from].push_back((Edge<T>){to, cap, (int)g[to].size(), (int)epos.size() - 1});g[to].push_back((Edge<T>){from, 0, (int)g[from].size() - 1, -1});}T delete_edge(int id){Edge<T> &e = g[epos[id].first][epos[id].second];Edge<T> &re = g[e.to][e.rev];int u = re.to, v = e.to;T delete_f = re.cap;e.cap = 0;re.cap = 0;T reverse_f = delete_f - add_flow(u, v, delete_f);add_flow(u, source, reverse_f);add_flow(sink, v, reverse_f);current_flow -= reverse_f;return current_flow;}void bfs(int s){fill(d.begin(), d.end(), -1);std::queue<int> que;d[s] = 0;que.push(s);while(que.size()){int u = que.front();que.pop();for(Edge<T> &e : g[u]){int v = e.to;Edge<T> &re = g[v][e.rev];if(re.cap > 0 && d[v] == -1){d[v] = d[u] + 1;que.push(v);}}}}T dfs(int u, int s, T f){if(u == s) return f;for(int &i = nx[u]; i < (int)g[u].size(); i++){Edge<T> &e = g[u][i];int v = e.to;if(d[v] >= d[u] || e.cap == 0) continue;if(f == -1) f = e.cap;else f = std::min(f, e.cap);T fl = dfs(v, s, f);if(fl > 0){e.cap -= fl;g[e.to][e.rev].cap += fl;return fl;}}return 0;}T add_flow(int r, int s, T max_f){T res = 0;while(true){if(max_f == 0) return res;bfs(s);if(d[r] == -1) return res;for(int i = 0; i < n; i++) nx[i] = 0;for(T f; (f = dfs(r, s, max_f)) > 0;){res += f;if(max_f != -1) max_f -= f;}}}T max_flow(T max_f = -1){if(max_f != -1){max_f -= current_flow;if(max_f < 0){current_flow -= add_flow(sink, source, -max_f);return current_flow;}}current_flow += add_flow(source, sink, max_f);return current_flow;}int get_flow(int id){Edge<T> &e = g[epos[id].first][epos[id].second];Edge<T> &re = g[e.to][e.rev];return re.cap;}};struct BipartiteMatching{int p;int q;Flow<int> g;BipartiteMatching(){}BipartiteMatching(int p, int q) : p(p), q(q){g.n = p + q + 2;g.source = p + q;g.sink = p + q + 1;g.current_flow = 0;g.d.resize(g.n);g.nx.resize(g.n);g.g.resize(g.n);for(int i = 0; i < p; i++) g.add_edge(p + q, i, 1);for(int i = p; i < p + q; i++) g.add_edge(i, p + q + 1, 1);}void add_edge(int u, int v){g.add_edge(u, v + p, 1);}int delete_edge(int id){return g.delete_edge(id + p + q);}int max_matching(){return g.max_flow();}bool is_used(int id){return g.get_flow(id + p + q);}};vector<int> x[500005], y[500005];int main(){int h, w;cin >> h >> w;int a[502][502];for(int i = 0; i < h; i++){for(int j = 0; j < w; j++){cin >> a[i][j];x[a[i][j]].push_back(i);y[a[i][j]].push_back(j);}}int ans = 0;for(int k = 500000; k > 0; k--){int m = x[k].size();if(m <= 1){ans += m;continue;}map<int, int> mpx, mpy;int nx = 0, ny = 0;for(int i = 0; i < m; i++){if(!mpx.count(x[k][i])) mpx[x[k][i]] = nx++;if(!mpy.count(y[k][i])) mpy[y[k][i]] = ny++;}BipartiteMatching g(nx, ny);for(int i = 0; i < m; i++) g.add_edge(mpx[x[k][i]], mpy[y[k][i]]);ans += g.max_matching();}cout << ans << endl;}