結果
| 問題 |
No.1479 Matrix Eraser
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2021-04-16 20:24:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,533 bytes |
| コンパイル時間 | 6,203 ms |
| コンパイル使用メモリ | 191,944 KB |
| 最終ジャッジ日時 | 2025-01-20 18:41:25 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 19 TLE * 20 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
//最大二部マッチング復元 https://judge.yosupo.jp/submission/1148
const ll INF=1e18;
struct edge {int to; ll cap; int rev;} ;
vector<edge> G[500020];
int level[500020];
int iter[500020];
void add_edge(int from, int to, ll cap){
edge e;
e.to=to, e.cap=cap, e.rev=G[to].size();
G[from].push_back(e);
e.to=from, e.cap=0, e.rev=G[from].size()-1;
G[to].push_back(e);
}
void bfs(int s){
memset(level, -1, sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();
que.pop();
for(int i=0; i<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);
}
}
}
}
ll dfs(int v, int t, ll f){
if(v==t) return f;
for(int &i=iter[v]; i<G[v].size(); i++){
edge &e=G[v][i];
if(e.cap>0 && level[v]<level[e.to]){
ll 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;
}
ll max_flow(int s, int t){
ll flow=0;
while(1){
bfs(s);
if(level[t]<0) return flow;
memset(iter, 0, sizeof(iter));
ll f;
while((f=dfs(s, t, INF))>0){
flow+=f;
}
}
}
vector<P> v[500050];
int main()
{
int h, w; cin>>h>>w;
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
int a; cin>>a;
v[a].push_back({i, j});
}
}
int s=h+w, t=s+1;
int ans=0;
for(int i=1; i<=500000; i++){
if(v[i].empty()) continue;
set<int> st;
for(auto p:v[i]){
st.insert(p.first);
st.insert(p.second+h);
}
for(auto x:st){
if(x<h) add_edge(s, x, 1);
else add_edge(x, t, 1);
}
for(auto p:v[i]){
add_edge(p.first, p.second+h, 1);
}
ans+=max_flow(s, t);
for(auto x:st){
G[x].clear();
}
G[s].clear(); G[t].clear();
}
cout<<ans<<endl;
return 0;
}
chocorusk