結果

問題 No.348 カゴメカゴメ
ユーザー maspymaspy
提出日時 2020-04-16 00:10:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 758 ms / 2,000 ms
コード長 2,156 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 182,836 KB
最終ジャッジ日時 2024-04-09 19:05:34
合計ジャッジ時間 10,940 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 316 ms
126,796 KB
testcase_01 AC 57 ms
69,992 KB
testcase_02 AC 758 ms
182,836 KB
testcase_03 AC 425 ms
126,656 KB
testcase_04 AC 49 ms
65,300 KB
testcase_05 AC 52 ms
67,312 KB
testcase_06 AC 38 ms
55,168 KB
testcase_07 AC 37 ms
54,468 KB
testcase_08 AC 37 ms
54,524 KB
testcase_09 AC 39 ms
55,924 KB
testcase_10 AC 39 ms
54,768 KB
testcase_11 AC 37 ms
55,968 KB
testcase_12 AC 87 ms
77,740 KB
testcase_13 AC 87 ms
76,928 KB
testcase_14 AC 64 ms
74,344 KB
testcase_15 AC 222 ms
126,960 KB
testcase_16 AC 39 ms
54,172 KB
testcase_17 AC 39 ms
54,176 KB
testcase_18 AC 38 ms
54,708 KB
testcase_19 AC 36 ms
55,248 KB
testcase_20 AC 49 ms
66,680 KB
testcase_21 AC 37 ms
55,308 KB
testcase_22 AC 36 ms
55,336 KB
testcase_23 AC 361 ms
126,548 KB
testcase_24 AC 367 ms
115,052 KB
testcase_25 AC 362 ms
126,736 KB
testcase_26 AC 358 ms
126,812 KB
testcase_27 AC 365 ms
127,420 KB
testcase_28 AC 364 ms
126,980 KB
testcase_29 AC 362 ms
127,292 KB
testcase_30 AC 375 ms
126,796 KB
testcase_31 AC 364 ms
127,288 KB
testcase_32 AC 365 ms
127,664 KB
testcase_33 AC 127 ms
81,668 KB
testcase_34 AC 116 ms
81,528 KB
testcase_35 AC 127 ms
81,512 KB
testcase_36 AC 127 ms
81,560 KB
testcase_37 AC 121 ms
81,724 KB
testcase_38 AC 124 ms
81,544 KB
testcase_39 AC 132 ms
81,452 KB
testcase_40 AC 134 ms
81,452 KB
testcase_41 AC 134 ms
81,328 KB
testcase_42 AC 129 ms
81,432 KB
testcase_43 AC 84 ms
77,272 KB
testcase_44 AC 80 ms
76,724 KB
testcase_45 AC 87 ms
77,196 KB
testcase_46 AC 84 ms
77,216 KB
testcase_47 AC 85 ms
77,192 KB
testcase_48 AC 79 ms
76,900 KB
testcase_49 AC 95 ms
77,016 KB
testcase_50 AC 87 ms
76,876 KB
testcase_51 AC 89 ms
77,276 KB
testcase_52 AC 88 ms
77,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict

H, W = map(int, readline().split())
A = [-1] * (W + 4)
A += [-1] + [0] * (W + 2) + [-1]
for _ in range(H):
    A += [-1, 0] + [1 * (x == ord('x')) for x in readline().rstrip()] + [0, -1]
A += [-1] + [0] * (W + 2) + [-1]
A += [-1] * (W + 4)

H += 4
W += 4
N = H * W

root = [0] * N
for v in range(N):
    if A[v] or root[v]:
        continue
    stack = [v]
    root[v] = v
    while stack:
        v = stack.pop()
        for dx in (1, -1, W, -W):
            w = v + dx
            if A[w] or root[w]:
                continue
            root[w] = root[v]
            stack.append(w)

component_size = defaultdict(int)
for v in range(N):
    if A[v] != 1:
        continue
    nbd0 = []
    nbdx = []
    for dx in (1, -1, W - 1, W, W + 1, -W - 1, -W, -W + 1):
        w = v + dx
        if A[w]:
            nbdx.append(w)
            continue
        r = root[w]
        if not nbd0:
            nbd0.append(r)
        elif nbd0[0] != r and nbd0[-1] != r:
            nbd0.append(r)
    if len(nbd0) == 1:
        # 三角形
        new_node = sum(pow(v, 10, 10 ** 9 + 7) for v in [v] + nbdx)
        nbd0.append(new_node)
    key = tuple(sorted(nbd0))
    component_size[key] += 1

nodes = []

for (u, v) in component_size:
    nodes.append(u)
    nodes.append(v)

nodes = sorted(set(nodes))
n_to_i = {n: i for i, n in enumerate(nodes)}

N = len(nodes)
if N == 0:
    print(0)
    exit()

graph = [[] for _ in range(N)]
for (u, v), x in component_size.items():
    u = n_to_i[u]
    v = n_to_i[v]
    graph[u].append((v, x))
    graph[v].append((u, x))

# あとは 木 dp
root = 0
parent = [0] * N
cost = [0] * N
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y, c in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        cost[y] = c
        stack.append(y)

dp0 = [0] * N
dp1 = [0] * N
for v in order[::-1][:-1]:
    p = parent[v]
    x = cost[v]
    dp0[p] += dp1[v]
    dp1[p] += max(dp0[v] + x, dp1[v])

print(dp1[0])
0