結果

問題 No.2696 Sign Creation
ユーザー tipstar0125tipstar0125
提出日時 2024-03-25 17:35:42
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,698 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 107,432 KB
最終ジャッジ日時 2024-05-17 18:25:29
合計ジャッジ時間 8,313 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
56,140 KB
testcase_01 AC 42 ms
55,460 KB
testcase_02 AC 41 ms
55,600 KB
testcase_03 AC 42 ms
55,808 KB
testcase_04 AC 44 ms
56,020 KB
testcase_05 AC 42 ms
55,684 KB
testcase_06 AC 42 ms
55,332 KB
testcase_07 AC 44 ms
56,648 KB
testcase_08 AC 120 ms
77,792 KB
testcase_09 AC 48 ms
62,056 KB
testcase_10 AC 126 ms
78,284 KB
testcase_11 AC 48 ms
62,908 KB
testcase_12 AC 49 ms
62,084 KB
testcase_13 AC 553 ms
107,432 KB
testcase_14 AC 367 ms
104,864 KB
testcase_15 AC 359 ms
95,972 KB
testcase_16 AC 640 ms
100,096 KB
testcase_17 AC 54 ms
65,788 KB
testcase_18 AC 172 ms
89,380 KB
testcase_19 AC 407 ms
104,544 KB
testcase_20 AC 60 ms
67,584 KB
testcase_21 AC 374 ms
98,564 KB
testcase_22 AC 163 ms
91,268 KB
testcase_23 AC 357 ms
98,564 KB
testcase_24 AC 357 ms
98,808 KB
testcase_25 AC 446 ms
105,308 KB
testcase_26 AC 96 ms
79,088 KB
testcase_27 AC 148 ms
98,668 KB
testcase_28 AC 161 ms
99,192 KB
testcase_29 AC 61 ms
69,700 KB
testcase_30 AC 62 ms
70,208 KB
testcase_31 AC 636 ms
97,476 KB
testcase_32 AC 42 ms
54,648 KB
testcase_33 AC 46 ms
61,608 KB
testcase_34 AC 49 ms
62,380 KB
testcase_35 AC 42 ms
54,836 KB
testcase_36 AC 42 ms
56,460 KB
testcase_37 AC 42 ms
55,192 KB
testcase_38 AC 41 ms
55,364 KB
testcase_39 WA -
testcase_40 AC 41 ms
54,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict,deque

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 union(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

H,W,N,D=map(int,input().split())
XY=[tuple(map(int,input().split())) for _ in range(N)]

mp=[[N for _ in range(W+1)] for _ in range(H+1)]
for (i,(x,y)) in enumerate(XY):mp[x][y]=i

nxt=[]
for i in range(-5,6):
    for j in range(-5,6):
        if i==0 and j==0:continue
        if abs(i)+abs(j)<=D:nxt.append((i,j))

uf=UnionFind(N)
for (i,j) in XY:
    for (di,dj) in nxt:
        ni=i+di
        nj=j+dj
        if 1<=ni<=H and 1<=nj<=W and mp[ni][nj]!=N:
            uf.union(mp[i][j],mp[ni][nj])

cand=set()
star=set()
sign=set()
star_cand=[set() for _ in range(N)]
sign_cand=[set() for _ in range(N)]
for (i,j) in XY:
    root=uf.find(mp[i][j])
    size=uf.size(root)
    if size==1:star.add(root)
    else:sign.add(root)

    for (di,dj) in nxt:
        ni=i+di
        nj=j+dj
        if 1<=ni<=H and 1<=nj<=W and mp[ni][nj]==N:
            cand.add((ni,nj))
            if size==1:star_cand[root].add((ni,nj))
            else:sign_cand[root].add((ni,nj))

star_num=len(star)
sign_num=len(sign)

star_cand_cnt=[[0 for _ in range(W+1)] for _ in range(H+1)]
sign_cand_cnt=[[0 for _ in range(W+1)] for _ in range(H+1)]
for v in star_cand:
    for (x,y) in v:star_cand_cnt[x][y]+=1
for v in sign_cand:
    for (x,y) in v:sign_cand_cnt[x][y]+=1

inc=0
dec=0
for (x,y) in cand:
    if star_cand_cnt[x][y]>0 and sign_cand_cnt[x][y]==0:inc+=1
    elif star_cand_cnt[x][y]==0 and sign_cand_cnt[x][y]>0:dec=max(dec,sign_cand_cnt[x][y])

mx=sign_num
mn=sign_num
if inc>0:mx+=1
if dec>0:mn-=dec-1

print(mn,mx)
0