結果
問題 | No.1479 Matrix Eraser |
ユーザー | firiexp |
提出日時 | 2021-04-16 22:26:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 397 ms / 3,000 ms |
コード長 | 3,631 bytes |
コンパイル時間 | 1,669 ms |
コンパイル使用メモリ | 121,880 KB |
最終ジャッジ日時 | 2025-01-20 20:09:43 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; template<class T, bool directed> class Dinic { void bfs(int s){ fill(level.begin(),level.end(), -1); queue<int> Q; level[s] = 0; Q.emplace(s); while(!Q.empty()){ int v = Q.front(); Q.pop(); for (auto &&e : G[v]){ if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; Q.emplace(e.to); } } } } T dfs(int v, int t, T f){ if(v == t) return f; for(int &i = iter[v]; i < G[v].size(); i++){ edge &e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ T d = dfs(e.to, t, min(f, e.cap)); if(d == 0) continue; e.cap -= d; G[e.to][e.rev].cap += d; return d; } } return 0; } public: struct edge { int to{}; T cap; int rev{}; edge() = default; edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {} }; vector<vector<edge>> G; vector<int> level, iter; Dinic() = default; explicit Dinic(int n) : G(n), level(n), iter(n) {} void add_edge(int from, int to, int cap){ G[from].emplace_back(to, cap, G[to].size()); G[to].emplace_back(from, directed ? 0 : cap, G[from].size()-1); } T flow(int s, int t, T lim = INF<T>){ T ret = 0; while(true) { bfs(s); if(level[t] < 0 || lim == 0) break; fill(iter.begin(),iter.end(), 0); while(true){ T f = dfs(s, t, lim); if(f == 0) break; ret += f; lim -= f; } } return ret; } }; int main() { int h, w; cin >> h >> w; vector<vector<int>> v(h, vector<int>(w, 0)); vector<vector<pair<int, int>>> id(h, vector<pair<int, int>>(w)); vector<vector<int>> r(500001), c(500001); vector<vector<pair<int, int>>> a(500001); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { cin >> v[i][j]; if(v[i][j] && (r[v[i][j]].empty() || r[v[i][j]].back() != i)) r[v[i][j]].emplace_back(i); if(v[i][j]) { id[i][j].first = r[v[i][j]].size()-1; a[v[i][j]].emplace_back(i, j); } } } for (int j = 0; j < w; ++j) { for (int i = 0; i < h; ++i) { if(v[i][j] && (c[v[i][j]].empty() || c[v[i][j]].back() != j)) c[v[i][j]].emplace_back(j); if(v[i][j]) id[i][j].second = c[v[i][j]].size()-1; } } int ans = 0; for (int i = 0; i <= 500000; ++i) { if(a[i].empty()) continue; int S = r[i].size(), T = c[i].size(); if(S == 0) continue; if(S == 1 || T == 1){ ans += 1; continue; } Dinic<int, true> G(S+T+2); for (int j = 0; j < S; ++j) G.add_edge(0, j+1, 1); for (int j = 0; j < T; ++j) G.add_edge(S+1+j, S+T+1, 1); for (auto &&[j, k] : a[i]) { G.add_edge(id[j][k].first+1, S+1+id[j][k].second, 1); } ans += G.flow(0, S+T+1, min(S, T)); } cout << ans << "\n"; return 0; }