結果

問題 No.2238 Rock and Hole
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-03-03 21:37:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 3,000 ms
コード長 4,614 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 109,284 KB
実行使用メモリ 10,820 KB
最終ジャッジ日時 2024-09-17 22:31:25
合計ジャッジ時間 2,346 ms
ジャッジサーバーID
(参考情報)
judge6 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,760 KB
testcase_01 AC 3 ms
7,764 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
7,760 KB
testcase_04 AC 2 ms
8,012 KB
testcase_05 AC 3 ms
9,808 KB
testcase_06 AC 3 ms
7,760 KB
testcase_07 AC 3 ms
9,812 KB
testcase_08 AC 2 ms
7,888 KB
testcase_09 AC 3 ms
9,680 KB
testcase_10 AC 5 ms
8,320 KB
testcase_11 AC 4 ms
7,904 KB
testcase_12 AC 4 ms
8,172 KB
testcase_13 AC 4 ms
8,036 KB
testcase_14 AC 3 ms
8,028 KB
testcase_15 AC 6 ms
10,820 KB
testcase_16 AC 5 ms
9,264 KB
testcase_17 AC 5 ms
8,816 KB
testcase_18 AC 4 ms
8,352 KB
testcase_19 AC 9 ms
8,644 KB
testcase_20 AC 6 ms
8,420 KB
testcase_21 AC 7 ms
8,508 KB
testcase_22 AC 5 ms
8,220 KB
testcase_23 AC 2 ms
7,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

namespace bm {
constexpr int LIM_N0 = 100'010;
constexpr int LIM_N1 = 100'010;
constexpr int LIM_M = 400'010;
int n0, n1, m, as[LIM_M], bs[LIM_M];
int to[LIM_N0], fr[LIM_N1], tof;
int pt[LIM_N0 + 2], zu[LIM_M], used[LIM_N0], lev[LIM_N0], que[LIM_N0], *qb, *qe;
void init(int n0_, int n1_) {
  n0 = n0_; n1 = n1_; m = 0;
}
int ae(int u, int v) {
  as[m] = u; bs[m] = v; return m++;
}
int augment(int u) {
  used[u] = tof;
  for (int j = pt[u]; j < pt[u + 1]; ++j) {
    const int v = zu[j];
    const int w = fr[v];
    if (!~w || (used[w] != tof && lev[u] < lev[w] && augment(w))) {
      to[u] = v; fr[v] = u; return 1;
    }
  }
  return 0;
}
int run() {
  memset(pt, 0, (n0 + 2) * sizeof(int));
  for (int i = 0; i < m; ++i) ++pt[as[i] + 2];
  for (int u = 2; u <= n0; ++u) pt[u + 1] += pt[u];
  for (int i = 0; i < m; ++i) zu[pt[as[i] + 1]++] = bs[i];
  memset(to, ~0, n0 * sizeof(int));
  memset(fr, ~0, n1 * sizeof(int));
  memset(used, ~0, n0 * sizeof(int));
  for (tof = 0; ; ) {
    qb = qe = que; memset(lev, ~0, n0 * sizeof(int));
    for (int u = 0; u < n0; ++u) if (!~to[u]) lev[*qe++ = u] = 0;
    for (; qb != qe; ) {
      const int u = *qb++;
      for (int j = pt[u]; j < pt[u + 1]; ++j) {
        const int w = fr[zu[j]];
        if (~w && !~lev[w]) lev[*qe++ = w] = lev[u] + 1;
      }
    }
    int f = 0;
    for (int u = 0; u < n0; ++u) if (!~to[u]) f += augment(u);
    if (!f) return tof;
    tof += f;
  }
}

// s: true, t: false (s: reachable from unmatched left)
// vertex cover: (0: false, 0: true)
// independent set: (0: true, 1: false)
bool side0[LIM_N0], side1[LIM_N1];
void dfs(int u) {
  if (!side0[u]) {
    side0[u] = true;
    for (int j = pt[u]; j < pt[u + 1]; ++j) {
      const int v = zu[j];
      if (!side1[v]) {
        side1[v] = true;
        const int w = fr[v];
        if (~w) dfs(w);
      }
    }
  }
}
void minCut() {
  memset(side0, 0, n0 * sizeof(bool));
  memset(side1, 0, n1 * sizeof(bool));
  for (int u = 0; u < n0; ++u) if (!~to[u]) dfs(u);
}
}  // namespace bm


char buf[100'010];

int H, W;
vector<string> S;

int main() {
  for (; ~scanf("%d%d", &H, &W); ) {
    S.resize(H);
    for (int x = 0; x < H; ++x) {
      scanf("%s", buf);
      S[x] = buf;
    }
    
    int A = 0, B = 0;
    vector<vector<int>> ids(H, vector<int>(W, -1));
    for (int x = 0; x < H; ++x) for (int y = 0; y < W; ++y) {
      if (S[x][y] == 'r') ids[x][y] = A++;
      if (S[x][y] == 'h') ids[x][y] = B++;
    }
    
    bm::init(A, B);
    {
      int bx, by;
      bx = by = -1;
      for (int x = 0; x < H; ++x) for (int y = 0; y < W; ++y) {
        if (S[x][y] == 'r') if (x == bx) bm::ae(ids[x][y], ids[bx][by]);
        if (S[x][y] == 'h') { bx = x; by = y; }
      }
      bx = by = -1;
      for (int x = 0; x < H; ++x) for (int y = W; --y >= 0; ) {
        if (S[x][y] == 'r') if (x == bx) bm::ae(ids[x][y], ids[bx][by]);
        if (S[x][y] == 'h') { bx = x; by = y; }
      }
      bx = by = -1;
      for (int y = 0; y < W; ++y) for (int x = 0; x < H; ++x) {
        if (S[x][y] == 'r') if (y == by) bm::ae(ids[x][y], ids[bx][by]);
        if (S[x][y] == 'h') { bx = x; by = y; }
      }
      bx = by = -1;
      for (int y = 0; y < W; ++y) for (int x = H; --x >= 0; ) {
        if (S[x][y] == 'r') if (y == by) bm::ae(ids[x][y], ids[bx][by]);
        if (S[x][y] == 'h') { bx = x; by = y; }
      }
    }
    
    const int res = bm::run();
    puts((res == A) ? "Yes" : "No");
  }
  return 0;
}
0