結果

問題 No.1479 Matrix Eraser
ユーザー chocoruskchocorusk
提出日時 2021-04-16 20:24:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,533 bytes
コンパイル時間 4,180 ms
コンパイル使用メモリ 200,088 KB
実行使用メモリ 35,956 KB
最終ジャッジ日時 2023-09-15 21:03:15
合計ジャッジ時間 8,888 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
35,128 KB
testcase_01 AC 18 ms
30,796 KB
testcase_02 AC 16 ms
30,716 KB
testcase_03 AC 17 ms
30,844 KB
testcase_04 AC 17 ms
30,744 KB
testcase_05 AC 19 ms
30,800 KB
testcase_06 AC 17 ms
30,740 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0