結果

問題 No.1479 Matrix Eraser
ユーザー SSRSSSRS
提出日時 2021-04-16 20:20:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,622 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 194,788 KB
実行使用メモリ 26,292 KB
最終ジャッジ日時 2023-09-15 20:48:37
合計ジャッジ時間 35,047 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 52 ms
6,540 KB
testcase_08 AC 95 ms
8,796 KB
testcase_09 AC 229 ms
14,404 KB
testcase_10 AC 470 ms
22,780 KB
testcase_11 AC 271 ms
16,160 KB
testcase_12 AC 66 ms
7,464 KB
testcase_13 AC 94 ms
9,000 KB
testcase_14 AC 71 ms
7,672 KB
testcase_15 AC 15 ms
4,452 KB
testcase_16 AC 80 ms
8,224 KB
testcase_17 AC 581 ms
25,984 KB
testcase_18 AC 583 ms
26,196 KB
testcase_19 AC 603 ms
26,100 KB
testcase_20 AC 571 ms
26,032 KB
testcase_21 AC 556 ms
25,940 KB
testcase_22 AC 539 ms
25,980 KB
testcase_23 AC 542 ms
25,996 KB
testcase_24 AC 539 ms
26,236 KB
testcase_25 AC 537 ms
26,016 KB
testcase_26 AC 534 ms
26,292 KB
testcase_27 AC 2,709 ms
7,284 KB
testcase_28 AC 2,704 ms
7,220 KB
testcase_29 AC 2,705 ms
7,308 KB
testcase_30 AC 2,702 ms
7,344 KB
testcase_31 AC 2,702 ms
7,388 KB
testcase_32 AC 1,114 ms
13,460 KB
testcase_33 AC 1,112 ms
13,248 KB
testcase_34 AC 1,122 ms
13,492 KB
testcase_35 AC 1,110 ms
13,520 KB
testcase_36 AC 1,110 ms
13,824 KB
testcase_37 AC 36 ms
4,380 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000;
template <typename Cap>
struct ford_fulkerson{
	struct edge{
		int to, rev;
		Cap cap;
		edge(int to, int rev, Cap cap): to(to), rev(rev), cap(cap){
		}
	};
	int N;
	vector<vector<edge>> G;
	ford_fulkerson(){
	}
	ford_fulkerson(int N): N(N), G(N){
	}
	void add_edge(int from, int to, Cap cap){
		int id1 = G[from].size();
		int id2 = G[to].size();
		G[from].push_back(edge(to, id2, cap));
		G[to].push_back(edge(from, id1, 0));
	}
	Cap max_flow(int s, int t){
		Cap flow = 0;
		while (1){
			vector<Cap> m(N, INF);
			vector<int> pv(N, -1);
			vector<int> pe(N, -1);
			vector<bool> used(N, false);
			queue<int> Q;
			Q.push(s);
			used[s] = true;
			while (!Q.empty()){
				int v = Q.front();
				Q.pop();
				int cnt = G[v].size();
				for (int i = 0; i < cnt; i++){
					int w = G[v][i].to;
					if (!used[w] && G[v][i].cap > 0){
						used[w] = true;
						m[w] = min(m[v], G[v][i].cap);
						pv[w] = v;
						pe[w] = i;
						Q.push(w);
					}
				}
			}
			if (!used[t]){
				break;
			}
			Cap f = m[t];
			for (int i = t; i != s; i = pv[i]){
				G[pv[i]][pe[i]].cap -= f;
				G[i][G[pv[i]][pe[i]].rev].cap += f;
			}
			flow += f;
		}
		return flow;
	}
};
int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> A[i][j];
    }
  }
  map<int, vector<pair<int, int>>> mp;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (A[i][j] > 0){
        mp[A[i][j]].push_back(make_pair(i, j));
      }
    }
  }
  int ans = 0;
  for (auto P : mp){
    int cnt = P.second.size();
    vector<int> X, Y;
    for (int i = 0; i < cnt; i++){
      X.push_back(P.second[i].first);
      Y.push_back(P.second[i].second);
    }
    sort(X.begin(), X.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    sort(Y.begin(), Y.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());
    int Xcnt = X.size(), Ycnt = Y.size();
    vector<int> x(cnt), y(cnt);
    for (int i = 0; i < cnt; i++){
      x[i] = lower_bound(X.begin(), X.end(), P.second[i].first) - X.begin();
      y[i] = lower_bound(Y.begin(), Y.end(), P.second[i].second) - Y.begin();
    }
    ford_fulkerson<int> G(Xcnt + Ycnt + 2);
    for (int i = 0; i < Xcnt; i++){
      G.add_edge(Xcnt + Ycnt, i, 1);
    }
    for (int i = 0; i < Ycnt; i++){
      G.add_edge(Xcnt + i, Xcnt + Ycnt + 1, 1);
    }
    for (int i = 0; i < cnt; i++){
      G.add_edge(x[i], Xcnt + y[i], 1);
    }
    ans += G.max_flow(Xcnt + Ycnt, Xcnt + Ycnt + 1);
  }
  cout << ans << endl;
}
0