結果

問題 No.2696 Sign Creation
ユーザー ntuda
提出日時 2024-03-23 15:51:43
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,528 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 94,416 KB
最終ジャッジ日時 2024-12-20 12:45:43
合計ジャッジ時間 18,206 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36 WA * 1 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]
def unite(x,y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x,y = y,x
    P[x] += P[y]
    P[y] = x

def same(x,y):
    return root(x) == root(y)

def size(x):
    x = root(x)
    return -P[x]


H, W, N, D = map(int, input().split())
XY = [list(map(int,input().split())) for _ in range(N)]
P = [-1] * N
dic = {}
for i in range(N):
    dic[tuple(XY[i])] = i

def search(x, y):
    ret = set()
    for i in range(-D, D+1):
        if 0 < x + i <= H:
            xi = x + i
            for j in range(-D + abs(i), D - abs(i) + 1):
                if 0 < y + j <= W:
                    yj = y+j
                    if (xi,yj) in dic:
                        ret.add(dic[(xi,yj)])
    return ret

for i in range(N):
    x,y = XY[i]
    for j in search(x,y):
        unite(i,j)

Q = set()
for i in range(N):
    if size(i) >= 2:
        Q.add(root(i))
gcount = len(Q)


ansmax = 0
ansmin = gcount
for i in range(1,H+1):
    for j in range(1,W+1):
        tmp = search(i,j)
        Q1 = set()
        Q2 = set()
        for k in tmp:
            if size(k) > 1:
                Q2.add(root(k))
            else:
                Q1.add(k)
        if Q2:
            tmp2 = gcount - len(Q2) + 1
            ansmax = max(ansmax, tmp2)
            ansmin = min(ansmin, tmp2)
        elif Q1:
            tmp2 = gcount + 1
            ansmax = max(ansmax,tmp2)
            ansmin = min(ansmin,tmp2)

print(ansmin,ansmax)

0