結果

問題 No.348 カゴメカゴメ
ユーザー ctyl_0ctyl_0
提出日時 2016-02-27 06:38:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 4,907 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 105,792 KB
実行使用メモリ 130,132 KB
最終ジャッジ日時 2023-10-24 18:24:23
合計ジャッジ時間 6,280 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 175 ms
127,588 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 189 ms
130,132 KB
testcase_03 AC 184 ms
128,336 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 7 ms
6,956 KB
testcase_13 AC 4 ms
4,524 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 157 ms
127,580 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 1 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,372 KB
testcase_23 AC 196 ms
128,460 KB
testcase_24 AC 197 ms
128,396 KB
testcase_25 AC 196 ms
128,316 KB
testcase_26 AC 197 ms
128,260 KB
testcase_27 AC 196 ms
128,524 KB
testcase_28 AC 197 ms
128,240 KB
testcase_29 AC 198 ms
128,428 KB
testcase_30 AC 197 ms
128,412 KB
testcase_31 AC 197 ms
128,276 KB
testcase_32 AC 198 ms
128,208 KB
testcase_33 AC 19 ms
13,500 KB
testcase_34 AC 19 ms
13,500 KB
testcase_35 AC 19 ms
13,500 KB
testcase_36 AC 19 ms
13,500 KB
testcase_37 AC 20 ms
13,500 KB
testcase_38 AC 20 ms
13,500 KB
testcase_39 AC 19 ms
13,500 KB
testcase_40 AC 19 ms
13,500 KB
testcase_41 AC 19 ms
13,500 KB
testcase_42 AC 19 ms
13,500 KB
testcase_43 AC 4 ms
4,524 KB
testcase_44 AC 4 ms
4,524 KB
testcase_45 AC 4 ms
4,524 KB
testcase_46 AC 4 ms
4,524 KB
testcase_47 AC 4 ms
4,524 KB
testcase_48 AC 4 ms
4,524 KB
testcase_49 AC 4 ms
4,524 KB
testcase_50 AC 4 ms
4,524 KB
testcase_51 AC 5 ms
4,524 KB
testcase_52 AC 4 ms
4,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

// union find
class union_find {
public:
    int n;
    vector<int> parent, rnk, num;
    
    union_find(int n){
        parent.resize(n);
        rnk.resize(n, 0);
        num.resize(n, 1);
        rep(i, n){
            parent[i] = i;
        }
        this -> n = n;
    }
    
    int root(int x){
        if (parent[x] == x) {
            return x;
        }
        else{
            return root(parent[x]);
        }
    }
    
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if (x == y) {
            return;
        }
        if (rnk[x] < rnk[y]) {
            parent[x] = y;
            num[y] += num[x];
        }
        else{
            parent[y] = x;
            num[x] += num[y];
            if (rnk[x] == rnk[y]) {
                rnk[x]++;
            }
        }
        n--;
    }
    
    bool same(int x, int y){
        return root(x) == root(y);
    }
    
    int count(){
        return n;
    }
    
    int count(int x){
        return num[root(x)];
    }
};


int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int H, W;
vector<int> vis;
vector<vector<int>> G, T, dp;
vector<string> S;

void dfs(int pos, int ring, union_find &uf){ // ring = 1 : 'x', 0: '.'
    vis[pos] = 1;

    for(int c: G[pos]){
        // cout << pos << ", " << c << endl;
        int cx = c % W, cy = c / W;
        if (ring == 1) {
            rep(k, 8){
                if (cx + dx[k] >= 0 && cx + dx[k] < W && cy + dy[k] >= 0 && cy + dy[k] < H) {
                    //cout << c << ": " << (cy + dy[k]) * W + cx + dx[k] << ", " << vis[uf.root((cy + dy[k]) * W + cx + dx[k])] << endl;
                    if (S[cy + dy[k]][cx + dx[k]] == '.' && vis[uf.root((cy + dy[k]) * W + cx + dx[k])] == 0) {
                        T[pos].pb(uf.root((cy + dy[k]) * W + cx + dx[k]));
                        dfs(uf.root((cy + dy[k]) * W + cx + dx[k]), 0, uf);
                    }
                }
                
            }
        }
        else{
            rep(k, 4){
                if (cx + dx[k] >= 0 && cx + dx[k] < W && cy + dy[k] >= 0 && cy + dy[k] < H) {
                    //cout << c << ": " << (cy + dy[k]) * W + cx + dx[k] << endl;
                    if (S[cy + dy[k]][cx + dx[k]] == 'x' && vis[uf.root((cy + dy[k]) * W + cx + dx[k])] == 0) {
                        T[pos].pb(uf.root((cy + dy[k]) * W + cx + dx[k]));
                        dfs(uf.root((cy + dy[k]) * W + cx + dx[k]), 1, uf);
                    }
                }
                
            }
        }
    }
}

void dfs_m(int now, union_find &uf){ // 一番浅いところにあるxの輪すべてについてこの操作を行う メモ化再帰
    dp[now][1] = uf.count(now);
    for(int ch: T[now]){
        for(int chch : T[ch]){
            dfs_m(chch, uf);
            dp[now][0] += max(dp[chch][0], dp[chch][1]);
            dp[now][1] += dp[chch][0];
        }
    }
}



int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    

    cin >> H >> W;
    S.resize(H + 2);
    S[0] = S[H + 1] = string(W + 2, '.');
    rep(i, H){
        cin >> S[i + 1];
        S[i + 1] = '.' + S[i + 1] + '.';
    }
    
    H += 2, W += 2;
    
    union_find uf(H * W);
    rep(y, H) rep(x, W){
        if (S[y][x] == 'x') {
            rep(k, 8){
                if (!(x + dx[k] >= 0 && x + dx[k] < W && y + dy[k] >= 0 && y + dy[k] < H)) continue;
                if (S[y + dy[k]][x + dx[k]] == 'x'){
                    uf.unite(y * W + x, (y + dy[k]) * W + x + dx[k]);
                }
            }
        }
        else{ // '.'
            rep(k, 4){
                if (!(x + dx[k] >= 0 && x + dx[k] < W && y + dy[k] >= 0 && y + dy[k] < H)) continue;
                if (S[y + dy[k]][x + dx[k]] == '.'){
                    uf.unite(y * W + x, (y + dy[k]) * W + x + dx[k]);
                }
            }

        }
    }
    
    vis.assign(H * W, 0), G.resize(H * W), T.resize(H * W), dp.resize(H * W, vector<int>(2));
    
    rep(i, H * W) G[uf.root(i)].pb(i);
    
    dfs(uf.root(0), 0, uf);
    
    int ret = 0;
    for (int x: T[uf.root(0)]) {
        dfs_m(x, uf);
        ret += max(dp[x][0], dp[x][1]);
    }
    
    
    output(ret, 0);
    
    return 0;
}
0