結果

問題 No.2696 Sign Creation
ユーザー tipstar0125tipstar0125
提出日時 2024-03-25 16:47:06
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,322 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 120,500 KB
最終ジャッジ日時 2024-05-17 18:25:17
合計ジャッジ時間 5,565 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
62,596 KB
testcase_01 AC 38 ms
55,380 KB
testcase_02 AC 37 ms
55,588 KB
testcase_03 AC 39 ms
56,080 KB
testcase_04 AC 40 ms
57,376 KB
testcase_05 AC 40 ms
55,892 KB
testcase_06 AC 39 ms
55,888 KB
testcase_07 AC 39 ms
56,264 KB
testcase_08 AC 257 ms
78,960 KB
testcase_09 AC 44 ms
56,784 KB
testcase_10 AC 177 ms
79,160 KB
testcase_11 AC 46 ms
57,864 KB
testcase_12 AC 43 ms
57,472 KB
testcase_13 TLE -
testcase_14 AC 871 ms
111,244 KB
testcase_15 AC 828 ms
105,556 KB
testcase_16 TLE -
testcase_17 AC 58 ms
70,708 KB
testcase_18 AC 287 ms
97,300 KB
testcase_19 AC 910 ms
114,948 KB
testcase_20 AC 76 ms
77,692 KB
testcase_21 AC 565 ms
102,708 KB
testcase_22 AC 310 ms
97,420 KB
testcase_23 AC 716 ms
113,524 KB
testcase_24 AC 714 ms
113,572 KB
testcase_25 AC 1,230 ms
120,500 KB
testcase_26 AC 144 ms
79,948 KB
testcase_27 AC 299 ms
108,216 KB
testcase_28 AC 367 ms
112,952 KB
testcase_29 AC 83 ms
77,064 KB
testcase_30 AC 87 ms
77,408 KB
testcase_31 TLE -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

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=defaultdict(int)
for (i,xy) in enumerate(XY):mp[xy]=i

uf=UnionFind(N)
for (x,y) in XY:
    dist=defaultdict(int)
    dist[(x,y)]=0
    Q=deque()
    Q.append((x,y))
    while len(Q):
        i,j=Q.popleft()
        if dist[(i,j)]==D:continue
        for (di,dj) in [(1,0),(-1,0),(0,1),(0,-1)]:
            ni=i+di
            nj=j+dj
            if 1<=ni<=H and 1<=nj<=W:
                if dist.get((ni,nj)) is None or dist[(i,j)]+1<dist[(ni,nj)]:
                    dist[(ni,nj)]=dist[(i,j)]+1
                    Q.append((ni,nj))
                    if mp.get((ni,nj)) is not None:
                        uf.union(mp[(x,y)],mp[(ni,nj)])

cand=set()
star=set()
sign=set()
star_cand=defaultdict(set)
sign_cand=defaultdict(set)
for (x,y) in XY:
    root=uf.find(mp[(x,y)])
    size=uf.size(root)
    if size==1:star.add(root)
    else:sign.add(root)

    dist=defaultdict(int)
    dist[(x,y)]=0
    Q=deque()
    Q.append((x,y))
    while len(Q):
        i,j=Q.popleft()
        if dist[(i,j)]==D:continue
        for (di,dj) in [(1,0),(-1,0),(0,1),(0,-1)]:
            ni=i+di
            nj=j+dj
            if 1<=ni<=H and 1<=nj<=W:
                if dist.get((ni,nj)) is None or dist[(i,j)]+1<dist[(ni,nj)]:
                    dist[(ni,nj)]=dist[(i,j)]+1
                    Q.append((ni,nj))
                    if mp.get((ni,nj)) is None:
                        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=defaultdict(int)
sign_cand_cnt=defaultdict(int)
for (k,v) in star_cand.items():
    for x in v:star_cand_cnt[x]+=1
for (k,v) in sign_cand.items():
    for x in v:sign_cand_cnt[x]+=1

inc=0
dec=0
for c in cand:
    if star_cand_cnt.get(c) is not None and sign_cand_cnt.get(c) is None:inc+=1
    elif star_cand_cnt.get(c) is None and sign_cand_cnt.get(c) is not None:dec=max(dec,sign_cand_cnt[c])

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

print(mn,mx)

0