結果

問題 No.421 しろくろチョコレート
ユーザー YazatenYazaten
提出日時 2016-09-22 21:07:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,662 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 164,816 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:49:58
合計ジャッジ時間 3,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 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 4 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 4 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 4 ms
4,348 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 4 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 4 ms
4,348 KB
testcase_35 AC 4 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 5 ms
4,348 KB
testcase_38 AC 5 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 4 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 5 ms
4,348 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 5 ms
4,348 KB
testcase_49 AC 4 ms
4,348 KB
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 6 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 3 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 3 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 5 ms
4,348 KB
testcase_61 AC 4 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 1 ms
4,348 KB
testcase_64 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)


//2部最大マッチング verified AOJ GRL_7_A
#define MAX_V 2500
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u,int v){
	G[u].pb(v);
	G[v].pb(u);
}

// 増大路をDFSで探す
bool dfs(int v){
	used[v]=true;
	rep(i,G[v].size()){
		int u=G[v][i],w=match[u];           //u:vからの移動先の頂点 , w:uとマッチングしている頂点
		if(w<0 || (!used[w] && dfs(w))){    //uがフリーか、このdfsではwを使っておらず かつwの別のペアが見つかった(=増大路発見) 場合
			match[v]=u;
			match[u]=v;
			return true;
		}
	}
	return false;
}

int bipartite_matching(int V){
	int res=0;
	rep(i,MAX_V)match[i]=-1;
	rep(v,V){
		if(match[v]<0){
			rep(i,MAX_V)used[i]=0;
			if( dfs(v) ) res++;       //増大路が見つかればres+=1
		}
	}
	return res;
}

int main(){
	int h,w;
	cin>>h>>w;
	vector<string> vs(h);
	rep(i,h)cin>>vs[i];
	
	int white=0,black=0;
	rep(i,h)rep(j,w){
		if(vs[i][j]=='w')white++;
		if(vs[i][j]=='b')black++;
	}
	
	rep(i,h){
		rep(j,w){
			if(vs[i][j]=='b'){
				int dy[]={1,0,-1,0};
				int dx[]={0,1,0,-1};
				rep(k,4){
					int ddy = i+dy[k];
					int ddx = j+dx[k];
					if(  ddx>=0 && ddy>=0 && ddx<w && ddy<h &&vs[ddy][ddx]=='w' ){
						add_edge(i*w+j,ddy*w+ddx);
					}
				}
			}
		}
	}
	
	int res = bipartite_matching(h*w);
	white -= res;
	black -= res;
	cout<<res*100+min(white,black)*10+abs(white-black)<<endl;
	
}
0