結果
問題 | No.1479 Matrix Eraser |
ユーザー | Ahykw |
提出日時 | 2021-04-16 22:28:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 260 ms / 3,000 ms |
コード長 | 4,040 bytes |
コンパイル時間 | 3,380 ms |
コンパイル使用メモリ | 245,580 KB |
実行使用メモリ | 36,748 KB |
最終ジャッジ日時 | 2024-07-03 02:34:52 |
合計ジャッジ時間 | 10,309 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 23 ms
26,624 KB |
testcase_01 | AC | 24 ms
26,624 KB |
testcase_02 | AC | 23 ms
26,624 KB |
testcase_03 | AC | 22 ms
26,752 KB |
testcase_04 | AC | 23 ms
26,624 KB |
testcase_05 | AC | 23 ms
26,624 KB |
testcase_06 | AC | 23 ms
26,624 KB |
testcase_07 | AC | 48 ms
27,776 KB |
testcase_08 | AC | 77 ms
28,544 KB |
testcase_09 | AC | 121 ms
30,464 KB |
testcase_10 | AC | 208 ms
33,536 KB |
testcase_11 | AC | 136 ms
31,232 KB |
testcase_12 | AC | 58 ms
28,160 KB |
testcase_13 | AC | 67 ms
28,544 KB |
testcase_14 | AC | 58 ms
28,160 KB |
testcase_15 | AC | 31 ms
27,008 KB |
testcase_16 | AC | 62 ms
28,416 KB |
testcase_17 | AC | 250 ms
34,944 KB |
testcase_18 | AC | 248 ms
34,944 KB |
testcase_19 | AC | 248 ms
34,816 KB |
testcase_20 | AC | 260 ms
34,944 KB |
testcase_21 | AC | 245 ms
34,944 KB |
testcase_22 | AC | 244 ms
34,944 KB |
testcase_23 | AC | 248 ms
34,816 KB |
testcase_24 | AC | 244 ms
34,944 KB |
testcase_25 | AC | 247 ms
34,944 KB |
testcase_26 | AC | 246 ms
34,944 KB |
testcase_27 | AC | 189 ms
31,744 KB |
testcase_28 | AC | 187 ms
31,744 KB |
testcase_29 | AC | 188 ms
31,872 KB |
testcase_30 | AC | 191 ms
31,744 KB |
testcase_31 | AC | 195 ms
31,744 KB |
testcase_32 | AC | 88 ms
36,728 KB |
testcase_33 | AC | 88 ms
36,628 KB |
testcase_34 | AC | 86 ms
36,724 KB |
testcase_35 | AC | 88 ms
36,660 KB |
testcase_36 | AC | 87 ms
36,748 KB |
testcase_37 | AC | 37 ms
28,800 KB |
testcase_38 | AC | 207 ms
30,976 KB |
testcase_39 | AC | 230 ms
36,480 KB |
testcase_40 | AC | 23 ms
26,496 KB |
ソースコード
#pragma GCC optimize("O3") //#pragma GCC target("avx2") //#pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ld> vld; typedef pair<ll,ll> Pll; typedef pair<int,int> Pin; ll INF = 1e16; int inf = 1e9; #define ALL(x) (x).begin(), (x).end() #define FOR(i, m, n) for (ll i = (m); i < (n); ++i) #define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i) #define REP(i, n) FOR(i, 0, n) #define REVREP(i, n) REVFOR(i, 0, n) #define fi first #define se second #define pb push_back #define mp make_pair #define eb emplace_back #define bcnt __builtin_popcountll #ifdef LOCAL #include <prettyprint.hpp> #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__); #else #define debug(...) 83; #endif void d_err() { cerr << endl; } template <typename H, typename... T> void d_err(H h, T... t) { cerr << h << " "; d_err(t...); } template <typename T> void print(T x) { cout << x << "\n"; } template <typename T> void print(vector<T>& x) { int N = x.size(); REP(i, N) { if (i > 0) cout << " "; cout << x[i]; } cout << "\n"; } // Dinic template<typename T> struct Dinic { struct Graph { int to, rev; T cap; Graph(int t, int r, const T &c) : to(t), rev(r), cap(c) {}; }; vector<vector<Graph>> g; vector<int> level; vector<int> iter; int sz; Dinic(int N) : sz(N) { g.resize(N); iter.resize(N); level.resize(N); } void add_edge(int from, int to, T cap) { g[from].eb(to, (int) g[to].size(), cap); g[to].eb(from, (int) g[from].size() - 1, 0); } T operator()(int start, int end) { T flow = 0; while(true) { bfs(start); if (level[end] < 0) break; iter.assign(sz, 0); T f; while(f = dfs(start, end, inf) > 0) { flow += f; } } return flow; } private: void bfs(int s) { level.assign(sz, -1); level[s] = 0; queue<int> q; q.push(s); while(!q.empty()) { int v = q.front(); q.pop(); for(auto &e: g[v]) { int t = e.to; if (e.cap > 0 && level[t] < 0) { level[t] = level[v] + 1; q.push(t); } } } } T dfs(int v, int t, T flow) { if (v == t || flow == 0) return flow; for (int &i = iter[v]; i < g[v].size(); ++i) { auto &e = g[v][i]; if(e.cap <= 0 || level[v] >= level[e.to]) continue; if (T f = dfs(e.to, t, min(flow, e.cap)) > 0) { e.cap -= f; g[e.to][e.rev].cap += f; return f; } } return 0; } }; int main(){ cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); int h, w; cin >> h >> w; vector<vll> a(h, vll(w)); REP(i, h) REP(j, w) cin >> a[i][j]; vector<vector<Pin>> v(1e6); REP(i, h) REP(j, w) { if (a[i][j] == 0) continue; v[a[i][j]].eb(i, j); } ll ans = 0; for (auto &e: v) { if (e.empty()) continue; map<int, int> mi, mj; for(auto &e2: e) { if (mi.find(e2.fi) == mi.end()) mi[e2.fi] = mi.size(); if (mj.find(e2.se) == mj.end()) mj[e2.se] = mj.size(); } Dinic<ll> g(mi.size() + mj.size() + 2); for (auto &e2: e) { g.add_edge(mi[e2.fi], mi.size() + mj[e2.se], 1); } for (auto &p: mi) { g.add_edge(mi.size() + mj.size(), p.se, 1); } for (auto &p: mj) { g.add_edge(mi.size() + p.se, mi.size() + mj.size() + 1, 1); } ans += g(mi.size() + mj.size(), mi.size() + mj.size() + 1); } print(ans); }