結果

問題 No.2696 Sign Creation
ユーザー FromBooskaFromBooska
提出日時 2024-03-26 15:57:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 613 ms / 2,500 ms
コード長 3,656 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,984 KB
実行使用メモリ 91,444 KB
最終ジャッジ日時 2024-05-17 18:25:38
合計ジャッジ時間 9,174 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
63,412 KB
testcase_01 AC 45 ms
62,420 KB
testcase_02 AC 41 ms
54,940 KB
testcase_03 AC 42 ms
56,404 KB
testcase_04 AC 40 ms
56,260 KB
testcase_05 AC 38 ms
57,156 KB
testcase_06 AC 39 ms
55,800 KB
testcase_07 AC 38 ms
55,280 KB
testcase_08 AC 183 ms
79,580 KB
testcase_09 AC 84 ms
76,740 KB
testcase_10 AC 177 ms
78,944 KB
testcase_11 AC 82 ms
76,480 KB
testcase_12 AC 82 ms
76,264 KB
testcase_13 AC 499 ms
82,432 KB
testcase_14 AC 425 ms
82,724 KB
testcase_15 AC 385 ms
82,956 KB
testcase_16 AC 613 ms
83,276 KB
testcase_17 AC 178 ms
78,140 KB
testcase_18 AC 307 ms
80,048 KB
testcase_19 AC 394 ms
81,504 KB
testcase_20 AC 102 ms
76,908 KB
testcase_21 AC 302 ms
81,980 KB
testcase_22 AC 331 ms
80,336 KB
testcase_23 AC 401 ms
82,148 KB
testcase_24 AC 400 ms
82,028 KB
testcase_25 AC 412 ms
81,228 KB
testcase_26 AC 281 ms
79,228 KB
testcase_27 AC 343 ms
79,824 KB
testcase_28 AC 245 ms
79,668 KB
testcase_29 AC 211 ms
78,404 KB
testcase_30 AC 220 ms
78,464 KB
testcase_31 AC 586 ms
91,444 KB
testcase_32 AC 43 ms
62,380 KB
testcase_33 AC 64 ms
71,644 KB
testcase_34 AC 43 ms
56,288 KB
testcase_35 AC 39 ms
55,944 KB
testcase_36 AC 39 ms
55,152 KB
testcase_37 AC 40 ms
55,180 KB
testcase_38 AC 38 ms
54,792 KB
testcase_39 AC 38 ms
55,936 KB
testcase_40 AC 38 ms
55,568 KB
権限があれば一括ダウンロードができます

ソースコード

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:
                        if abs(h-dh)+abs(w-dw)<= D:
                            UF.unite(board[h][w], board[dh][dw])


initial_count = 0
for members in UF.all_group_members().values():
    #print(members, len(members))
    if len(members)>=2:
        initial_count += 1
                        
#print('initial_count', initial_count)

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:
                        if abs(h-dh)+abs(w-dw)<= D:
                            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, 'seiza', seiza, 'star', star)
            if seiza == 0:
                if star >= 1:
                    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', mn, 'mx', mx)
            #print()
print(mn, mx)
0