結果

問題 No.2696 Sign Creation
ユーザー FromBooskaFromBooska
提出日時 2024-03-23 17:43:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,443 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,548 KB
実行使用メモリ 90,288 KB
最終ジャッジ日時 2024-05-17 18:24:46
合計ジャッジ時間 8,610 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
61,988 KB
testcase_01 AC 49 ms
63,440 KB
testcase_02 AC 46 ms
55,688 KB
testcase_03 AC 44 ms
55,636 KB
testcase_04 AC 44 ms
55,820 KB
testcase_05 AC 43 ms
56,632 KB
testcase_06 WA -
testcase_07 AC 45 ms
55,996 KB
testcase_08 AC 142 ms
77,840 KB
testcase_09 WA -
testcase_10 AC 154 ms
78,256 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 455 ms
81,836 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 504 ms
81,912 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 128 ms
77,684 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 266 ms
78,752 KB
testcase_28 WA -
testcase_29 AC 206 ms
78,008 KB
testcase_30 WA -
testcase_31 AC 469 ms
90,288 KB
testcase_32 AC 48 ms
62,268 KB
testcase_33 AC 70 ms
73,212 KB
testcase_34 AC 46 ms
56,332 KB
testcase_35 AC 43 ms
55,780 KB
testcase_36 AC 43 ms
56,580 KB
testcase_37 AC 42 ms
54,996 KB
testcase_38 AC 42 ms
55,568 KB
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

# D小さい、HW積小さい、Union Findでまず星をつなげて、それから空きマスからルート探索
# 最小値最大値を求めるのが面倒

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
 
    def size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
 
    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

H, W, N, D = map(int, input().split())
board = [[-1]*W for i in range(H)]
XY = []
for i in range(N):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    XY.append((x, y))
    board[x][y] = i
    
from collections import defaultdict
UF = UnionFind(N)
for h in range(H):
    for w in range(W):
        if board[h][w]>=0:
            for dh in range(max(0, h-D), min(H, h+D+1)):
                for dw in range(max(0, w-D), min(W, w+D+1)):
                    if (dh, dw)==(h, w):
                        continue
                    if board[dh][dw]>=0:
                        UF.unite(board[h][w], board[dh][dw])

initial_count = 0
for members in UF.all_group_members().values():
    if len(members)>=2:
        initial_count += 1
                        
mn = initial_count
mx = initial_count
for h in range(H):
    for w in range(W):
        if board[h][w]==-1:
            seiza = 0
            star = 0
            root_visited = set()
            for dh in range(max(0, h-D), min(H, h+D+1)):
                for dw in range(max(0, w-D), min(W, w+D+1)):
                    if (dh, dw)==(h, w):
                        continue
                    
                    if board[dh][dw]>=0:
                        r = UF.find(board[dh][dw])
                        if r not in root_visited:
                            root_visited.add(r)
                            if UF.size(r) == 1:
                                star += 1
                            elif UF.size(r) >= 2:
                                seiza += 1
            #print('h', h, 'w', w, 'calc', calc)
            if seiza == 0:
                if star <= 1:
                    pass
                elif star >= 2:
                    mn = min(mn, initial_count+1)
                    mx = max(mx, initial_count+1)
            elif seiza == 1:
                pass
            elif seiza >= 2:
                mn = min(mn, initial_count-(seiza-1))
                mx = max(mx, initial_count-(seiza-1))
            
print(mn, mx)



0