結果
問題 | No.1479 Matrix Eraser |
ユーザー | Today03 |
提出日時 | 2024-05-24 09:32:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 424 ms / 3,000 ms |
コード長 | 3,681 bytes |
コンパイル時間 | 3,216 ms |
コンパイル使用メモリ | 245,232 KB |
実行使用メモリ | 23,972 KB |
最終ジャッジ日時 | 2024-12-20 19:16:05 |
合計ジャッジ時間 | 13,188 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
14,968 KB |
testcase_01 | AC | 7 ms
14,992 KB |
testcase_02 | AC | 9 ms
14,996 KB |
testcase_03 | AC | 8 ms
14,924 KB |
testcase_04 | AC | 8 ms
15,000 KB |
testcase_05 | AC | 8 ms
14,964 KB |
testcase_06 | AC | 8 ms
14,940 KB |
testcase_07 | AC | 54 ms
15,944 KB |
testcase_08 | AC | 88 ms
16,684 KB |
testcase_09 | AC | 177 ms
18,368 KB |
testcase_10 | AC | 339 ms
21,140 KB |
testcase_11 | AC | 209 ms
18,992 KB |
testcase_12 | AC | 69 ms
16,272 KB |
testcase_13 | AC | 85 ms
16,600 KB |
testcase_14 | AC | 69 ms
16,280 KB |
testcase_15 | AC | 21 ms
15,224 KB |
testcase_16 | AC | 75 ms
16,460 KB |
testcase_17 | AC | 390 ms
22,140 KB |
testcase_18 | AC | 398 ms
22,164 KB |
testcase_19 | AC | 399 ms
22,244 KB |
testcase_20 | AC | 392 ms
22,284 KB |
testcase_21 | AC | 397 ms
22,140 KB |
testcase_22 | AC | 395 ms
22,236 KB |
testcase_23 | AC | 406 ms
22,252 KB |
testcase_24 | AC | 400 ms
22,340 KB |
testcase_25 | AC | 397 ms
22,236 KB |
testcase_26 | AC | 395 ms
22,268 KB |
testcase_27 | AC | 291 ms
18,928 KB |
testcase_28 | AC | 290 ms
19,036 KB |
testcase_29 | AC | 293 ms
19,116 KB |
testcase_30 | AC | 287 ms
19,156 KB |
testcase_31 | AC | 303 ms
19,064 KB |
testcase_32 | AC | 133 ms
23,972 KB |
testcase_33 | AC | 133 ms
23,972 KB |
testcase_34 | AC | 132 ms
23,916 KB |
testcase_35 | AC | 130 ms
23,964 KB |
testcase_36 | AC | 134 ms
23,936 KB |
testcase_37 | AC | 52 ms
18,300 KB |
testcase_38 | AC | 424 ms
18,092 KB |
testcase_39 | AC | 386 ms
23,680 KB |
testcase_40 | AC | 9 ms
14,968 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; template <typename Cap> struct max_flow { max_flow(int n) { this->n = n; G = vector<vector<tuple<int, int, Cap>>>(n); } void add_edge(int u, int v, Cap c) { G[u].push_back(make_tuple(v, G[v].size(), c)); G[v].push_back(make_tuple(u, (int)G[u].size() - 1, 0)); } Cap get_max_flow(int start, int goal) { Cap ret = 0; while (true) { vector<int> dst = calculate_distance(start); if (dst[goal] < 0) { return ret; } vector<int> removed(n, 0); while (true) { Cap add = flowing(start, goal, numeric_limits<Cap>::max(), removed, dst); if (add == 0) { break; } ret += add; } } return ret; } private: int n; vector<vector<tuple<int, int, Cap>>> G; vector<int> calculate_distance(int start) { vector<int> dst(n, -1); dst[start] = 0; queue<int> que; que.push(start); while (!que.empty()) { int now = que.front(); que.pop(); for (auto [nxt, _, cap] : G[now]) { if (cap > 0 && dst[nxt] == -1) { dst[nxt] = dst[now] + 1; que.push(nxt); } } } return dst; } Cap flowing(int now, int goal, Cap limit, vector<int> &removed, vector<int> &dst) { if (now == goal) { return limit; } while (removed[now] < (int)G[now].size()) { auto [nxt, rev, cap] = G[now][removed[now]]; if (cap > 0 && dst[now] < dst[nxt]) { Cap flow = flowing(nxt, goal, min(limit, cap), removed, dst); if (flow > 0) { get<2>(G[now][removed[now]]) -= flow; get<2>(G[nxt][rev]) += flow; return flow; } } removed[now]++; } return 0; } }; const int MX = 5e5 + 5; int main() { int H, W; cin >> H >> W; vector<vector<int>> A(H, vector<int>(W)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A[i][j]; } } vector<vector<pair<int, int>>> P(MX); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { P[A[i][j]].push_back({i, j}); } } int ans = 0; for (int i = 1; i < MX; i++) { if (!P[i].empty()) { map<int, int> mp; { vector<int> idx; for (auto [x, y] : P[i]) { idx.push_back(x); idx.push_back(H + y); } idx.push_back(H + W); idx.push_back(H + W + 1); for (int x : set<int>(idx.begin(), idx.end())) { mp[x] = mp.size(); } } max_flow<ll> mf(mp.size()); set<int> used; for (auto [x, y] : P[i]) { mf.add_edge(mp[x], mp[H + y], 1); if (used.count(x) == 0) { mf.add_edge(mp[H + W], mp[x], 1); used.insert(x); } if (used.count(H + y) == 0) { mf.add_edge(mp[H + y], mp[H + W + 1], 1); used.insert(H + y); } } ans += mf.get_max_flow(mp[H + W], mp[H + W + 1]); } } cout << ans << endl; }