結果
問題 | No.2238 Rock and Hole |
ユーザー |
![]() |
提出日時 | 2023-03-03 23:32:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 70 ms / 3,000 ms |
コード長 | 3,093 bytes |
コンパイル時間 | 2,292 ms |
コンパイル使用メモリ | 215,968 KB |
最終ジャッジ日時 | 2025-02-11 04:44:01 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 24 |
ソースコード
#include<bits/stdc++.h>#define REP(i,n) for(int i=0,i##_len=int(n);i<i##_len;++i)#define rep(i,a,b) for(int i=int(a);i<int(b);++i)#define All(x) (x).begin(),(x).end()#define rAll(x) (x).rbegin(),(x).rend()using namespace std;using ll = long long;struct HopcroftKarp {vector< vector< int > > graph;vector< int > dist, match;vector< bool > used, vv;HopcroftKarp(int n, int m) : graph(n), match(m, -1), used(n) {}void add_edge(int u, int v) {graph[u].push_back(v);}void bfs() {dist.assign(graph.size(), -1);queue< int > que;for(int i = 0; i < graph.size(); i++) {if(!used[i]) {que.emplace(i);dist[i] = 0;}}while(!que.empty()) {int a = que.front();que.pop();for(auto &b : graph[a]) {int c = match[b];if(c >= 0 && dist[c] == -1) {dist[c] = dist[a] + 1;que.emplace(c);}}}}bool dfs(int a) {vv[a] = true;for(auto &b : graph[a]) {int c = match[b];if(c < 0 || (!vv[c] && dist[c] == dist[a] + 1 && dfs(c))) {match[b] = a;used[a] = true;return (true);}}return (false);}int bipartite_matching() {int ret = 0;while(true) {bfs();vv.assign(graph.size(), false);int flow = 0;for(int i = 0; i < graph.size(); i++) {if(!used[i] && dfs(i)) ++flow;}if(flow == 0) return (ret);ret += flow;}}void output() {for(int i = 0; i < match.size(); i++) {if(~match[i]) {cout << match[i] << "-" << i << endl;}}}};int main() {int h,w;cin>>h>>w;assert(h*w <= 100000);vector<string> s(h);REP(i,h) cin>>s[i];// 岩と穴に番号を付けるint X = 0, Y = 0;map<pair<int,int>, int> x, yw, yh;REP(i, h) REP(j, w) {if(s[i][j] == 'r'){x[pair<int,int>(i,j)] = X;X += 1;}if(s[i][j] == 'h') {yw[pair<int,int>(j, i)] = Y;yh[pair<int,int>(i, j)] = Y;Y += 1;}}HopcroftKarp G(X , Y);for (auto kvp: x) { // X(1~X) から Y(X+1~X+Y) への辺pair<int,int> pos = kvp.first;int node = kvp.second;auto iter = yw.lower_bound(pair<int,int>(pos.second, pos.first));if(iter != yw.end() && iter->first.first == pos.second) {G.add_edge(node, iter->second);}if(iter != yw.begin()) {iter = prev(iter);if(iter->first.first == pos.second) {G.add_edge(node, iter->second);}}iter = yh.lower_bound(pos);if(iter != yh.end() && iter->first.first == pos.first) {G.add_edge(node, iter->second);}if(iter != yh.begin()) {iter = prev(iter);if(iter->first.first == pos.first) {G.add_edge(node, iter->second);}}}cout << (G.bipartite_matching() == X?"Yes":"No") << endl;}