結果
問題 | No.1479 Matrix Eraser |
ユーザー | firiexp |
提出日時 | 2021-04-16 22:26:19 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 330 ms / 3,000 ms |
コード長 | 3,631 bytes |
コンパイル時間 | 1,578 ms |
コンパイル使用メモリ | 127,044 KB |
実行使用メモリ | 64,724 KB |
最終ジャッジ日時 | 2024-07-03 02:26:16 |
合計ジャッジ時間 | 8,903 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 17 ms
38,368 KB |
testcase_01 | AC | 16 ms
38,380 KB |
testcase_02 | AC | 17 ms
38,360 KB |
testcase_03 | AC | 17 ms
38,372 KB |
testcase_04 | AC | 21 ms
38,300 KB |
testcase_05 | AC | 18 ms
38,420 KB |
testcase_06 | AC | 20 ms
38,416 KB |
testcase_07 | AC | 44 ms
41,480 KB |
testcase_08 | AC | 75 ms
43,500 KB |
testcase_09 | AC | 124 ms
48,848 KB |
testcase_10 | AC | 271 ms
56,704 KB |
testcase_11 | AC | 154 ms
50,600 KB |
testcase_12 | AC | 64 ms
42,236 KB |
testcase_13 | AC | 69 ms
43,652 KB |
testcase_14 | AC | 66 ms
42,368 KB |
testcase_15 | AC | 25 ms
39,472 KB |
testcase_16 | AC | 70 ms
42,856 KB |
testcase_17 | AC | 315 ms
59,860 KB |
testcase_18 | AC | 324 ms
59,892 KB |
testcase_19 | AC | 325 ms
59,880 KB |
testcase_20 | AC | 320 ms
59,912 KB |
testcase_21 | AC | 321 ms
59,856 KB |
testcase_22 | AC | 317 ms
59,860 KB |
testcase_23 | AC | 329 ms
59,864 KB |
testcase_24 | AC | 314 ms
59,904 KB |
testcase_25 | AC | 330 ms
59,960 KB |
testcase_26 | AC | 322 ms
59,920 KB |
testcase_27 | AC | 154 ms
44,800 KB |
testcase_28 | AC | 162 ms
44,776 KB |
testcase_29 | AC | 169 ms
44,672 KB |
testcase_30 | AC | 165 ms
44,664 KB |
testcase_31 | AC | 167 ms
44,800 KB |
testcase_32 | AC | 80 ms
47,880 KB |
testcase_33 | AC | 71 ms
47,916 KB |
testcase_34 | AC | 78 ms
47,816 KB |
testcase_35 | AC | 69 ms
47,816 KB |
testcase_36 | AC | 77 ms
47,800 KB |
testcase_37 | AC | 55 ms
41,340 KB |
testcase_38 | AC | 150 ms
45,440 KB |
testcase_39 | AC | 210 ms
64,724 KB |
testcase_40 | AC | 18 ms
38,352 KB |
ソースコード
#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; }