結果

問題 No.2696 Sign Creation
ユーザー yansi819yansi819
提出日時 2024-06-01 14:05:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 340 ms / 2,500 ms
コード長 2,493 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 88,496 KB
最終ジャッジ日時 2024-06-01 14:06:04
合計ジャッジ時間 7,118 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
60,356 KB
testcase_01 AC 45 ms
59,724 KB
testcase_02 AC 41 ms
54,192 KB
testcase_03 AC 49 ms
60,728 KB
testcase_04 AC 42 ms
54,676 KB
testcase_05 AC 47 ms
61,076 KB
testcase_06 AC 42 ms
54,260 KB
testcase_07 AC 40 ms
53,604 KB
testcase_08 AC 107 ms
77,048 KB
testcase_09 AC 56 ms
65,800 KB
testcase_10 AC 120 ms
77,444 KB
testcase_11 AC 69 ms
69,972 KB
testcase_12 AC 68 ms
68,776 KB
testcase_13 AC 335 ms
82,688 KB
testcase_14 AC 260 ms
84,308 KB
testcase_15 AC 223 ms
79,816 KB
testcase_16 AC 340 ms
80,812 KB
testcase_17 AC 138 ms
82,016 KB
testcase_18 AC 180 ms
82,344 KB
testcase_19 AC 238 ms
81,812 KB
testcase_20 AC 75 ms
72,120 KB
testcase_21 AC 238 ms
81,184 KB
testcase_22 AC 260 ms
82,960 KB
testcase_23 AC 259 ms
83,400 KB
testcase_24 AC 232 ms
82,676 KB
testcase_25 AC 275 ms
83,936 KB
testcase_26 AC 164 ms
83,616 KB
testcase_27 AC 233 ms
82,824 KB
testcase_28 AC 204 ms
83,324 KB
testcase_29 AC 170 ms
82,300 KB
testcase_30 AC 183 ms
83,728 KB
testcase_31 AC 303 ms
88,496 KB
testcase_32 AC 44 ms
60,268 KB
testcase_33 AC 51 ms
64,016 KB
testcase_34 AC 48 ms
60,764 KB
testcase_35 AC 58 ms
54,112 KB
testcase_36 AC 39 ms
53,480 KB
testcase_37 AC 38 ms
53,032 KB
testcase_38 AC 39 ms
53,308 KB
testcase_39 AC 40 ms
52,956 KB
testcase_40 AC 39 ms
53,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n
    
    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), self.leader(b)
        if x == y: return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    
    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]
    
    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]

H, W, N, D = map(int, input().split())
A = [[-1] * W for _ in range(H)]
for i in range(N):
    h, w = map(int, input().split())
    h -= 1; w -= 1
    A[h][w] = i
B = []
for i in range(-D, D + 1):
    for j in range(-D, D + 1):
        if abs(i) + abs(j) > D: continue
        if i == 0 and j == 0: continue
        B.append((i, j))
uf = DSU(N)
for i in range(H):
    for j in range(W):
        if A[i][j] == -1: continue
        for di, dj in B:
            ni = i + di
            nj = j + dj
            if 0 > ni or ni >= H or 0 > nj or nj >= W or A[ni][nj] == -1: continue
            uf.merge(A[i][j], A[ni][nj])
cnt = 0
for g in uf.groups():
    if len(g) >= 2:
        cnt += 1
ans = []
for i in range(H):
    for j in range(W):
        if A[i][j] != -1: continue
        DD, EE = set(), set()
        for di, dj in B:
            ni = i + di
            nj = j + dj
            if 0 > ni or ni >= H or 0 > nj or nj >= W or A[ni][nj] == -1: continue
            ld = uf.leader(A[ni][nj])
            if uf.size(ld) == 1:
                EE.add(ld)
            else:
                DD.add(ld)
        if len(EE) == 0:
            if len(DD) == 0:
                ans.append(cnt)
            else:
                ans.append(cnt - len(DD) + 1)
        else:
            ans.append(cnt - len(DD) + 1)
print(min(ans), max(ans))
0