結果

問題 No.348 カゴメカゴメ
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-22 13:01:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 181,200 KB
実行使用メモリ 25,488 KB
最終ジャッジ日時 2023-09-28 21:16:45
合計ジャッジ時間 7,311 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
9,388 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 395 ms
25,488 KB
testcase_03 AC 85 ms
9,392 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 4 ms
7,324 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 57 ms
9,276 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 124 ms
10,976 KB
testcase_24 AC 123 ms
11,192 KB
testcase_25 AC 124 ms
11,212 KB
testcase_26 AC 124 ms
11,276 KB
testcase_27 AC 122 ms
11,200 KB
testcase_28 AC 123 ms
11,324 KB
testcase_29 AC 124 ms
11,200 KB
testcase_30 AC 125 ms
11,204 KB
testcase_31 AC 122 ms
11,204 KB
testcase_32 AC 124 ms
11,352 KB
testcase_33 AC 11 ms
4,532 KB
testcase_34 AC 11 ms
4,512 KB
testcase_35 AC 11 ms
4,792 KB
testcase_36 AC 11 ms
4,632 KB
testcase_37 AC 12 ms
4,504 KB
testcase_38 AC 11 ms
4,504 KB
testcase_39 AC 11 ms
4,536 KB
testcase_40 AC 11 ms
4,788 KB
testcase_41 AC 11 ms
4,668 KB
testcase_42 AC 11 ms
4,504 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 4 ms
4,380 KB
testcase_45 AC 3 ms
4,380 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 4 ms
4,380 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 4 ms
4,380 KB
testcase_51 AC 3 ms
4,384 KB
testcase_52 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)



int H, W; string G[1010];
//-----------------------------------------------------------------------------------
int dx[8] = { 0,1,1,1,0,-1,-1,-1 };
int dy[8] = { -1,-1,0,1,1,1,0,-1 };
int C[1010][1010];
vector<int> T, cnt;
vector<set<int>> E;
void makeTree() {
    rep(y, 0, H) rep(x, 0, W) C[y][x] = -1;
    int c = 0;
    rep(y, 0, H) rep(x, 0, W) if (C[y][x] < 0) {
        int d;

        if (G[y][x] == '.') T.push_back(0), d = 2;
        else T.push_back(1) , d = 1;

        queue<int> que;
        que.push(y * 1010 + x);
        C[y][x] = c;
        cnt.push_back(1);
        while (!que.empty()) {
            int q = que.front(); que.pop();
            int xx = q % 1010;
            int yy = q / 1010;
            for (int i = 0; i < 8; i += d) {
                int xxx = xx + dx[i];
                int yyy = yy + dy[i];
                if (xxx < 0 || W <= xxx) continue;
                if (yyy < 0 || H <= yyy) continue;
                if (G[y][x] != G[yyy][xxx]) continue;
                if (0 <= C[yyy][xxx]) continue;

                C[yyy][xxx] = c; cnt[c]++;
                que.push(yyy * 1010 + xxx);
            }
        }

        c++;
    }

    E.resize(c);

    rep(y, 0, H) rep(x, 0, W) {
        rep(i, 0, 4) {
            int xx = x + dx[i * 2];
            int yy = y + dy[i * 2];
            if (xx < 0 || W <= xx) continue;
            if (yy < 0 || H <= yy) continue;
            if (C[yy][xx] == C[y][x]) continue;

            int ca = C[y][x];
            int cb = C[yy][xx];

            E[ca].insert(cb);
            E[cb].insert(ca);
        }
    }
}
//-----------------------------------------------------------------------------------
pair<int, int> dfs(int cu, int pa = -1) {
    if (T[cu] == 0) { // ="."
        int a = 0, b = 0;
        for (int to : E[cu]) if (to != pa) {
            auto p = dfs(to, cu);
            a += max(p.second, p.first);
            b += p.second;
        }
        return { a, b };
    } else { // ="x"
        int a = cnt[cu], b = 0;
        for (int to : E[cu]) if (to != pa) {
            auto p = dfs(to, cu);
            a += p.second;
            b += max(p.first, p.second);
        }
        return { a,b };
    }
}
//-----------------------------------------------------------------------------------
int main() {
    cin >> H >> W;
    rep(y, 1, H + 1) cin >> G[y];
    rep(x, 0, W + 2) G[0] += ".";
    rep(y, 1, H + 1) G[y] = "." + G[y] + ".";
    rep(x, 0, W + 2) G[H + 1] += ".";
    W += 2; H += 2;

    makeTree();

    auto p = dfs(0);
    int ans = max(p.first, p.second);
    cout << ans << endl;
}
0