結果

問題 No.1479 Matrix Eraser
ユーザー norikamenorikame
提出日時 2021-04-17 11:14:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 511 ms / 3,000 ms
コード長 1,319 bytes
コンパイル時間 4,593 ms
コンパイル使用メモリ 278,620 KB
実行使用メモリ 30,896 KB
最終ジャッジ日時 2023-09-16 18:18:45
合計ジャッジ時間 14,917 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 49 ms
6,524 KB
testcase_08 AC 90 ms
8,936 KB
testcase_09 AC 214 ms
14,084 KB
testcase_10 AC 401 ms
21,948 KB
testcase_11 AC 247 ms
15,756 KB
testcase_12 AC 64 ms
7,512 KB
testcase_13 AC 90 ms
8,744 KB
testcase_14 AC 66 ms
7,564 KB
testcase_15 AC 14 ms
4,652 KB
testcase_16 AC 75 ms
8,296 KB
testcase_17 AC 508 ms
25,040 KB
testcase_18 AC 492 ms
25,072 KB
testcase_19 AC 502 ms
25,064 KB
testcase_20 AC 511 ms
25,180 KB
testcase_21 AC 511 ms
25,060 KB
testcase_22 AC 506 ms
25,108 KB
testcase_23 AC 500 ms
25,064 KB
testcase_24 AC 502 ms
25,288 KB
testcase_25 AC 496 ms
25,044 KB
testcase_26 AC 500 ms
25,076 KB
testcase_27 AC 183 ms
6,332 KB
testcase_28 AC 183 ms
6,432 KB
testcase_29 AC 184 ms
6,304 KB
testcase_30 AC 184 ms
6,256 KB
testcase_31 AC 184 ms
6,352 KB
testcase_32 AC 89 ms
11,500 KB
testcase_33 AC 90 ms
11,472 KB
testcase_34 AC 88 ms
11,820 KB
testcase_35 AC 89 ms
11,472 KB
testcase_36 AC 91 ms
11,484 KB
testcase_37 AC 35 ms
4,380 KB
testcase_38 AC 224 ms
5,928 KB
testcase_39 AC 382 ms
30,896 KB
testcase_40 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 解説を見て実装

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

int main() {
	int h, w;
	cin >> h >> w;
	map<int, vector<pair<int, int>>> aval;
	rep(i, h) rep(j, w) {
		int ai;
		cin >> ai;
		if (ai == 0) continue;
		aval[ai].emplace_back(i, j);
	}
	int res = 0;
	for (auto p : aval) {
		map<int, int> hid, wid;
		rep(i, (int)(p.second.size())) {
			hid[p.second[i].first] = -1;
			wid[p.second[i].second] = -1;
		}
		auto hitr=hid.begin(), witr=wid.begin();
		for (int j1=0; hitr!=hid.end(); ++hitr,++j1) hitr->second = j1;
		for (int j2=0; witr!=wid.end(); ++witr,++j2) witr->second = j2;
		int n = (int)(hid.size()) + (int)(wid.size());
		mf_graph<int> g(n+2);
		for (auto p2 : hid) g.add_edge(n, p2.second, 1);
		for (auto p2 : wid) g.add_edge((int)(hid.size())+p2.second, n+1, 1);
		rep(i, (int)(p.second.size())) g.add_edge(hid[p.second[i].first], (int)(hid.size())+wid[p.second[i].second], 1);
		res += g.flow(n, n+1);
	}
	cout << res << endl;
	return 0;
}

0