結果

問題 No.1479 Matrix Eraser
ユーザー rabimearabimea
提出日時 2023-01-26 22:27:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 3,000 ms
コード長 2,472 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 206,448 KB
実行使用メモリ 27,820 KB
最終ジャッジ日時 2023-09-09 20:43:18
合計ジャッジ時間 8,593 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
19,832 KB
testcase_01 AC 9 ms
19,916 KB
testcase_02 AC 8 ms
19,788 KB
testcase_03 AC 8 ms
19,840 KB
testcase_04 AC 9 ms
19,724 KB
testcase_05 AC 9 ms
19,760 KB
testcase_06 AC 9 ms
19,736 KB
testcase_07 AC 16 ms
20,672 KB
testcase_08 AC 23 ms
21,344 KB
testcase_09 AC 45 ms
22,884 KB
testcase_10 AC 101 ms
25,116 KB
testcase_11 AC 55 ms
23,376 KB
testcase_12 AC 18 ms
20,924 KB
testcase_13 AC 22 ms
21,352 KB
testcase_14 AC 19 ms
20,924 KB
testcase_15 AC 11 ms
20,104 KB
testcase_16 AC 21 ms
21,112 KB
testcase_17 AC 128 ms
26,092 KB
testcase_18 AC 131 ms
26,052 KB
testcase_19 AC 129 ms
26,036 KB
testcase_20 AC 134 ms
26,004 KB
testcase_21 AC 131 ms
25,992 KB
testcase_22 AC 134 ms
25,984 KB
testcase_23 AC 132 ms
26,096 KB
testcase_24 AC 135 ms
26,036 KB
testcase_25 AC 130 ms
26,032 KB
testcase_26 AC 128 ms
26,016 KB
testcase_27 AC 77 ms
22,744 KB
testcase_28 AC 77 ms
22,716 KB
testcase_29 AC 78 ms
22,804 KB
testcase_30 AC 79 ms
22,740 KB
testcase_31 AC 79 ms
22,812 KB
testcase_32 AC 39 ms
27,724 KB
testcase_33 AC 40 ms
27,820 KB
testcase_34 AC 39 ms
27,668 KB
testcase_35 AC 39 ms
27,612 KB
testcase_36 AC 38 ms
27,700 KB
testcase_37 AC 26 ms
17,388 KB
testcase_38 AC 40 ms
21,708 KB
testcase_39 AC 60 ms
23,484 KB
testcase_40 AC 7 ms
15,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
using vi = vector <int>;
using ll = long long;
using pii = pair <int, int>;
//~ const ll mod = 998244353;
const ll mod = 1e9 + 7;
ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a;
	for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; }


const int N = 511;
const int mxn = N * 2;
const int mxm = N * N * 4;
using Flow = int;
const int MXL = 1e9;
struct Flownet {
	int n, m;
	int fe[mxn];
	int ne[mxm];
	int ev[mxm];
	Flow f[mxm];
	void clear(int n) {
		this->n = n;
		fill(fe, fe + n + 1, 0);
		m = 1;
	}
	void add_(int x, int y, Flow flow) { // 添加一条边
		ne[++m] = fe[x];
		fe[x] = m;
		ev[m] = y;
		f[m] = flow;
	}
	void add1(int x, int y, Flow flow) { // 添加一条单向边和其反向边
		add_(x, y, flow);
		add_(y, x, 0);
	}
	
	int T;
	int arc[mxn], lev[mxn];
	Flow extend(int x, Flow flow) {
		if (x == T) return flow;
		Flow re = 0;
		for (int &e = arc[x]; e; e = ne[e]) if (f[e] && lev[ev[e]] == lev[x] + 1) {
			Flow t = extend(ev[e], min(flow - re, f[e]));
			if (t) {
				f[e] -= t;
				f[e ^ 1] += t;
				if ((re += t) == flow)
					break;
			}
		}
		if (!re) lev[x] = 0;
		return re;
	}
	int q[mxn], *qb, *qe;
	Flow dinic(int S, int T) {
		this->T = T;
		Flow re = 0;
		for (;;) {
			fill(lev, lev + n + 1, 0);
			copy(fe, fe + n + 1, arc);
			lev[S] = 1;
			qb = qe = q;
			*qe++ = S;
			while (qb != qe) {
				int x = *qb++;
				if (lev[T] && lev[x] + 1 >= lev[T])
					break;
				for (int e = fe[x]; e; e = ne[e]) if (f[e] && !lev[ev[e]]) {
					lev[ev[e]] = lev[x] + 1;
					*qe++ = ev[e];
				}
			}
			if (!lev[T]) break;
			re += extend(S, MXL);
		}
		return re;
	}
}net;

int vx[N], vy[N];

const int M = 5e5 + 11;
vector <pii> v[M];

int main() {
	ios :: sync_with_stdio(false);
	
	int n, m; cin >> n >> m;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++) {
			int x; cin >> x;
			v[x].pb({i, j});
		}
	
	int ans = 0;
	for(int i = 1; i < M; i ++) if(v[i].size()) {
		if(v[i].size() == 1) ans ++;
		else {
			int S = 0, T = n + m + 1;
			net.clear(T);
			
			memset(vx, 0, sizeof vx);
			memset(vy, 0, sizeof vy);
			for(auto [x, y] : v[i]) {
				net.add1(x, y + n, 1);
				vx[x] = 1; vy[y] = 1;
			}
			
			for(int x = 1; x <= n; x ++)
				if(vx[x]) net.add1(S, x, 1);
			for(int y = 1; y <= m; y ++)
				if(vy[y]) net.add1(y + n, T, 1);
			
			ans += net.dinic(S, T);
		}
	}
	
	cout << ans << '\n';
}
0