結果

問題 No.1479 Matrix Eraser
ユーザー chocoruskchocorusk
提出日時 2021-04-16 21:18:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,435 bytes
コンパイル時間 3,784 ms
コンパイル使用メモリ 199,760 KB
実行使用メモリ 74,204 KB
最終ジャッジ日時 2023-09-15 23:16:22
合計ジャッジ時間 14,299 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
30,840 KB
testcase_01 AC 12 ms
30,800 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 21 ms
32,580 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 75 ms
45,928 KB
testcase_33 AC 77 ms
45,740 KB
testcase_34 AC 77 ms
45,944 KB
testcase_35 AC 76 ms
45,876 KB
testcase_36 AC 80 ms
45,784 KB
testcase_37 AC 46 ms
31,040 KB
testcase_38 AC 246 ms
68,584 KB
testcase_39 AC 343 ms
74,204 KB
testcase_40 AC 13 ms
30,924 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    set<int> sx[505], sy[505];
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            int a; cin>>a;
            if(a==0) continue;
            v[a].push_back({i, j});
            sx[i].insert(a);
            sy[j].insert(a);
        }
    }
    int s=h+w, t=s+1;
    for(int i=0; i<h; i++) add_edge(s, i, sx[i].size());
    for(int i=0; i<w; i++) add_edge(i+h, t, sy[i].size());
    int ans=0;
    for(int i=1; i<=500000; i++){
        if(v[i].empty()) continue;
        for(auto p:v[i]){
            add_edge(p.first, p.second+h, 1);
        }
    }
    ans=max_flow(s, t);
    cout<<ans<<endl;
    return 0;
}
0