結果
問題 | No.1479 Matrix Eraser |
ユーザー | SSRS |
提出日時 | 2021-04-16 20:20:00 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,622 bytes |
コンパイル時間 | 2,670 ms |
コンパイル使用メモリ | 198,632 KB |
実行使用メモリ | 26,240 KB |
最終ジャッジ日時 | 2024-07-02 22:22:05 |
合計ジャッジ時間 | 34,244 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
13,756 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 53 ms
6,940 KB |
testcase_08 | AC | 94 ms
8,960 KB |
testcase_09 | AC | 242 ms
14,592 KB |
testcase_10 | AC | 474 ms
22,784 KB |
testcase_11 | AC | 286 ms
16,512 KB |
testcase_12 | AC | 69 ms
7,424 KB |
testcase_13 | AC | 97 ms
8,960 KB |
testcase_14 | AC | 72 ms
7,936 KB |
testcase_15 | AC | 15 ms
6,940 KB |
testcase_16 | AC | 81 ms
8,448 KB |
testcase_17 | AC | 582 ms
26,112 KB |
testcase_18 | AC | 563 ms
26,112 KB |
testcase_19 | AC | 574 ms
26,240 KB |
testcase_20 | AC | 571 ms
26,112 KB |
testcase_21 | AC | 575 ms
26,112 KB |
testcase_22 | AC | 578 ms
26,112 KB |
testcase_23 | AC | 578 ms
26,112 KB |
testcase_24 | AC | 574 ms
26,240 KB |
testcase_25 | AC | 576 ms
26,112 KB |
testcase_26 | AC | 583 ms
26,112 KB |
testcase_27 | AC | 2,710 ms
7,296 KB |
testcase_28 | AC | 2,699 ms
7,296 KB |
testcase_29 | AC | 2,700 ms
7,296 KB |
testcase_30 | AC | 2,702 ms
7,424 KB |
testcase_31 | AC | 2,695 ms
7,424 KB |
testcase_32 | AC | 1,120 ms
13,808 KB |
testcase_33 | AC | 1,123 ms
13,624 KB |
testcase_34 | AC | 1,121 ms
13,760 KB |
testcase_35 | AC | 1,113 ms
13,632 KB |
testcase_36 | AC | 1,120 ms
13,716 KB |
testcase_37 | AC | 40 ms
6,940 KB |
testcase_38 | TLE | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; const int INF = 100000; template <typename Cap> struct ford_fulkerson{ struct edge{ int to, rev; Cap cap; edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){ } }; int N; vector<vector<edge>> G; ford_fulkerson(){ } ford_fulkerson(int N): N(N), G(N){ } void add_edge(int from, int to, Cap cap){ int id1 = G[from].size(); int id2 = G[to].size(); G[from].push_back(edge(to, id2, cap)); G[to].push_back(edge(from, id1, 0)); } Cap max_flow(int s, int t){ Cap flow = 0; while (1){ vector<Cap> m(N, INF); vector<int> pv(N, -1); vector<int> pe(N, -1); vector<bool> used(N, false); queue<int> Q; Q.push(s); used[s] = true; while (!Q.empty()){ int v = Q.front(); Q.pop(); int cnt = G[v].size(); for (int i = 0; i < cnt; i++){ int w = G[v][i].to; if (!used[w] && G[v][i].cap > 0){ used[w] = true; m[w] = min(m[v], G[v][i].cap); pv[w] = v; pe[w] = i; Q.push(w); } } } if (!used[t]){ break; } Cap f = m[t]; for (int i = t; i != s; i = pv[i]){ G[pv[i]][pe[i]].cap -= f; G[i][G[pv[i]][pe[i]].rev].cap += f; } flow += f; } return flow; } }; 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]; } } map<int, vector<pair<int, int>>> mp; for (int i = 0; i < H; i++){ for (int j = 0; j < W; j++){ if (A[i][j] > 0){ mp[A[i][j]].push_back(make_pair(i, j)); } } } int ans = 0; for (auto P : mp){ int cnt = P.second.size(); vector<int> X, Y; for (int i = 0; i < cnt; i++){ X.push_back(P.second[i].first); Y.push_back(P.second[i].second); } sort(X.begin(), X.end()); X.erase(unique(X.begin(), X.end()), X.end()); sort(Y.begin(), Y.end()); Y.erase(unique(Y.begin(), Y.end()), Y.end()); int Xcnt = X.size(), Ycnt = Y.size(); vector<int> x(cnt), y(cnt); for (int i = 0; i < cnt; i++){ x[i] = lower_bound(X.begin(), X.end(), P.second[i].first) - X.begin(); y[i] = lower_bound(Y.begin(), Y.end(), P.second[i].second) - Y.begin(); } ford_fulkerson<int> G(Xcnt + Ycnt + 2); for (int i = 0; i < Xcnt; i++){ G.add_edge(Xcnt + Ycnt, i, 1); } for (int i = 0; i < Ycnt; i++){ G.add_edge(Xcnt + i, Xcnt + Ycnt + 1, 1); } for (int i = 0; i < cnt; i++){ G.add_edge(x[i], Xcnt + y[i], 1); } ans += G.max_flow(Xcnt + Ycnt, Xcnt + Ycnt + 1); } cout << ans << endl; }