本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15674805.html

题目链接

题目

一个点每过一个单位时间就会向四个方向扩散一个距离,如图。

两个点a、b连通,记作e(a,b),当且仅当a、b的扩散区域有公共部分。连通块的定义是块内的任意两个点u、v都必定存在路径e(u,a0),e(a0,a1),…,e(ak,v)。给定平面上的n给点,问最早什么时刻它们形成一个连通块。

思路

二分答案+并查集。

首先二分时间 tt

如果两个点能直接相连,则他们的曼哈顿距离小于二倍 tt。并且把他们放到一个并查集里面。

最后判断所有点是否在一个并查集里即可。

Code

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
// Problem: P1661 扩散
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1661
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 55
//#define M
//#define mo
int n, m, i, j, k;
int x[N], y[N], f[N], l, r, mid;

int fa(int x)
{
if(f[x]==x) return x;
return f[x]=fa(f[x]);
}

int check(int t)
{
for(i=1; i<=n; ++i) f[i]=i;
for(i=1; i<=n; ++i)
for(j=i+1; j<=n; ++j)
if(abs(x[i]-x[j])+abs(y[i]-y[j])<=2*t)
f[fa(i)]=fa(j);
for(i=2; i<=n; ++i)
if(fa(i)!=fa(i-1)) return 0;
return 1;
}

signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
n=read();
for(i=1; i<=n; ++i)
x[i]=read(), y[i]=read();
l=1, r=1000000000000;
while(l<r)
{
mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%lld", l);
return 0;
}