結果
問題 | No.1479 Matrix Eraser |
ユーザー |
![]() |
提出日時 | 2021-04-16 21:36:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,368 ms / 3,000 ms |
コード長 | 2,014 bytes |
コンパイル時間 | 7,872 ms |
コンパイル使用メモリ | 255,788 KB |
最終ジャッジ日時 | 2025-01-20 19:37:16 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 39 |
ソースコード
#include<bits/stdc++.h>#include<atcoder/all>using namespace std;using namespace atcoder;#define rep(i,n)for (int i = 0; i < int(n); ++i)#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)#define all(x) (x).begin(), (x).end()#define rall(x) (x).rbegin(), (x).rend()template<class T> void chmax(T& a, const T& b) {a = max(a, b);}template<class T> void chmin(T& a, const T& b) {a = min(a, b);}using ll = long long;using P = pair<int,int>;using VI = vector<int>;using VVI = vector<VI>;using VL = vector<ll>;using VVL = vector<VL>;// https://ei1333.github.io/algorithm/bipartite-matching.htmlstruct Bipartite_Matching{vector< vector< int > > graph;vector< int > match, alive, used;int timestamp;Bipartite_Matching(int n){timestamp = 0;graph.resize(n);alive.assign(n, 1);used.assign(n, 0);match.assign(n, -1);}void add_edge(int u, int v){graph[u].push_back(v);graph[v].push_back(u);}bool dfs(int v){used[v] = timestamp;for(int i = 0; i < graph[v].size(); i++) {int u = graph[v][i], w = match[u];if(alive[u] == 0) continue;if(w == -1 || (used[w] != timestamp && dfs(w))) {match[v] = u;match[u] = v;return (true);}}return (false);}int bipartite_matching(){int ret = 0;for(int i = 0; i < graph.size(); i++) {if(alive[i] == 0) continue;if(match[i] == -1) {++timestamp;ret += dfs(i);}}return (ret);}};int main() {ios::sync_with_stdio(false);cin.tie(0);int h, w;cin >> h >> w;vector<vector<P>> d(500001);rep(i, h) rep(j, w) {int a;cin >> a;if (a) d[a].emplace_back(i, j);}int ans = 0;for(auto& ps: d) if (!ps.empty()) {Bipartite_Matching bm(h + w);for(auto [i, j]: ps) {bm.add_edge(i, h + j);}ans += bm.bipartite_matching();}cout << ans << '\n';}