結果

問題 No.1479 Matrix Eraser
ユーザー simansiman
提出日時 2022-03-16 11:27:25
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,711 bytes
コンパイル時間 2,314 ms
コンパイル使用メモリ 135,492 KB
実行使用メモリ 31,112 KB
最終ジャッジ日時 2023-10-24 16:08:17
合計ジャッジ時間 45,096 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 398 ms
6,884 KB
testcase_08 AC 639 ms
9,036 KB
testcase_09 AC 1,296 ms
14,412 KB
testcase_10 AC 2,281 ms
22,220 KB
testcase_11 AC 1,521 ms
16,108 KB
testcase_12 AC 497 ms
7,720 KB
testcase_13 AC 637 ms
9,064 KB
testcase_14 AC 522 ms
7,900 KB
testcase_15 AC 121 ms
4,712 KB
testcase_16 AC 572 ms
8,388 KB
testcase_17 AC 2,712 ms
25,280 KB
testcase_18 AC 2,611 ms
25,300 KB
testcase_19 AC 2,602 ms
25,308 KB
testcase_20 AC 2,674 ms
25,312 KB
testcase_21 AC 2,594 ms
25,288 KB
testcase_22 AC 2,685 ms
25,280 KB
testcase_23 AC 2,762 ms
25,280 KB
testcase_24 AC 2,665 ms
25,276 KB
testcase_25 AC 2,713 ms
25,304 KB
testcase_26 AC 2,703 ms
25,328 KB
testcase_27 AC 144 ms
5,308 KB
testcase_28 AC 141 ms
5,248 KB
testcase_29 AC 145 ms
5,184 KB
testcase_30 AC 143 ms
5,148 KB
testcase_31 AC 143 ms
5,464 KB
testcase_32 AC 54 ms
8,948 KB
testcase_33 AC 54 ms
8,948 KB
testcase_34 AC 54 ms
8,948 KB
testcase_35 AC 55 ms
8,948 KB
testcase_36 AC 56 ms
8,948 KB
testcase_37 AC 39 ms
4,448 KB
testcase_38 AC 130 ms
4,888 KB
testcase_39 TLE -
testcase_40 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int INF = INT_MAX;

struct Edge {
  int to;
  int cap;
  int rev;

  Edge(int to = -1, int cap = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->rev = rev;
  }
};

const int MAX_N = 100;
const int MAX_V = MAX_N * MAX_N + 10;

class MaxFlow {
  public:
    int level[MAX_V];
    int iter[MAX_V];
    vector <Edge> G[MAX_V];

    void add_edge(int from, int to, int cap) {
      G[from].push_back(Edge(to, cap, G[to].size()));
      G[to].push_back(Edge(from, 0, G[from].size() - 1));
    }

    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 < (int) 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);
          }
        }
      }
    }

    int dfs(int v, int t, int f) {
      if (v == t) return f;

      for (int &i = iter[v]; i < (int) G[v].size(); ++i) {
        Edge *e = &G[v][i];

        if (e->cap > 0 && level[v] < level[e->to]) {
          int 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;
    }

    int max_flow(int s, int t) {
      int flow = 0;

      for (;;) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
          flow += f;
        }
      }
    }
};

int main() {
  int H, W;
  cin >> H >> W;

  map<int, vector<int>> S;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      int a;
      cin >> a;
      int z = y * W + x;
      S[a].push_back(z);
    }
  }

  int s = H + W + 1;
  int t = H + W + 2;
  int ans = 0;

  for (auto &[a, positions] : S) {
    MaxFlow mf;
    bool checked[H + W];
    memset(checked, false, sizeof(checked));
    if (a == 0) continue;

    for (int z : positions) {
      int y = z / W;
      int x = z % W;

      if (not checked[y]) {
        checked[y] = true;
        mf.add_edge(s, y, 1);
      }
      if (not checked[H + x]) {
        checked[H + x] = true;
        mf.add_edge(H + x, t, 1);
      }
      mf.add_edge(y, H + x, 1);
    }

    ans += mf.max_flow(s, t);
  }

  cout << ans << endl;

  return 0;
}

0