結果

問題 No.348 カゴメカゴメ
ユーザー convexineqconvexineq
提出日時 2021-02-01 19:56:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 487 ms / 2,000 ms
コード長 2,428 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 183,448 KB
最終ジャッジ日時 2023-09-12 10:40:12
合計ジャッジ時間 13,537 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 445 ms
110,376 KB
testcase_01 AC 94 ms
76,704 KB
testcase_02 AC 455 ms
183,448 KB
testcase_03 AC 487 ms
102,812 KB
testcase_04 AC 87 ms
76,640 KB
testcase_05 AC 102 ms
76,692 KB
testcase_06 AC 77 ms
71,172 KB
testcase_07 AC 74 ms
71,012 KB
testcase_08 AC 73 ms
71,032 KB
testcase_09 AC 78 ms
71,392 KB
testcase_10 AC 77 ms
71,064 KB
testcase_11 AC 77 ms
71,268 KB
testcase_12 AC 127 ms
78,444 KB
testcase_13 AC 120 ms
77,912 KB
testcase_14 AC 102 ms
77,356 KB
testcase_15 AC 296 ms
128,788 KB
testcase_16 AC 77 ms
71,264 KB
testcase_17 AC 77 ms
71,180 KB
testcase_18 AC 74 ms
71,352 KB
testcase_19 AC 75 ms
71,380 KB
testcase_20 AC 89 ms
76,444 KB
testcase_21 AC 77 ms
71,404 KB
testcase_22 AC 75 ms
71,384 KB
testcase_23 AC 453 ms
106,540 KB
testcase_24 AC 452 ms
107,272 KB
testcase_25 AC 453 ms
106,724 KB
testcase_26 AC 474 ms
106,728 KB
testcase_27 AC 456 ms
107,432 KB
testcase_28 AC 461 ms
106,604 KB
testcase_29 AC 455 ms
106,020 KB
testcase_30 AC 461 ms
106,752 KB
testcase_31 AC 454 ms
107,180 KB
testcase_32 AC 454 ms
107,084 KB
testcase_33 AC 169 ms
80,172 KB
testcase_34 AC 168 ms
80,472 KB
testcase_35 AC 169 ms
80,416 KB
testcase_36 AC 166 ms
80,148 KB
testcase_37 AC 170 ms
80,520 KB
testcase_38 AC 165 ms
80,680 KB
testcase_39 AC 169 ms
80,688 KB
testcase_40 AC 173 ms
80,912 KB
testcase_41 AC 165 ms
80,360 KB
testcase_42 AC 169 ms
80,636 KB
testcase_43 AC 130 ms
78,048 KB
testcase_44 AC 126 ms
77,924 KB
testcase_45 AC 133 ms
78,124 KB
testcase_46 AC 123 ms
77,828 KB
testcase_47 AC 128 ms
78,040 KB
testcase_48 AC 126 ms
78,072 KB
testcase_49 AC 134 ms
78,140 KB
testcase_50 AC 130 ms
78,280 KB
testcase_51 AC 130 ms
78,164 KB
testcase_52 AC 124 ms
77,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

n,m = map(int,input().split())
b = ["."*(m+2)]
for i in range(n):
    b.append("."+input()+".")
b.append("."*(m+2))
n += 2
m += 2
N = n*m
UF = UnionFind(N+1)
for i in range(n):
    for j in range(m):
        if b[i][j] == "x":
            for ni,nj in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1)]:
                if 0 <= ni < n and 0 <= nj < m and b[ni][nj] == "x":
                    UF.merge(i*m+j,ni*m+nj)

def dfs(X):
    q = [X]
    outer = N
    inner = set()
    while q:
        x = q.pop()
        i,j = x//m, x%m
        for ni,nj in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
            nx = ni*m+nj
            if 0 <= ni < n and 0 <= nj < m:
                if b[ni][nj] == ".":
                    if used[nx]==0:
                        used[nx] = 1
                        q.append(nx)
                else:
                    nr = UF.root(nx)
                    if nr in parent:
                        outer = nr
                    else:
                        inner.add(nr)
    for x in inner:
        parent[x] = outer

used = [0]*N
parent = {N:-1}
for i in range(n):
    for j in range(m):
        x = i*m+j
        if used[x]==0 and b[i][j] == ".":
            used[x] = 1
            dfs(x)

P = list(parent.keys())
dpnot = {i:0 for i in P}
dpuse = {i:UF.getsize(i) for i in P}
dpuse[N] = 0
dpuse[-1] = dpnot[-1] = 0
for v in P[::-1]:
    p = parent[v]
    dpuse[p] += dpnot[v]
    dpnot[p] += max(dpuse[v],dpnot[v])

print(max(dpuse[N],dpnot[N]))
0