結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
126,648 KB
testcase_01 AC 64 ms
68,736 KB
testcase_02 AC 959 ms
182,940 KB
testcase_03 AC 480 ms
126,964 KB
testcase_04 AC 52 ms
64,896 KB
testcase_05 AC 54 ms
67,328 KB
testcase_06 AC 41 ms
53,888 KB
testcase_07 AC 40 ms
54,400 KB
testcase_08 AC 40 ms
53,888 KB
testcase_09 AC 44 ms
55,040 KB
testcase_10 AC 44 ms
54,400 KB
testcase_11 AC 43 ms
54,400 KB
testcase_12 AC 93 ms
77,824 KB
testcase_13 AC 94 ms
77,212 KB
testcase_14 AC 68 ms
72,832 KB
testcase_15 AC 237 ms
126,960 KB
testcase_16 AC 42 ms
53,888 KB
testcase_17 AC 42 ms
54,144 KB
testcase_18 AC 41 ms
54,272 KB
testcase_19 AC 40 ms
54,400 KB
testcase_20 AC 57 ms
64,768 KB
testcase_21 AC 43 ms
54,656 KB
testcase_22 AC 42 ms
54,144 KB
testcase_23 AC 399 ms
126,820 KB
testcase_24 AC 408 ms
114,668 KB
testcase_25 AC 413 ms
126,780 KB
testcase_26 AC 408 ms
126,964 KB
testcase_27 AC 392 ms
126,752 KB
testcase_28 AC 403 ms
126,660 KB
testcase_29 AC 452 ms
126,700 KB
testcase_30 AC 406 ms
126,888 KB
testcase_31 AC 411 ms
126,960 KB
testcase_32 AC 416 ms
126,704 KB
testcase_33 AC 138 ms
81,536 KB
testcase_34 AC 128 ms
81,664 KB
testcase_35 AC 139 ms
81,536 KB
testcase_36 AC 139 ms
81,280 KB
testcase_37 AC 130 ms
81,280 KB
testcase_38 AC 137 ms
81,496 KB
testcase_39 AC 135 ms
81,528 KB
testcase_40 AC 139 ms
81,536 KB
testcase_41 AC 140 ms
81,524 KB
testcase_42 AC 134 ms
81,408 KB
testcase_43 AC 94 ms
77,184 KB
testcase_44 AC 92 ms
76,672 KB
testcase_45 AC 94 ms
76,800 KB
testcase_46 AC 91 ms
77,156 KB
testcase_47 AC 92 ms
77,116 KB
testcase_48 AC 84 ms
76,928 KB
testcase_49 AC 101 ms
77,056 KB
testcase_50 AC 95 ms
77,020 KB
testcase_51 AC 92 ms
76,800 KB
testcase_52 AC 95 ms
76,928 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