結果

問題 No.348 カゴメカゴメ
ユーザー convexineqconvexineq
提出日時 2021-02-01 18:55:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,383 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 180,616 KB
最終ジャッジ日時 2023-09-12 10:18:11
合計ジャッジ時間 12,508 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 430 ms
180,616 KB
testcase_03 WA -
testcase_04 AC 81 ms
76,584 KB
testcase_05 AC 83 ms
76,408 KB
testcase_06 AC 75 ms
71,580 KB
testcase_07 AC 73 ms
71,088 KB
testcase_08 AC 75 ms
71,392 KB
testcase_09 AC 75 ms
71,116 KB
testcase_10 AC 75 ms
71,376 KB
testcase_11 AC 74 ms
71,272 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 280 ms
127,164 KB
testcase_16 AC 74 ms
71,404 KB
testcase_17 AC 74 ms
71,236 KB
testcase_18 AC 74 ms
71,280 KB
testcase_19 AC 74 ms
71,344 KB
testcase_20 AC 81 ms
76,392 KB
testcase_21 AC 77 ms
71,468 KB
testcase_22 AC 74 ms
71,624 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 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
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,*b = open(0).read().split()
n,m = int(n),int(m)
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(i,j):
    C = i*m+j
    q = [i*m+j]
    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)]:
            if 0 <= ni < n and 0 <= nj < m and used[ni][nj] == 0:
                used[ni][nj] = 1
                nx = ni*m+nj
                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]*m for _ in range(n)]
parent = {N:-1}
for i in range(n):
    for j in range(m):
        if used[i][j]==0 and b[i][j] == ".":
            used[i][j] = 1
            dfs(i,j)

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

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