結果

問題 No.421 しろくろチョコレート
ユーザー rsk0315rsk0315
提出日時 2019-02-13 01:33:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 2,896 bytes
コンパイル時間 996 ms
コンパイル使用メモリ 96,020 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:57:01
合計ジャッジ時間 2,486 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 5 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 1 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 6 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 1 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 6 ms
4,348 KB
testcase_61 AC 3 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cassert>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
#include <queue>

constexpr size_t di[] = {size_t(-1), 0, 1, 0};
constexpr size_t dj[] = {0, size_t(-1), 0, 1};

template <class Weight>
struct Edge {
  size_t src, dst;
  Weight cost;

  Edge(size_t src, size_t dst, Weight cost=1):
    src(src), dst(dst), cost(cost)
  {}

  bool operator <(const Edge<Weight> &rhs) const {
    if (cost != rhs.cost) return cost < rhs.cost;
    if (src != rhs.src) return src < rhs.src;
    return dst < rhs.dst;
  }
};

template <class Weight>
struct Graph: public std::vector<std::vector<Edge<Weight>>> {
  Graph(size_t n): std::vector<std::vector<Edge<Weight>>>(n) {}

  void connect_with(size_t src, size_t dst, Weight cost=1) {
    (*this)[src].emplace_back(src, dst, cost);
    (*this)[dst].emplace_back(dst, src, cost);
  }

  void connect_to(size_t src, size_t dst, Weight cost=1) {
    (*this)[src].emplace_back(src, dst, cost);
  }
};

template <class Weight>
size_t bipartite_match(const Graph<Weight> &g, size_t mid) { 
  std::vector<size_t> pair(g.size(), -1);
  std::function<bool (size_t, std::vector<bool> &)> augment;
  augment = [&](size_t u, std::vector<bool> &visited) {
    if (u+1 == 0) return true;

    for (const Edge<Weight> &e: g[u]) {
      if (visited[e.dst]) continue;
      visited[e.dst] = true;
      if (augment(pair[e.dst], visited)) {
        pair[e.src] = e.dst;
        pair[e.dst] = e.src;
        return true;
      }
    }
    return false;
  };

  size_t match = 0;
  for (size_t i = 0; i < mid; ++i) {
    std::vector<bool> visited(g.size());
    if (augment(i, visited))
      ++match;
  }
  return match;
}

int main() {
  size_t N, M;
  scanf("%zu %zu", &N, &M);

  std::vector<std::string> s(N);
  int w = 0;
  int b = 0;
  for (auto& si: s) {
    char buf[64];
    scanf("%s", buf);
    si = buf;
    w += std::count(si.begin(), si.end(), 'w');
    b += std::count(si.begin(), si.end(), 'b');
  }

  std::vector<std::vector<size_t>> ind(N, std::vector<size_t>(M));
  size_t mid;
  {
    size_t cur = 0;
    for (size_t i = 0; i < N; ++i)
      for (size_t j = i%2; j < M; j += 2)
        ind[i][j] = cur++;

    mid = cur;
    for (size_t i = 0; i < N; ++i)
      for (size_t j = 1-i%2; j < M; j += 2)
        ind[i][j] = cur++;
  }

  Graph<int> g(N*M);
  for (size_t i = 0; i < N; ++i) {
    for (size_t j = 0; j < M; ++j) {
      if (s[i][j] != 'w') continue;

      for (int k = 0; k < 4; ++k) {
        size_t ni = i + di[k];
        size_t nj = j + dj[k];
        if (!(ni < N && nj < M)) continue;
        if (s[ni][nj] != 'b') continue;

        g.connect_with(ind[i][j], ind[ni][nj]);
      }
    }
  }

  int max = bipartite_match(g, mid);
  int res = 100*max;
  w -= max;
  b -= max;
  if (w > b) std::swap(w, b);
  res += 10*w;
  res += b-w;
  printf("%d\n", res);
}
0