【P1661 扩散】题解

题目链接

题目

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

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

思路

二分答案+并查集。

首先二分时间 \(t\)

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

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

Code

// 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;
}

posted @ 2021-12-11 10:32  zhangtingxi  阅读(146)  评论(0编辑  收藏  举报