結果

問題 No.421 しろくろチョコレート
ユーザー beetbeet
提出日時 2018-11-08 12:38:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,130 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 224,096 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:56:52
合計ジャッジ時間 4,528 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 5 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 20 ms
4,348 KB
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 11 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 3 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 11 ms
4,348 KB
testcase_61 AC 4 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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< w&&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;
}
0