結果

問題 No.348 カゴメカゴメ
ユーザー mayoko_mayoko_
提出日時 2016-02-27 14:14:12
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,437 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 105,684 KB
実行使用メモリ 56,884 KB
最終ジャッジ日時 2023-10-24 18:32:12
合計ジャッジ時間 6,935 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 12 ms
35,560 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 12 ms
35,596 KB
testcase_05 AC 11 ms
35,584 KB
testcase_06 AC 10 ms
35,540 KB
testcase_07 AC 10 ms
35,540 KB
testcase_08 AC 10 ms
35,540 KB
testcase_09 AC 10 ms
35,544 KB
testcase_10 AC 10 ms
35,540 KB
testcase_11 AC 10 ms
35,540 KB
testcase_12 AC 13 ms
35,840 KB
testcase_13 AC 10 ms
35,668 KB
testcase_14 AC 10 ms
35,568 KB
testcase_15 WA -
testcase_16 AC 11 ms
35,540 KB
testcase_17 AC 9 ms
35,540 KB
testcase_18 AC 9 ms
35,540 KB
testcase_19 AC 9 ms
35,540 KB
testcase_20 AC 9 ms
35,552 KB
testcase_21 AC 9 ms
35,544 KB
testcase_22 AC 10 ms
35,540 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 29 ms
36,208 KB
testcase_34 AC 25 ms
36,184 KB
testcase_35 AC 25 ms
36,204 KB
testcase_36 AC 30 ms
36,272 KB
testcase_37 AC 27 ms
36,204 KB
testcase_38 AC 26 ms
36,176 KB
testcase_39 AC 26 ms
36,220 KB
testcase_40 AC 26 ms
36,188 KB
testcase_41 AC 26 ms
36,212 KB
testcase_42 AC 25 ms
36,272 KB
testcase_43 AC 12 ms
35,688 KB
testcase_44 AC 12 ms
35,692 KB
testcase_45 AC 12 ms
35,688 KB
testcase_46 AC 11 ms
35,696 KB
testcase_47 AC 12 ms
35,696 KB
testcase_48 AC 11 ms
35,704 KB
testcase_49 AC 12 ms
35,700 KB
testcase_50 AC 13 ms
35,704 KB
testcase_51 AC 16 ms
35,688 KB
testcase_52 AC 13 ms
35,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

struct UnionFind {
    vector<int> par;
    int n, cnt;
    UnionFind(const int& x = 0) {init(x);}
    void init(const int& x) {par.assign(cnt=n=x, -1);}
    inline int find(const int& x) {return par[x] < 0 ? x : par[x] = find(par[x]);}
    inline bool same(const int& x, const int& y) {return find(x) == find(y);}
    inline bool unite(int x, int y) {
        if ((x = find(x)) == (y = find(y))) return false;
        --cnt;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    inline int count() const {return cnt;}
    inline int count(int x) {return -par[find(x)];}
};

const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddx[] = {1, 0, -1, 0};
const int ddy[] = {0, 1, 0, -1};

string grid[1010];
bool done[1030*1030];
vector<int> G[1010*1010];
int dp[1010*1010][2];

// flag: 1 ならそのスコアを加算しても良い
// flag: 0 ならスコアを加算しちゃダメ
int dfs(int v, int flag, const vector<int>& score) {
    int& ret = dp[v][flag];
    if (ret >= 0) return ret;
    ret = 0;
    if (score[v] == 0) {
        for (int ch : G[v]) {
            ret += dfs(ch, flag, score);
        }
    } else {
        if (flag == 0) {
            for (int ch : G[v]) {
                ret += dfs(ch, 1, score);
            }
        } else {
            ret = score[v];
            for (int ch : G[v]) {
                ret += dfs(ch, 0, score);
            }
            int tmp = 0;
            for (int ch : G[v]) {
                tmp += dfs(ch, 1, score);
            }
            ret = max(tmp, ret);
        }
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    // 周りを . で囲みつつ input
    for (int i = 0; i < M+2; i++) grid[0] += '.';
    for (int i = 1; i <= N; i++) {
        cin >> grid[i];
        grid[i] = "." + grid[i] + ".";
    }
    for (int i = 0; i < M+2; i++) grid[0] += '.';
    N += 2; M += 2;
    UnionFind uf(N*M);
    // 'x'
    for (int y = 0; y < N; y++) for (int x = 0; x < M; x++) {
        if (grid[y][x] != 'x') continue;
        for (int k = 0; k < 8; k++) {
            int ny = y+dy[k], nx = x+dx[k];
            if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
            if (grid[ny][nx] == 'x') uf.unite(y*M+x, ny*M+nx);
        }
    }
    // '.'
    for (int y = 0; y < N; y++) for (int x = 0; x < M; x++) {
        if (grid[y][x] == 'x') continue;
        for (int k = 0; k < 4; k++) {
            int ny = y+ddy[k], nx = x+ddx[k];
            if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
            if (grid[ny][nx] != 'x') uf.unite(y*M+x, ny*M+nx);
        }
    }
    // 木を作っていく
    map<int, int> trans;
    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) {
        trans[uf.find(i*M+j)] = 0;
    }
    int k = 0;
    for (auto& p : trans) p.second = k++;
    vector<int> score(k);
    for (auto p : trans) {
        int y = p.first/M, x = p.first%M;
        if (grid[y][x] == '.') score[p.second] = 0;
        else score[p.second] = uf.count(p.first);
    }
    vector<vi> group(k);
    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) {
        group[trans[uf.find(i*M+j)]].push_back(i*M+j);
    }
    // 木を作る dfs
    function<void(int)> f = [&] (int v) {
        done[v] = true;
        for (int el : group[v]) {
            int y = el/M, x = el%M;
            for (int k = 0; k < 4; k++) {
                int ny = y+ddy[k], nx = x+ddx[k];
                if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
                int next = trans[uf.find(ny*M+nx)];
                if (done[next]) continue;
                G[v].push_back(next);
                f(next);
            }
        }
    };
    f(0);
    // 木DP
    memset(dp, -1, sizeof(dp));
    cout << dfs(trans[0], 1, score) << endl;
    return 0;
}
0