結果
問題 | No.2238 Rock and Hole |
ユーザー | umimel |
提出日時 | 2023-03-03 23:19:14 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,101 bytes |
コンパイル時間 | 2,519 ms |
コンパイル使用メモリ | 194,608 KB |
実行使用メモリ | 45,424 KB |
最終ジャッジ日時 | 2024-09-18 00:25:47 |
合計ジャッジ時間 | 4,009 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; #define drep(i, cc, n) for (int i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll INF = 1LL << 60; const ll MAX_N = 2e5; struct Dinic{ struct Edge{ ll to; ll cap; ll rev; }; vector<vector<Edge>> G; vector<ll> level; vector<ll> iter; ll N; Dinic(vector<vector<pll>> &G_){ N = (ll)G_.size(); G.resize(N); level.resize(N); iter.resize(N); rep(from, N){ for(pll p : G_[from]){ ll to = p.fi; ll cap = p.se; G[from].pb((Edge){to, cap, (ll)G[to].size()}); G[to].pb((Edge){from, 0, (ll)G[from].size()-1}); } } } void bfs(ll s, vector<vector<Edge>> &H){ rep(i, N) level[i]=-1; queue<ll> Q; level[s] = 0; Q.push(s); while(!Q.empty()){ ll v = Q.front(); Q.pop(); for(ll i=0; i<(ll)H[v].size(); i++){ Edge &e = H[v][i]; if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; Q.push(e.to); } } } } ll dfs(ll v, ll t, ll f, vector<vector<Edge>> &H){ if(v == t) return f; for(ll &i=iter[v]; i<(ll)H[v].size(); i++){ Edge &e = H[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ ll d = dfs(e.to, t, min(f, e.cap), H); if(d > 0){ e.cap -= d; H[e.to][e.rev].cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t){ vector<vector<Edge>> H(N); rep(from, N) for(Edge e : G[from]) H[from].pb(e); ll flow = 0; for(;;){ bfs(s, H); if(level[t] < 0) return flow; rep(i, N) iter[i] = 0; ll f; while((f = dfs(s, t, INF, H)) > 0){ flow += f; } } } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll h, w; cin >> h >> w; vector<string> s(h); rep(i, h) cin >> s[i]; ll hall = 0, rock = 0; vector<vector<ll>> alloc(h, vector<ll>(w, INF)); rep(i, h) rep(j, w) if(s[i][j]=='h') alloc[i][j]=hall++; rep(i, h) rep(j, w) if(s[i][j]=='r') alloc[i][j]=hall+(rock++); if(hall < rock){ cout << "NO" << endl; return 0; } vector<vector<ll>> line_hall(h), row_hall(w); rep(i, h) rep(j, w) if(s[i][j]=='h') line_hall[i].pb(j); rep(j, w) rep(i, h) if(s[i][j]=='h') row_hall[j].pb(i); vector<vector<pll>> G(hall+rock+2); ll S = hall+rock; ll T = hall+rock+1; rep(v, hall) G[S].pb({v, 1}); rep(i, h) rep(j, w){ if(s[i][j]=='r'){ //上側とマッチ auto itr = lower_bound(all(row_hall[j]), i); if(itr!=row_hall[j].begin()){ itr--; G[alloc[*itr][j]].pb({alloc[i][j], 1}); } //下側とマッチ itr = lower_bound(all(row_hall[j]), i); if(itr!=row_hall[j].end()){ G[alloc[*itr][j]].pb({alloc[i][j], 1}); } //左 itr = lower_bound(all(line_hall[i]), j); if(itr!=line_hall[i].begin()){ itr--; G[alloc[i][*itr]].pb({alloc[i][j], 1}); } //右 itr = lower_bound(all(line_hall[i]), j); if(itr!=line_hall[i].end()){ G[alloc[i][*itr]].pb({alloc[i][j], 1}); } } } for(ll v=hall; v<S; v++) G[v].pb({T, 1}); Dinic dinic(G); if(dinic.max_flow(S, T)==rock){ cout << "YES" << endl; }else{ cout << "NO" << endl; } }