結果
| 問題 |
No.1479 Matrix Eraser
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-18 15:31:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,274 ms / 3,000 ms |
| コード長 | 3,629 bytes |
| コンパイル時間 | 3,359 ms |
| コンパイル使用メモリ | 154,216 KB |
| 最終ジャッジ日時 | 2025-01-20 21:44:19 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct HopcroftKarp {
vector<vector<int>> graph;
vector<int> dist, match;
vector<bool> used, visited;
HopcroftKarp(int n, int m) : graph(n), match(m, -1), used(n) {}
void add_edge(int u, int v){
graph[u].emplace_back(v);
}
void bfs(){
dist.assign(graph.size(), -1);
queue<int> que;
for(int i = 0; i < (int)graph.size(); ++i){
if(!used[i]){
que.emplace(i);
dist[i] = 0;
}
}
while(!que.empty()){
int from = que.front();
que.pop();
for(auto& to : graph[from]){
int v = match[to];
if(v >= 0 && dist[v] == -1){
dist[v] = dist[from] + 1;
que.emplace(v);
}
}
}
}
bool dfs(int s){
visited[s] = true;
for(auto& u : graph[s]){
int v = match[u];
if(v < 0 || (!visited[v] && dist[v] == dist[s] + 1 && dfs(v))){
match[u] = s;
used[s] = true;
return true;
}
}
return false;
}
int bipartite_matching(){
int ret = 0;
while(true){
bfs();
visited.assign(graph.size(), false);
int flow = 0;
for(int i = 0; i < (int)graph.size(); ++i){
if(!used[i] && dfs(i)) ++flow;
}
if(flow == 0) return ret;
ret += flow;
}
}
void output(){
for(int i = 0; i < (int)match.size(); ++i){
if(~match[i]){
cout << match[i] << '-' << i << '\n';
}
}
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector A(H, vector<int>(W));
vector<int> vals;
for(int i = 0; i < H; ++i){
for(int j = 0; j < W; ++j){
cin >> A[i][j];
if(A[i][j]) vals.emplace_back(A[i][j]);
}
}
sort(begin(vals), end(vals));
vals.erase(unique(begin(vals), end(vals)), end(vals));
int sz = vals.size();
vector<vector<pair<int, int>>> ps(sz);
for(int i = 0; i < H; ++i){
for(int j = 0; j < W; ++j){
if(!A[i][j]) continue;
int k = lower_bound(begin(vals), end(vals), A[i][j]) - begin(vals);
ps[k].emplace_back(i, j);
}
}
int ans = 0;
for(int i = 0; i < sz; ++i){
if(ps[i].size() <= 1) { ans += 1; continue; }
HopcroftKarp g(H, W);
for(auto& [x, y] : ps[i]) g.add_edge(x, y);
ans += g.bipartite_matching();
}
cout << ans << endl;
return 0;
}