結果

問題 No.1479 Matrix Eraser
ユーザー chocoruskchocorusk
提出日時 2021-04-16 20:28:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,594 bytes
コンパイル時間 4,046 ms
コンパイル使用メモリ 202,716 KB
実行使用メモリ 35,816 KB
最終ジャッジ日時 2023-09-15 21:17:09
合計ジャッジ時間 9,333 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
30,808 KB
testcase_01 AC 20 ms
30,788 KB
testcase_02 AC 16 ms
30,788 KB
testcase_03 AC 17 ms
30,720 KB
testcase_04 AC 17 ms
30,904 KB
testcase_05 AC 17 ms
30,740 KB
testcase_06 AC 17 ms
30,708 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 ans=0;
    for(int i=1; i<=500000; i++){
        if(v[i].empty()) continue;
        map<int, int> mp;
        for(auto p:v[i]){
            mp[p.first]=0;
            mp[p.second+h]=0;
        }
        int s=mp.size(), t=s+1;
        int c=0;
        for(auto &p:mp){
            p.second=c++;
        }
        for(auto p:mp){
            if(p.first<h) add_edge(s, p.second, 1);
            else add_edge(p.second, t, 1);
        }
        for(auto p:v[i]){
            add_edge(mp[p.first], mp[p.second+h], 1);
        }
        ans+=max_flow(s, t);
        for(int j=0; j<=t; j++) G[j].clear();
    }
    cout<<ans<<endl;
    return 0;
}
0