結果
問題 | No.1479 Matrix Eraser |
ユーザー | siman |
提出日時 | 2022-03-16 11:48:44 |
言語 | C++17(clang) (17.0.6 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 377 ms / 3,000 ms |
コード長 | 3,210 bytes |
コンパイル時間 | 1,630 ms |
コンパイル使用メモリ | 148,824 KB |
実行使用メモリ | 25,464 KB |
最終ジャッジ日時 | 2024-09-24 07:48:02 |
合計ジャッジ時間 | 9,570 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 40 ms
6,940 KB |
testcase_08 | AC | 73 ms
7,616 KB |
testcase_09 | AC | 160 ms
12,184 KB |
testcase_10 | AC | 291 ms
17,816 KB |
testcase_11 | AC | 188 ms
13,204 KB |
testcase_12 | AC | 51 ms
6,940 KB |
testcase_13 | AC | 70 ms
7,612 KB |
testcase_14 | AC | 54 ms
6,940 KB |
testcase_15 | AC | 13 ms
6,944 KB |
testcase_16 | AC | 64 ms
7,532 KB |
testcase_17 | AC | 375 ms
21,300 KB |
testcase_18 | AC | 377 ms
21,196 KB |
testcase_19 | AC | 367 ms
21,328 KB |
testcase_20 | AC | 373 ms
21,324 KB |
testcase_21 | AC | 367 ms
21,328 KB |
testcase_22 | AC | 376 ms
21,320 KB |
testcase_23 | AC | 377 ms
21,328 KB |
testcase_24 | AC | 373 ms
21,324 KB |
testcase_25 | AC | 352 ms
21,192 KB |
testcase_26 | AC | 373 ms
21,324 KB |
testcase_27 | AC | 163 ms
6,940 KB |
testcase_28 | AC | 164 ms
6,940 KB |
testcase_29 | AC | 163 ms
6,940 KB |
testcase_30 | AC | 162 ms
6,940 KB |
testcase_31 | AC | 166 ms
6,940 KB |
testcase_32 | AC | 61 ms
8,788 KB |
testcase_33 | AC | 61 ms
8,796 KB |
testcase_34 | AC | 63 ms
8,664 KB |
testcase_35 | AC | 63 ms
8,788 KB |
testcase_36 | AC | 62 ms
8,796 KB |
testcase_37 | AC | 41 ms
6,944 KB |
testcase_38 | AC | 159 ms
6,944 KB |
testcase_39 | AC | 314 ms
25,464 KB |
testcase_40 | AC | 2 ms
6,940 KB |
ソースコード
#include <cassert> #include <cmath> #include <algorithm> #include <iostream> #include <iomanip> #include <limits.h> #include <map> #include <unordered_map> #include <queue> #include <set> #include <string.h> #include <vector> using namespace std; typedef long long ll; const int INF = INT_MAX; struct Edge { int to; int cap; int rev; Edge(int to = -1, int cap = -1, int rev = -1) { this->to = to; this->cap = cap; this->rev = rev; } }; const int MAX_N = 100; const int MAX_V = 1010; class MaxFlow { public: int V; vector<int> level; vector<int> iter; vector <vector<Edge>> G; MaxFlow(int V) { this->V = V; level.resize(V); iter.resize(V); G.resize(V); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } void bfs(int s) { level = vector<int>(V, -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < (int) G[v].size(); ++i) { Edge *e = &G[v][i]; if (e->cap > 0 && level[e->to] < 0) { level[e->to] = level[v] + 1; que.push(e->to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < (int) G[v].size(); ++i) { Edge *e = &G[v][i]; if (e->cap > 0 && level[v] < level[e->to]) { int d = dfs(e->to, t, min(f, e->cap)); if (d > 0) { e->cap -= d; G[e->to][e->rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { bfs(s); if (level[t] < 0) return flow; iter = vector<int>(V, 0); int f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } } }; int main() { int H, W; cin >> H >> W; unordered_map<int, vector<int>> S; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { int a; cin >> a; int z = y * W + x; S[a].push_back(z); } } int ans = 0; for (auto &[a, positions] : S) { bool checked[H + W]; memset(checked, false, sizeof(checked)); if (a == 0) continue; int row_num = 0; int col_num = 0; unordered_map<int, int> hor; unordered_map<int, int> ver; for (int z : positions) { int y = z / W; int x = z % W; if (not hor.count(y)) { hor[y] = row_num; row_num++; } if (not ver.count(x)) { ver[x] = col_num; col_num++; } } int s = row_num + col_num; int t = row_num + col_num + 1; MaxFlow mf(col_num + row_num + 2); for (auto [y, idx] : hor) { mf.add_edge(s, idx, 1); } for (auto [x, idx] : ver) { mf.add_edge(row_num + idx, t, 1); } for (int z : positions) { int y = z / W; int x = z % W; mf.add_edge(hor[y], row_num + ver[x], 1); } ans += mf.max_flow(s, t); } cout << ans << endl; return 0; }