結果

問題 No.348 カゴメカゴメ
ユーザー convexineqconvexineq
提出日時 2021-02-01 19:15:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,346 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 182,772 KB
最終ジャッジ日時 2023-09-12 10:33:48
合計ジャッジ時間 13,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 429 ms
182,772 KB
testcase_03 WA -
testcase_04 AC 83 ms
76,528 KB
testcase_05 AC 86 ms
76,312 KB
testcase_06 AC 76 ms
71,596 KB
testcase_07 AC 74 ms
71,240 KB
testcase_08 AC 76 ms
71,416 KB
testcase_09 AC 77 ms
71,400 KB
testcase_10 AC 75 ms
71,240 KB
testcase_11 AC 74 ms
71,408 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 278 ms
130,400 KB
testcase_16 AC 74 ms
71,244 KB
testcase_17 AC 74 ms
71,376 KB
testcase_18 AC 73 ms
71,264 KB
testcase_19 AC 72 ms
71,100 KB
testcase_20 AC 83 ms
76,164 KB
testcase_21 AC 76 ms
71,512 KB
testcase_22 AC 73 ms
71,416 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(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