結果
| 問題 |
No.421 しろくろチョコレート
|
| ユーザー |
beet
|
| 提出日時 | 2018-11-08 12:37:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,130 bytes |
| コンパイル時間 | 2,721 ms |
| コンパイル使用メモリ | 216,368 KB |
| 最終ジャッジ日時 | 2025-01-06 15:52:00 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 56 WA * 9 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
struct BiMatch{
int L,R;
vector<vector<int> > G;
vector<int> match,level;
BiMatch(){}
BiMatch(int L,int R):L(L),R(R),G(L+R),match(L+R,-1),level(L){}
void add_edge(int u,int v){
G[u].push_back(v+L);
G[v+L].push_back(u);
}
bool bfs(){
queue<int> q;
for(int i=0;i<L;i++){
level[i]=-1;
if(match[i]<0){
level[i]=0;
q.emplace(i);
}
}
while(!q.empty()){
int v=q.front();q.pop();
for(int u:G[v]){
int w=match[u];
if(w<0) return true;
if(level[w]<0){
level[w]=level[v]+1;
q.emplace(w);
}
}
}
return false;
}
bool dfs(int v){
for(int u:G[v]){
int w=match[u];
if(w<0||(level[w]>level[v]&&dfs(w))){
match[v]=u;
match[u]=v;
return true;
}
}
return false;
}
int build(){
int res=0;
while(bfs())
for(int i=0;i<L;i++)
if(match[i]<0&&dfs(i))
res++;
return res;
}
};
//INSERT ABOVE HERE
signed main(){
int h,w;
cin>>h>>w;
vector<string> s(h);
for(int i=0;i<h;i++) cin>>s[i];
int wc=0,bc=0;
using P = pair<int, int>;
map<P, int> ws,bs;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(s[i][j]=='w') ws[P(i,j)]=wc++;
if(s[i][j]=='b') bs[P(i,j)]=bc++;
}
}
BiMatch bm(wc,bc);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(s[i][j]!='w') continue;
if(i+1< h&&s[i+1][j]=='b')
bm.add_edge(ws[P(i,j)],bs[P(i+1,j)]);
if(i-1>=0&&s[i-1][j]=='b')
bm.add_edge(ws[P(i,j)],bs[P(i-1,j)]);
if(j+1< h&&s[i][j+1]=='b')
bm.add_edge(ws[P(i,j)],bs[P(i,j+1)]);
if(j-1>=0&&s[i][j-1]=='b')
bm.add_edge(ws[P(i,j)],bs[P(i,j-1)]);
}
}
int k=bm.build();
int d=min(wc,bc)-k;
int ans=k*100+d*10+wc+bc-2*(k+d);
cout<<ans<<endl;
return 0;
}
beet