結果
| 問題 |
No.2238 Rock and Hole
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-03-03 22:11:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 3,000 ms |
| コード長 | 4,567 bytes |
| コンパイル時間 | 2,642 ms |
| コンパイル使用メモリ | 212,900 KB |
| 最終ジャッジ日時 | 2025-02-11 02:51:16 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include <bits/stdc++.h>
using i64 = long long;
template<class T>
struct MaxFlow {
struct _Edge {
int to;
T cap;
_Edge(int to, T cap) : to(to), cap(cap) {}
};
int n;
std::vector<_Edge> e;
std::vector<std::vector<int>> g;
std::vector<int> cur, h;
MaxFlow() {}
MaxFlow(int n) {
init(n);
}
void init(int n) {
this->n = n;
e.clear();
g.assign(n, {});
cur.resize(n);
h.resize(n);
}
bool bfs(int s, int t) {
h.assign(n, -1);
std::queue<int> que;
h[s] = 0;
que.push(s);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (int i : g[u]) {
auto [v, c] = e[i];
if (c > 0 && h[v] == -1) {
h[v] = h[u] + 1;
if (v == t) {
return true;
}
que.push(v);
}
}
}
return false;
}
T dfs(int u, int t, T f) {
if (u == t) {
return f;
}
auto r = f;
for (int &i = cur[u]; i < int(g[u].size()); ++i) {
const int j = g[u][i];
auto [v, c] = e[j];
if (c > 0 && h[v] == h[u] + 1) {
auto a = dfs(v, t, std::min(r, c));
e[j].cap -= a;
e[j ^ 1].cap += a;
r -= a;
if (r == 0) {
return f;
}
}
}
return f - r;
}
void addEdge(int u, int v, T c) {
g[u].push_back(e.size());
e.emplace_back(v, c);
g[v].push_back(e.size());
e.emplace_back(u, 0);
}
T flow(int s, int t) {
T ans = 0;
while (bfs(s, t)) {
cur.assign(n, 0);
ans += dfs(s, t, std::numeric_limits<T>::max());
}
return ans;
}
std::vector<bool> minCut() {
std::vector<bool> c(n);
for (int i = 0; i < n; i++) {
c[i] = (h[i] != -1);
}
return c;
}
struct Edge {
int from;
int to;
T cap;
T flow;
};
std::vector<Edge> edges() {
std::vector<Edge> a;
for (int i = 0; i < e.size(); i += 2) {
Edge x;
x.from = e[i + 1].to;
x.to = e[i].to;
x.cap = e[i].cap + e[i + 1].cap;
x.flow = e[i + 1].cap;
a.push_back(x);
}
return a;
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int H, W;
std::cin >> H >> W;
std::vector<std::string> s(H);
int cnt = 0;
MaxFlow<int> g(H * W + 2);
int S = H * W, T = S + 1;
for (int i = 0; i < H; i++) {
std::cin >> s[i];
for (int j = 0; j < W; j++) {
if (s[i][j] == 'r') {
g.addEdge(S, i * W + j, 1);
cnt++;
}
if (s[i][j] == 'h') {
g.addEdge(i * W + j, T, 1);
}
}
}
for (int i = 0; i < H; i++) {
int k = -1;
for (int j = 0; j < W; j++) {
if (s[i][j] == 'h') {
k = j;
}
if (s[i][j] == 'r') {
if (k != -1) {
g.addEdge(i * W + j, i * W + k, 1);
}
}
}
k = -1;
for (int j = W - 1; j >= 0; j--) {
if (s[i][j] == 'h') {
k = j;
}
if (s[i][j] == 'r') {
if (k != -1) {
g.addEdge(i * W + j, i * W + k, 1);
}
}
}
}
for (int j = 0; j < W; j++) {
int k = -1;
for (int i = 0; i < H; i++) {
if (s[i][j] == 'h') {
k = i;
}
if (s[i][j] == 'r') {
if (k != -1) {
g.addEdge(i * W + j, k * W + j, 1);
}
}
}
k = -1;
for (int i = H - 1; i >= 0; i--) {
if (s[i][j] == 'h') {
k = i;
}
if (s[i][j] == 'r') {
if (k != -1) {
g.addEdge(i * W + j, k * W + j, 1);
}
}
}
}
if (g.flow(S, T) == cnt) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
return 0;
}