結果

問題 No.421 しろくろチョコレート
ユーザー hs0319boyhs0319boy
提出日時 2020-07-25 04:01:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,254 bytes
コンパイル時間 1,694 ms
コンパイル使用メモリ 177,988 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 15:01:30
合計ジャッジ時間 3,292 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 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 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 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 4 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 2 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 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 4 ms
4,348 KB
testcase_39 AC 3 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 3 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 2 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 3 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 4 ms
4,348 KB
testcase_61 AC 7 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 ll=long long;
using vin=vector<int>;
using vvin=vector<vector<int>>;
using vll=vector<long long>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const int inf=1e9+7;
const ll INF=1e18;
vin dy={0,1,0,-1};
vin dx={1,0,-1,0};

int MAX_V=2600;

vvin g(MAX_V);//グラフの隣接リスト表現
vin match(MAX_V);//マッチングのペア
vbo used(MAX_V);

void add_edge(int u,int v){
    g[u].push_back(v);
    g[v].push_back(u);
}

//増加パスをdfsで探す
bool dfs(int v){
    used[v]=true;
    for(auto u:g[v]){
        int w=match[u];
        if(w<0||(!used[w]&&dfs(w))){
            match[v]=u;
            match[u]=v;
            return true;
        }
    }
    return false;
}

//二部グラフの最大マッチングを求める
int max_matching(int v){//vは頂点数
    int res=0;
    fill(all(match),-1);
    rep(i,v){
        if(match[i]<0){
            fill(all(used),false);
            if(dfs(i))res++;
        }
    }
    return res;
}

int main(){
    int n,m;cin>>n>>m;
    vstr s(n);rep(i,n)cin>>s[i];
    int res=0;int ni,nj;
    rep(i,n)rep(j,m){
        if(s[i][j]=='w'){
            rep(k,4){
                ni=i+dy[k];
                nj=j+dx[k];
                if(0<=ni&&ni<n&&0<=nj&&nj<m&&s[ni][nj]=='b')add_edge(i*m+j,ni*m+nj);
            }
        }
    }
    int ans=100*max_matching(n*m);
    rep(i,n)rep(j,m){
        g[i*m+j].clear();
        if(match[i*m+j]>=0)s[i][j]='.';
    }
    rep(i,n)rep(j,m){
        if(s[i][j]=='w'){
            rep(k,n)rep(l,m)if(s[k][l]=='b')add_edge(i*m+j,k*m+l);
        }
    }
    ans+=10*max_matching(n*m);
    rep(i,n)rep(j,m){
        g[i*m+j].clear();
        if(match[i*m+j]>=0)s[i][j]='.';
    }
    rep(i,n)rep(j,m)if(s[i][j]!='.')ans++;
    cout<<ans<<endl;
}
0