結果

問題 No.421 しろくろチョコレート
ユーザー k
提出日時 2021-05-01 14:45:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 2,129 ms
コンパイル使用メモリ 204,348 KB
最終ジャッジ日時 2025-01-21 05:11:57
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class bipartite_matching {
  int n;
  vector<vector<int> > g;
  vector<int> match;
  vector<bool> used;
  
  bool dfs(int v) {
    used[v] = true;
    for (int u: g[v]) {
      int w = match[u];
      if (w < 0 || !used[w] && dfs(w)) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }
  
public:
  bipartite_matching(int n) {
    this->n = n;
    this->g = vector<vector<int> >(n);
    this->match = vector<int>(n, -1);
    this->used = vector<bool>(n);
  }
  
  void add_edge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }
  
  int build() {
    int ret = 0;
    for (int v = 0; v < n; v++) {
      if (match[v] < 0) {
        fill(used.begin(), used.end(), false);
        if (dfs(v))
          ++ret;
      }
    }
    return ret;
  }
  
  int pair(int v) {
    return match[v];
  }
};


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int r, c;
  cin >> r >> c;

  vector<string> bd(r);
  for (int i = 0; i < r; i++)
    cin >> bd[i];

  int black = 0;
  int white = 0;
  bipartite_matching matcher(r * c);
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      if (bd[i][j] == '.') continue;
      else if (bd[i][j] == 'w') ++white;
      else ++black;
      if (j + 1 < c && bd[i][j+1] != '.') 
        matcher.add_edge(i * c + j, i * c + j + 1);
      if (i + 1 < r && bd[i+1][j] != '.')
        matcher.add_edge(i * c + j, (i + 1) * c + j);
    }
  }

  int matched =  matcher.build();
  int ret = 100 * matched;
  white -= matched;
  black -= matched;

  ret += 10 * min(white, black);
  ret += max(white, black) - min(white, black);
  cout << ret << endl;
  
  return 0;
}
0