結果
問題 | No.1479 Matrix Eraser |
ユーザー | Ahykw |
提出日時 | 2021-04-16 22:44:46 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 243 ms / 3,000 ms |
コード長 | 4,047 bytes |
コンパイル時間 | 3,394 ms |
コンパイル使用メモリ | 245,488 KB |
実行使用メモリ | 36,752 KB |
最終ジャッジ日時 | 2024-07-03 02:59:58 |
合計ジャッジ時間 | 10,177 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 14 ms
26,656 KB |
testcase_01 | AC | 14 ms
26,624 KB |
testcase_02 | AC | 14 ms
26,520 KB |
testcase_03 | AC | 15 ms
26,624 KB |
testcase_04 | AC | 14 ms
26,624 KB |
testcase_05 | AC | 13 ms
26,740 KB |
testcase_06 | AC | 13 ms
26,624 KB |
testcase_07 | AC | 40 ms
27,844 KB |
testcase_08 | AC | 58 ms
28,440 KB |
testcase_09 | AC | 111 ms
30,592 KB |
testcase_10 | AC | 199 ms
33,620 KB |
testcase_11 | AC | 127 ms
31,232 KB |
testcase_12 | AC | 47 ms
28,096 KB |
testcase_13 | AC | 60 ms
28,416 KB |
testcase_14 | AC | 50 ms
28,032 KB |
testcase_15 | AC | 21 ms
27,108 KB |
testcase_16 | AC | 54 ms
28,280 KB |
testcase_17 | AC | 243 ms
34,972 KB |
testcase_18 | AC | 234 ms
34,832 KB |
testcase_19 | AC | 238 ms
34,772 KB |
testcase_20 | AC | 237 ms
34,916 KB |
testcase_21 | AC | 238 ms
34,936 KB |
testcase_22 | AC | 233 ms
34,944 KB |
testcase_23 | AC | 239 ms
34,940 KB |
testcase_24 | AC | 235 ms
34,920 KB |
testcase_25 | AC | 239 ms
34,908 KB |
testcase_26 | AC | 230 ms
34,872 KB |
testcase_27 | AC | 185 ms
31,872 KB |
testcase_28 | AC | 183 ms
31,616 KB |
testcase_29 | AC | 188 ms
31,740 KB |
testcase_30 | AC | 186 ms
31,744 KB |
testcase_31 | AC | 187 ms
31,604 KB |
testcase_32 | AC | 87 ms
36,600 KB |
testcase_33 | AC | 83 ms
36,632 KB |
testcase_34 | AC | 80 ms
36,660 KB |
testcase_35 | AC | 82 ms
36,532 KB |
testcase_36 | AC | 82 ms
36,752 KB |
testcase_37 | AC | 28 ms
28,672 KB |
testcase_38 | AC | 198 ms
30,848 KB |
testcase_39 | AC | 222 ms
36,428 KB |
testcase_40 | AC | 14 ms
26,712 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; } }; template<typename T> T bipartite_matching(const vector<Pin>& edge) { map<ll, ll> m1, m2; for(auto [s, t]: edge) { if (m1.find(s) == m1.end()) m1[s] = m1.size(); if (m2.find(t) == m2.end()) m2[t] = m2.size(); } ll X = m1.size(), Y = m2.size(); ll start = X + Y, goal = X + Y + 1; Dinic<T> g(X + Y + 2); for (auto [s, t]: edge) { g.add_edge(m1[s], X + m2[t], 1); } for (auto [_, s]: m1) { g.add_edge(start, s, 1); } for (auto [_, t]: m2) { g.add_edge(X + t, goal, 1); } return g(start, goal); } 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; ans += bipartite_matching<ll>(e); } print(ans); }