結果

問題 No.348 カゴメカゴメ
ユーザー kyunakyuna
提出日時 2020-02-27 07:03:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 1,759 bytes
コンパイル時間 1,203 ms
コンパイル使用メモリ 92,532 KB
実行使用メモリ 215,296 KB
最終ジャッジ日時 2024-04-21 16:46:26
合計ジャッジ時間 5,928 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
62,848 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 500 ms
141,900 KB
testcase_03 AC 121 ms
12,288 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 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 2 ms
5,376 KB
testcase_12 AC 7 ms
7,552 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 213 ms
215,296 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 1 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 166 ms
65,152 KB
testcase_24 AC 160 ms
60,612 KB
testcase_25 AC 163 ms
67,296 KB
testcase_26 AC 164 ms
66,600 KB
testcase_27 AC 155 ms
61,020 KB
testcase_28 AC 162 ms
64,168 KB
testcase_29 AC 164 ms
67,556 KB
testcase_30 AC 167 ms
65,472 KB
testcase_31 AC 161 ms
60,876 KB
testcase_32 AC 180 ms
66,012 KB
testcase_33 AC 15 ms
9,984 KB
testcase_34 AC 14 ms
8,960 KB
testcase_35 AC 14 ms
8,960 KB
testcase_36 AC 15 ms
9,344 KB
testcase_37 AC 15 ms
9,344 KB
testcase_38 AC 15 ms
9,088 KB
testcase_39 AC 15 ms
8,960 KB
testcase_40 AC 16 ms
9,600 KB
testcase_41 AC 15 ms
8,960 KB
testcase_42 AC 13 ms
8,704 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 3 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 <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (auto &vi: vec) os << vi << " "; return os; }

int main() {
    int h, w; cin >> h >> w;
    vector<string> s(h);
    for (auto &si: s) cin >> si;
    vector<string> field(h + 2, string(w + 2, '.'));    // .x
    for (int i = 0; i < h; i++) field[i + 1] = '.' + s[i] + '.';
    h += 2, w += 2;
    vector<vector<int>> visited(h, vector<int>(w, -1));
    vector<set<int>> g;
    auto dfs = [&](auto dfs, int i, int j, int id, int &cnt) {
        if (~visited[i][j]) return ;
        visited[i][j] = id, cnt++;
        for (int di: {-1, 0, 1}) for (int dj: {-1, 0, 1}) {
            if (abs(di) + abs(dj) > 1 + (field[i][j] == 'x')) continue;
            int ni = i + di, nj = j + dj;
            if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
            if (field[ni][nj] == field[i][j]) dfs(dfs, ni, nj, id, cnt);
            else if (~visited[ni][nj]) g[visited[ni][nj]].emplace(id);
        }
    };
    int id = 0;
    vector<int> num;
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
        if (visited[i][j] == -1) {
            int cnt = 0;
            dfs(dfs, i, j, id, cnt);
            num.emplace_back(cnt);
            g.resize(++id);
        }
    }
    vector<vector<int>> dp(id, vector<int>(2));
    auto dfs2 = [&](auto dfs2, int u) -> int {
        dp[u][1] = num[u];
        for (int v: g[u]) for (int w: g[v]) {
            dp[u][0] += dfs2(dfs2, w);
            dp[u][1] += dp[w][0];
        }
        return max(dp[u][0], dp[u][1]);
    };
    int ans = 0;
    for (int u: g[0]) ans += dfs2(dfs2, u);
    cout << ans << endl;
    return 0;
}
0