結果

問題 No.1479 Matrix Eraser
ユーザー 👑 kmjpkmjp
提出日時 2021-04-16 23:23:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 3,000 ms
コード長 2,432 bytes
コンパイル時間 2,569 ms
コンパイル使用メモリ 219,100 KB
実行使用メモリ 24,076 KB
最終ジャッジ日時 2023-09-16 02:59:19
合計ジャッジ時間 8,822 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
15,644 KB
testcase_01 AC 8 ms
15,876 KB
testcase_02 AC 8 ms
15,680 KB
testcase_03 AC 8 ms
15,656 KB
testcase_04 AC 7 ms
15,688 KB
testcase_05 AC 8 ms
15,648 KB
testcase_06 AC 8 ms
15,800 KB
testcase_07 AC 27 ms
16,968 KB
testcase_08 AC 42 ms
17,504 KB
testcase_09 AC 84 ms
19,000 KB
testcase_10 AC 148 ms
21,472 KB
testcase_11 AC 96 ms
19,432 KB
testcase_12 AC 33 ms
16,780 KB
testcase_13 AC 40 ms
17,264 KB
testcase_14 AC 33 ms
16,816 KB
testcase_15 AC 13 ms
15,916 KB
testcase_16 AC 35 ms
16,968 KB
testcase_17 AC 176 ms
22,732 KB
testcase_18 AC 177 ms
22,540 KB
testcase_19 AC 180 ms
22,620 KB
testcase_20 AC 183 ms
22,520 KB
testcase_21 AC 183 ms
22,520 KB
testcase_22 AC 178 ms
22,552 KB
testcase_23 AC 181 ms
22,500 KB
testcase_24 AC 174 ms
22,484 KB
testcase_25 AC 174 ms
22,488 KB
testcase_26 AC 173 ms
22,544 KB
testcase_27 AC 145 ms
19,024 KB
testcase_28 AC 145 ms
19,484 KB
testcase_29 AC 147 ms
19,076 KB
testcase_30 AC 144 ms
19,464 KB
testcase_31 AC 149 ms
19,024 KB
testcase_32 AC 70 ms
23,804 KB
testcase_33 AC 70 ms
23,912 KB
testcase_34 AC 67 ms
23,572 KB
testcase_35 AC 67 ms
23,616 KB
testcase_36 AC 67 ms
23,640 KB
testcase_37 AC 23 ms
18,088 KB
testcase_38 AC 118 ms
18,400 KB
testcase_39 AC 165 ms
24,076 KB
testcase_40 AC 8 ms
15,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int H,W;
int A[505][505];

template<class V> class MaxFlow_dinic {
public:
	struct edge { int to,reve;V cap;};
	static const int MV = 1100;
	vector<edge> E[MV];
	int itr[MV],lev[MV];
	void add_edge(int x,int y,V cap,bool undir=false) {
		E[x].push_back((edge){y,(int)E[y].size(),cap});
		E[y].push_back((edge){x,(int)E[x].size()-1,undir?cap:0});
	}
	void bfs(int cur) {
		MINUS(lev);
		queue<int> q;
		lev[cur]=0;
		q.push(cur);
		while(q.size()) {
			int v=q.front(); q.pop();
			FORR(e,E[v]) if(e.cap>0 && lev[e.to]<0) lev[e.to]=lev[v]+1, q.push(e.to);
		}
	}
	V dfs(int from,int to,V cf) {
		if(from==to) return cf;
		for(;itr[from]<E[from].size();itr[from]++) {
			edge* e=&E[from][itr[from]];
			if(e->cap>0 && lev[from]<lev[e->to]) {
				V f=dfs(e->to,to,min(cf,e->cap));
				if(f>0) {
					e->cap-=f;
					E[e->to][e->reve].cap += f;
					return f;
				}
			}
		}
		return 0;
	}
	V maxflow(int from, int to) {
		V fl=0,tf;
		while(1) {
			bfs(from);
			if(lev[to]<0) return fl;
			ZERO(itr);
			while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl+=tf;
		}
	}
};
MaxFlow_dinic<int> mf;
vector<pair<int,int>> E[505050];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W;
	FOR(y,H) FOR(x,W) {
		cin>>A[y][x];
		E[A[y][x]].push_back({y,x});
	}
	
	int ret=0;
	FOR(i,505040) if(i&&E[i].size()) {
		map<int,int> R,C;
		FORR2(r,c,E[i]) R[r]=C[c]=0;
		x=y=0;
		FORR(r,R) r.second=x++;
		FORR(r,C) r.second=y++;
		FOR(j,x+y+2) mf.E[j].clear();
		FOR(j,x) mf.add_edge(0,1+j,1);
		FOR(j,y) mf.add_edge(1+x+j,1+x+y,1);
		FORR2(r,c,E[i]) mf.add_edge(1+R[r],1+x+C[c],1);
		ret+=mf.maxflow(0,1+x+y);
	}
	
	cout<<ret<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0