結果

問題 No.348 カゴメカゴメ
ユーザー btkbtk
提出日時 2016-05-19 22:02:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 2,944 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 185,848 KB
実行使用メモリ 16,872 KB
最終ジャッジ日時 2024-04-19 08:27:20
合計ジャッジ時間 4,683 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
7,680 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 70 ms
16,872 KB
testcase_03 AC 62 ms
10,368 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 47 ms
9,856 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 61 ms
7,400 KB
testcase_24 AC 61 ms
7,296 KB
testcase_25 AC 61 ms
7,256 KB
testcase_26 AC 62 ms
7,276 KB
testcase_27 AC 61 ms
7,308 KB
testcase_28 AC 63 ms
7,280 KB
testcase_29 AC 61 ms
7,312 KB
testcase_30 AC 60 ms
7,284 KB
testcase_31 AC 61 ms
7,196 KB
testcase_32 AC 61 ms
7,484 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 7 ms
5,376 KB
testcase_36 AC 7 ms
5,376 KB
testcase_37 AC 7 ms
5,376 KB
testcase_38 AC 6 ms
5,376 KB
testcase_39 AC 6 ms
5,376 KB
testcase_40 AC 7 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 7 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 3 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 3 ms
5,376 KB
testcase_52 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct node{
    int sz;
    list<int> edge;
    node(){sz=1;};
};
bool check(int r,int c,int N,int M){
    if(0<=r&&r<N)if(0<=c&&c<M)return true;
    return false;
}
int dir4[]={-1,0,1,0,-1};
int dir8[]={0,-1,-1,0,1,-1,1,1,0};
#define mp make_pair
int main() {
    int N,M;cin>>N>>M;N+=2,M+=2;
    vector<string> grid(N);
    using VB=vector<bool>;
    using VVB=vector<VB>;
    VVB used(N,VB(M,false));
    vector<node> tree(1);tree[0].sz=0;
    for(int i = 0; i < M; i++)grid[0]+='.',grid[N-1]+='.';
    for(int i=1;i<N;i++){
        cin>>grid[i];
        grid[i]="."+grid[i]+".";
    }
    int D=0;
    std::function<void(int,int,int)> dfs=[&](int r,int c,int now){
        D++;
        used[r][c]=true;
        stack<pair<int,int>> W;
        vector<pair<int,int>> X;
        W.push(mp(r,c));
        while(W.empty()==false){
            r=W.top().first;c=W.top().second;W.pop();

            for(int i = 0; i < 4; i++){
                int nr=r+dir4[i],nc=c+dir4[i+1];
                if(check(nr,nc,N,M)&&used[nr][nc]==false){
                    used[nr][nc]=true;
                    if(grid[nr][nc]=='.')W.push(mp(nr,nc));
                    else X.push_back(mp(nr,nc));
                }
            }
        }
        for(auto& it : X){
            r=it.first,c=it.second;
            used[r][c]=false;
        }
        for(auto& it : X){
            r=it.first,c=it.second;
            if(used[r][c]==false){
                tree[now].edge.push_back(tree.size());
                tree.push_back(node());
                W.push(mp(r,c));
                used[r][c]=true;
                while(W.empty()==false){

                    r=W.top().first;c=W.top().second;W.pop();
                    grid[r][c]='0'+D;
                    for(int i = 0; i < 8; i++){
                        int nr=r+dir8[i],nc=c+dir8[i+1];
                        if(check(nr,nc,N,M)&&used[nr][nc]==false&&grid[nr][nc]=='x'){
                            tree.back().sz++;
                            used[nr][nc]=true;
                            W.push(mp(nr,nc));
                        }
                    }
                }
                for(int i = 0; i < 4; i++){
                    int nr=r+dir4[i],nc=c+dir4[i+1];
                    if(check(nr,nc,N,M)&&used[nr][nc]==false){
                        dfs(nr,nc,tree.size()-1);
                    }
                }
            }
        }
        D--;
    };
    dfs(0,0,0);

    int Z=tree.size();
    vector<pair<int,int>> dp(Z,mp(0,0));
    std::function<void(int)> dfs2=[&](int v){
        dp[v].first=tree[v].sz;
        dp[v].second=0;
        for(auto& u : tree[v].edge){
            dfs2(u);
            dp[v].first+=dp[u].second;
            dp[v].second+=max(dp[u].first,dp[u].second);
        }
    };
    //for(auto& it : grid){cout << it << endl;}
    dfs2(0);
    cout<<max(dp[0].first,dp[0].second)<<endl;
    return 0;
}
0