1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class UF:
n,q=RR() uf=UF(n+q)
nums=[] h=[]
for i in range(n): x,y=RR() for j in range(i): px,py=nums[j] h.append((abs(px-x)+abs(py-y),i,j)) nums.append([x,y]) m=n
heapify(h)
for _ in range(q): tmp=RR() if tmp[0]==1: x,y=tmp[1:] idx=0 vis={} for px,py in nums: d=abs(px-x)+abs(py-y) root=uf.find(idx) if root in vis:vis[root]=min(vis[root],d) else:vis[root]=d idx+=1 for k,v in vis.items(): heappush(h,(v,k,n)) nums.append([x,y]) m+=1 n+=1
elif tmp[0]==2: if m==1 or not h:print(-1) else: while h and uf.connect(h[0][1],h[0][2]):heappop(h)
d=h[0][0] while h and h[0][0]==d: _,i,j=heappop(h) if not uf.connect(i,j): uf.union(i,j) m-=1 print(d) else: u,v=tmp[1:] v-=1 u-=1 if uf.connect(u,v):print('Yes') else:print('No')
|