結果

問題 No.2696 Sign Creation
ユーザー 👑 timitimi
提出日時 2024-02-03 04:15:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 287 ms / 2,500 ms
コード長 1,501 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 82,556 KB
実行使用メモリ 83,744 KB
最終ジャッジ日時 2024-05-17 18:08:06
合計ジャッジ時間 7,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
60,748 KB
testcase_01 AC 44 ms
60,200 KB
testcase_02 AC 35 ms
53,920 KB
testcase_03 AC 38 ms
59,548 KB
testcase_04 AC 36 ms
55,072 KB
testcase_05 AC 40 ms
60,960 KB
testcase_06 AC 35 ms
53,884 KB
testcase_07 AC 36 ms
53,420 KB
testcase_08 AC 89 ms
76,792 KB
testcase_09 AC 50 ms
64,480 KB
testcase_10 AC 103 ms
77,428 KB
testcase_11 AC 60 ms
67,072 KB
testcase_12 AC 57 ms
67,004 KB
testcase_13 AC 287 ms
82,388 KB
testcase_14 AC 226 ms
82,872 KB
testcase_15 AC 205 ms
81,508 KB
testcase_16 AC 271 ms
80,736 KB
testcase_17 AC 136 ms
80,660 KB
testcase_18 AC 152 ms
83,000 KB
testcase_19 AC 208 ms
83,084 KB
testcase_20 AC 67 ms
69,792 KB
testcase_21 AC 219 ms
82,316 KB
testcase_22 AC 200 ms
83,492 KB
testcase_23 AC 217 ms
82,884 KB
testcase_24 AC 231 ms
83,560 KB
testcase_25 AC 227 ms
82,896 KB
testcase_26 AC 160 ms
83,744 KB
testcase_27 AC 184 ms
81,968 KB
testcase_28 AC 156 ms
82,888 KB
testcase_29 AC 139 ms
80,368 KB
testcase_30 AC 141 ms
80,624 KB
testcase_31 AC 250 ms
80,332 KB
testcase_32 AC 39 ms
58,628 KB
testcase_33 AC 46 ms
63,832 KB
testcase_34 AC 43 ms
61,140 KB
testcase_35 AC 35 ms
52,532 KB
testcase_36 AC 35 ms
53,432 KB
testcase_37 AC 33 ms
53,740 KB
testcase_38 AC 39 ms
53,892 KB
testcase_39 AC 33 ms
53,224 KB
testcase_40 AC 34 ms
53,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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:
      B.append((i,j))
      
par=[i for i in range(N)];rank=[0]*N;size=[1]*N
def find(x):
  if par[x]==x:
    return x
  else:
    par[x]=find(par[x])
    return par[x]
 
#同じ集合か判定
def same(x,y):
  return find(x)==find(y)
 
def union(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 
  if rank[x]>rank[y]:
    par[y]=x
    size[x]+=size[y]
  else:
    par[x]=y
    size[y]+=size[x]
    if rank[x]==rank[y]:
      rank[y]+=1
 
for h in range(H):
  for w in range(W):
    if A[h][w]==-1:
      continue 
    p=A[h][w]
    for x,y in B:
      nh,nw=h+x,w+y
      if 0<=nh<H and 0<=nw<W and A[nh][nw]!=-1:
        q=A[nh][nw]
        union(p,q)
        
D={};E={};F={}
for i in range(N):
  d=find(i)
  if d not in F:
    F[d]=1
    if size[d]==1:
      D[d]=1
    else:
      E[d]=1
      
al=len(E);ans=[]
for h in range(H):
  for w in range(W):
    if A[h][w]!=-1:
      continue
    DD,EE=set(),set()
    for x,y in B:
      nh,nw=h+x,w+y
      if 0<=nh<H and 0<=nw<W and A[nh][nw]!=-1:
        q=find(A[nh][nw])
        if size[q]==1:
          EE.add(q)
        else:
          DD.add(q)
    if len(EE)==0:
      if len(DD)==0:
        ans.append(al)
      else:
        ans.append(al-len(DD)+1)
    else:
      ans.append(al-len(DD)+1)

print(min(ans),max(ans))
0