結果

問題 No.2238 Rock and Hole
ユーザー SSRSSSRS
提出日時 2023-03-03 21:52:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 3,000 ms
コード長 3,170 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 196,052 KB
実行使用メモリ 21,696 KB
最終ジャッジ日時 2023-10-18 01:51:28
合計ジャッジ時間 3,887 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 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 9 ms
5,564 KB
testcase_11 AC 37 ms
14,920 KB
testcase_12 AC 49 ms
15,296 KB
testcase_13 AC 35 ms
14,884 KB
testcase_14 AC 20 ms
10,016 KB
testcase_15 AC 11 ms
7,256 KB
testcase_16 AC 64 ms
18,512 KB
testcase_17 AC 70 ms
21,696 KB
testcase_18 AC 63 ms
19,596 KB
testcase_19 AC 70 ms
10,016 KB
testcase_20 AC 42 ms
12,264 KB
testcase_21 AC 37 ms
10,748 KB
testcase_22 AC 34 ms
11,092 KB
testcase_23 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
template <typename Cap>
struct dinic{
  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;
  dinic(){
  }
  dinic(int N): N(N), G(N){
  }
  void add_edge(int from, int to, Cap cap){
    G[from].push_back(edge(to, G[to].size(), cap));
    G[to].push_back(edge(from, G[from].size() - 1, 0));
  }
  Cap dfs(vector<int> &d, vector<int> &iter, int v, int t, Cap f){
    if (v == t){
      return f; 
    }
    while (iter[v] < G[v].size()){
      int w = G[v][iter[v]].to;
      if (G[v][iter[v]].cap > 0 && d[v] < d[w]){
        Cap f2 = dfs(d, iter, w, t, min(f, G[v][iter[v]].cap));
        if (f2 > 0){
          G[v][iter[v]].cap -= f2;
          G[w][G[v][iter[v]].rev].cap += f2;
          return f2;
        }
      }
      iter[v]++;
    }
    return 0;
  }
  Cap max_flow(int s, int t){
    Cap flow = 0;
    while (true){
      vector<int> d(N, -1);
      d[s] = 0;
      queue<int> Q;
      Q.push(s);
      while (!Q.empty()){
        if (d[t] != -1){
          break;
        }
        int v = Q.front();
        Q.pop();
        for (auto &e : G[v]){
          int w = e.to;
          if (e.cap > 0 && d[w] == -1){
            d[w] = d[v] + 1;
            Q.push(w);
          }
        }
      }
      if (d[t] == -1){
        break;
      }
      vector<int> iter(N, 0);
      while (true){
        Cap f = dfs(d, iter, s, t, INF);
        if (f == 0){
          break;
        }
        flow += f;
      }
    }
    return flow;
  }
};
int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<char>> S(H, vector<char>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> S[i][j];
    }
  }
  vector<pair<int, int>> r, h;
  vector<vector<int>> id(H, vector<int>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (S[i][j] == 'r'){
        id[i][j] = r.size();
        r.push_back(make_pair(i, j));
      }
      if (S[i][j] == 'h'){
        id[i][j] = h.size();
        h.push_back(make_pair(i, j));
      }
    }
  }
  int N = r.size(), M = h.size();
  vector<set<int>> st1(H), st2(W);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (S[i][j] == 'h'){
        st1[i].insert(j);
        st2[j].insert(i);
      }
    }
  }
  dinic<int> G(N + M + 2);
  for (int i = 0; i < N; i++){
    G.add_edge(N + M, i, 1);
  }
  for (int i = 0; i < M; i++){
    G.add_edge(N + i, N + M + 1, 1);
  }
  for (int i = 0; i < N; i++){
    int x = r[i].first;
    int y = r[i].second;
    auto itr1 = st1[x].lower_bound(y);
    if (itr1 != st1[x].end()){
      G.add_edge(i, N + id[x][*itr1], 1);
    }
    if (itr1 != st1[x].begin()){
      G.add_edge(i, N + id[x][*prev(itr1)], 1);
    }
    auto itr2 = st2[y].lower_bound(x);
    if (itr2 != st2[y].end()){
      G.add_edge(i, N + id[*itr2][y], 1);
    }
    if (itr2 != st2[y].begin()){
      G.add_edge(i, N + id[*prev(itr2)][y], 1);
    }
  }
  int F = G.max_flow(N + M, N + M + 1);
  if (F == N){
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
0