結果
| 問題 | No.1479 Matrix Eraser |
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-03-16 11:48:44 |
| 言語 | C++17(clang) (17.0.6 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 377 ms / 3,000 ms |
| コード長 | 3,210 bytes |
| 記録 | |
| コンパイル時間 | 1,630 ms |
| コンパイル使用メモリ | 148,824 KB |
| 実行使用メモリ | 25,464 KB |
| 最終ジャッジ日時 | 2024-09-24 07:48:02 |
| 合計ジャッジ時間 | 9,570 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 39 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = INT_MAX;
struct Edge {
int to;
int cap;
int rev;
Edge(int to = -1, int cap = -1, int rev = -1) {
this->to = to;
this->cap = cap;
this->rev = rev;
}
};
const int MAX_N = 100;
const int MAX_V = 1010;
class MaxFlow {
public:
int V;
vector<int> level;
vector<int> iter;
vector <vector<Edge>> G;
MaxFlow(int V) {
this->V = V;
level.resize(V);
iter.resize(V);
G.resize(V);
}
void add_edge(int from, int to, int cap) {
G[from].push_back(Edge(to, cap, G[to].size()));
G[to].push_back(Edge(from, 0, G[from].size() - 1));
}
void bfs(int s) {
level = vector<int>(V, -1);
queue<int> que;
level[s] = 0;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < (int) G[v].size(); ++i) {
Edge *e = &G[v][i];
if (e->cap > 0 && level[e->to] < 0) {
level[e->to] = level[v] + 1;
que.push(e->to);
}
}
}
}
int dfs(int v, int t, int f) {
if (v == t) return f;
for (int &i = iter[v]; i < (int) G[v].size(); ++i) {
Edge *e = &G[v][i];
if (e->cap > 0 && level[v] < level[e->to]) {
int d = dfs(e->to, t, min(f, e->cap));
if (d > 0) {
e->cap -= d;
G[e->to][e->rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t) {
int flow = 0;
for (;;) {
bfs(s);
if (level[t] < 0) return flow;
iter = vector<int>(V, 0);
int f;
while ((f = dfs(s, t, INF)) > 0) {
flow += f;
}
}
}
};
int main() {
int H, W;
cin >> H >> W;
unordered_map<int, vector<int>> S;
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
int a;
cin >> a;
int z = y * W + x;
S[a].push_back(z);
}
}
int ans = 0;
for (auto &[a, positions] : S) {
bool checked[H + W];
memset(checked, false, sizeof(checked));
if (a == 0) continue;
int row_num = 0;
int col_num = 0;
unordered_map<int, int> hor;
unordered_map<int, int> ver;
for (int z : positions) {
int y = z / W;
int x = z % W;
if (not hor.count(y)) {
hor[y] = row_num;
row_num++;
}
if (not ver.count(x)) {
ver[x] = col_num;
col_num++;
}
}
int s = row_num + col_num;
int t = row_num + col_num + 1;
MaxFlow mf(col_num + row_num + 2);
for (auto [y, idx] : hor) {
mf.add_edge(s, idx, 1);
}
for (auto [x, idx] : ver) {
mf.add_edge(row_num + idx, t, 1);
}
for (int z : positions) {
int y = z / W;
int x = z % W;
mf.add_edge(hor[y], row_num + ver[x], 1);
}
ans += mf.max_flow(s, t);
}
cout << ans << endl;
return 0;
}
siman