洛谷P3958 奶酪 并查集

时间:2020-12-06 09:12:46

两个空洞可互达当且仅当两个空洞相切,即球心距离小于等于球的直径。

一一枚举两个可互达的空洞,并用并查集连起来即可。

Code:

#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 1000 + 4;
int p[maxn];
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
struct Point
{
double x,y,z;
}points[maxn];
inline double dis(Point a,Point b)
{
return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z) );
}
inline void join(int a,int b)
{
int x = find(a);
int y = find(b);
if(x == y)return;
p[x] = y;
}
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n,h;
double r;
scanf("%d%d%lf",&n,&h,&r);
for(int i = 1;i <= n;++i)p[i] = i;
for(int i = 1;i <= n;++i)scanf("%lf%lf%lf",&points[i].x, &points[i].y, &points[i].z);
for(int i = 1;i <= n;++i)
for(int j = 1;j < i;++j) if(dis(points[i], points[j]) <= r * 2.0) join(i,j); int flag = 0;
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= n;++j)
{
if(find(i) == find(j) && points[i].z + r >= h && points[j].z - r <= 0)
{
flag = 1;
break;
}
}
if(flag == 1)break;
}
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}