結果

問題 No.348 カゴメカゴメ
ユーザー convexineqconvexineq
提出日時 2021-02-01 19:34:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,425 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 86,788 KB
実行使用メモリ 169,536 KB
最終ジャッジ日時 2023-09-12 10:38:55
合計ジャッジ時間 13,471 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 AC 398 ms
169,536 KB
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 69 ms
71,424 KB
testcase_08 AC 71 ms
71,456 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 283 ms
128,896 KB
testcase_16 AC 71 ms
71,572 KB
testcase_17 RE -
testcase_18 AC 72 ms
71,272 KB
testcase_19 AC 71 ms
71,116 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 70 ms
71,384 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 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

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(m):
    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 and used[nx] == 0:
                used[nx] = 1
                if b[ni][nj] == ".":
                    q.append(nx)
                else:
                    nr = UF.root(nx)
                    if nr in parent:
                        outer = nr
                    else:
                        inner.add(nr)
    #print(C,outer,inner)
    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