結果
問題 | No.2238 Rock and Hole |
ユーザー | kumakuma |
提出日時 | 2023-02-04 10:25:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 89 ms / 3,000 ms |
コード長 | 3,979 bytes |
コンパイル時間 | 2,153 ms |
コンパイル使用メモリ | 215,812 KB |
実行使用メモリ | 33,880 KB |
最終ジャッジ日時 | 2024-07-03 09:07:10 |
合計ジャッジ時間 | 4,070 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 9 ms
18,908 KB |
testcase_01 | AC | 10 ms
18,848 KB |
testcase_02 | AC | 9 ms
18,924 KB |
testcase_03 | AC | 9 ms
18,832 KB |
testcase_04 | AC | 10 ms
18,848 KB |
testcase_05 | AC | 9 ms
18,888 KB |
testcase_06 | AC | 11 ms
18,716 KB |
testcase_07 | AC | 10 ms
18,692 KB |
testcase_08 | AC | 10 ms
18,828 KB |
testcase_09 | AC | 9 ms
18,804 KB |
testcase_10 | AC | 10 ms
19,432 KB |
testcase_11 | AC | 19 ms
22,632 KB |
testcase_12 | AC | 20 ms
23,004 KB |
testcase_13 | AC | 18 ms
22,732 KB |
testcase_14 | AC | 14 ms
20,900 KB |
testcase_15 | AC | 11 ms
19,880 KB |
testcase_16 | AC | 38 ms
31,340 KB |
testcase_17 | AC | 49 ms
33,880 KB |
testcase_18 | AC | 24 ms
25,180 KB |
testcase_19 | AC | 89 ms
25,984 KB |
testcase_20 | AC | 49 ms
25,916 KB |
testcase_21 | AC | 79 ms
26,048 KB |
testcase_22 | AC | 27 ms
23,400 KB |
testcase_23 | AC | 10 ms
18,880 KB |
ソースコード
#include <bits/stdc++.h> // #include <atcoder/modint> #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ull unsigned long long #define ld long double #define pll pair<ll, ll> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const double PI = 3.141592653589793238462643383279; //=============dinic============================ ll MAX_V = 400000; struct edge {ll to, cap, rev;}; vector<vector<edge>> G(MAX_V); vector<ll> level(MAX_V); vector<ll> iter(MAX_V); void add_edge(ll from, ll to, ll cap = 1) { G.at(from).push_back((edge){to, cap, (ll)G.at(to).size()}); G.at(to).push_back((edge){from, 0ll, (ll)G.at(from).size() - 1}); } void bfs(ll s) { for (ll i = 0; i < MAX_V; ++i) { level.at(i) = -1; } queue<ll> q; level.at(s) = 0; q.push(s); while (!q.empty()) { ll v = q.front(); q.pop(); for (ll i = 0; i < G.at(v).size(); ++i) { edge &e = G.at(v).at(i); if (e.cap > 0 && level.at(e.to) < 0) { level.at(e.to) = level.at(v) + 1; q.push(e.to); } } } } ll dfs(ll v, ll t, ll f) { if (v == t) { return f; } for (ll &i = iter.at(v); i < G.at(v).size(); ++i) { edge &e = G.at(v).at(i); if (e.cap > 0 && level.at(v) < level.at(e.to)) { ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G.at(e.to).at(e.rev).cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t) { ll flow = 0; while (true) { bfs(s); if (level.at(t) < 0) { return flow; } for (ll i = 0; i < MAX_V; ++i) { iter.at(i) = 0; } ll f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } return flow; } //================================================= int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll H, W; cin >> H >> W; vector<string> S(H); for (ll i = 0; i < H; ++i) { cin >> S.at(i); } ll rock = 0, hole = 0; for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { if (S.at(i).at(j) == 'r') { rock += 1; } if (S.at(i).at(j) == 'h') { hole += 1; } } } ll s = H * W, t = s + 1; for (ll i = 0; i < H; ++i) { pll before_l = {-1, -1}; for (ll j = 0; j < W; ++j) { if (S.at(i).at(j) == 'h') { before_l = {i, j}; } if (S.at(i).at(j) == 'r') { if (before_l != (pll){-1, -1}) { add_edge(before_l.first * W + before_l.second, i * W + j); } } } pll before_r = {-1, -1}; for (ll j = W - 1; j >= 0; --j) { if (S.at(i).at(j) == 'h') { before_r = {i, j}; } if (S.at(i).at(j) == 'r') { if (before_r != (pll){-1, -1}) { add_edge(before_r.first * W + before_r.second, i * W + j); } } } } for (ll j = 0; j < W; ++j) { pll before_u = {-1, -1}; for (ll i = 0; i < H; ++i) { if (S.at(i).at(j) == 'h') { before_u = {i, j}; } if (S.at(i).at(j) == 'r') { if (before_u != (pll){-1, -1}) { add_edge(before_u.first * W + before_u.second, i * W + j); } } } pll before_d = {-1, -1}; for (ll i = H - 1; i >= 0; --i) { if (S.at(i).at(j) == 'h') { before_d = {i, j}; } if (S.at(i).at(j) == 'r') { if (before_d != (pll){-1, -1}) { add_edge(before_d.first * W + before_d.second, i * W + j); } } } } for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { if (S.at(i).at(j) == 'h') { add_edge(s, i * W + j); } else if(S.at(i).at(j) == 'r') { add_edge(i * W + j, t); } } } if (max_flow(s, t) == rock) { cout << "Yes" << "\n"; } else { cout << "No" << "\n"; } }