結果

問題 No.1479 Matrix Eraser
ユーザー simansiman
提出日時 2022-03-16 11:48:44
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 336 ms / 3,000 ms
コード長 3,210 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 137,608 KB
実行使用メモリ 25,284 KB
最終ジャッジ日時 2023-10-24 16:31:12
合計ジャッジ時間 8,787 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 39 ms
5,704 KB
testcase_08 AC 68 ms
7,440 KB
testcase_09 AC 143 ms
12,028 KB
testcase_10 AC 261 ms
17,572 KB
testcase_11 AC 165 ms
13,348 KB
testcase_12 AC 49 ms
6,232 KB
testcase_13 AC 68 ms
7,440 KB
testcase_14 AC 51 ms
6,496 KB
testcase_15 AC 13 ms
4,348 KB
testcase_16 AC 59 ms
7,288 KB
testcase_17 AC 322 ms
21,328 KB
testcase_18 AC 321 ms
21,328 KB
testcase_19 AC 324 ms
21,328 KB
testcase_20 AC 321 ms
21,328 KB
testcase_21 AC 322 ms
21,328 KB
testcase_22 AC 324 ms
21,328 KB
testcase_23 AC 322 ms
21,328 KB
testcase_24 AC 322 ms
21,328 KB
testcase_25 AC 336 ms
21,328 KB
testcase_26 AC 325 ms
21,328 KB
testcase_27 AC 160 ms
4,864 KB
testcase_28 AC 159 ms
5,160 KB
testcase_29 AC 160 ms
5,076 KB
testcase_30 AC 160 ms
5,092 KB
testcase_31 AC 162 ms
4,884 KB
testcase_32 AC 59 ms
8,628 KB
testcase_33 AC 59 ms
8,640 KB
testcase_34 AC 60 ms
8,628 KB
testcase_35 AC 60 ms
8,628 KB
testcase_36 AC 60 ms
8,628 KB
testcase_37 AC 40 ms
4,348 KB
testcase_38 AC 156 ms
4,580 KB
testcase_39 AC 305 ms
25,284 KB
testcase_40 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <unordered_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 = 1010;

class MaxFlow {
  public:
    int V;
    vector<int> level;
    vector<int> iter;
    vector <vector<Edge>> G;

    MaxFlow(int V) {
      this->V = V;
      level.resize(V);
      iter.resize(V);
      G.resize(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) {
      level = vector<int>(V, -1);
      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;
        iter = vector<int>(V, 0);
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
          flow += f;
        }
      }
    }
};

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

  unordered_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 ans = 0;

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

    int row_num = 0;
    int col_num = 0;
    unordered_map<int, int> hor;
    unordered_map<int, int> ver;

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

      if (not hor.count(y)) {
        hor[y] = row_num;
        row_num++;
      }
      if (not ver.count(x)) {
        ver[x] = col_num;
        col_num++;
      }
    }

    int s = row_num + col_num;
    int t = row_num + col_num + 1;
    MaxFlow mf(col_num + row_num + 2);
    for (auto [y, idx] : hor) {
      mf.add_edge(s, idx, 1);
    }
    for (auto [x, idx] : ver) {
      mf.add_edge(row_num + idx, t, 1);
    }
    for (int z : positions) {
      int y = z / W;
      int x = z % W;
      mf.add_edge(hor[y], row_num + ver[x], 1);
    }
    ans += mf.max_flow(s, t);
  }

  cout << ans << endl;

  return 0;
}

0