奶酪[并查集][NOIP2017]

又是一道并查集...

分析 

如果两个洞的dist小于2倍r,两个洞连通,合并为1个集合,一个集合里面的洞都可以两两到达

判断超过h的洞与在最底下的洞在不在连不连通即判断在不在一个集合

并查集判断集合----看祖先是否相同即可

代码

#include<bits/stdc++.h>
#define N 1005
#define LL long long 
using namespace std;
struct Node{int x,y,z;}a[N];
int T,n,h,r;
int fa[N],able1[N],able2[N],tot1,tot2;
int read(){
	int cnt=0,f=1;char ch=0;
	while(!isdigit(ch)){ch=getchar();if(ch=='-')f=-1;}
	while(isdigit(ch))cnt=cnt*10+ch-'0',ch=getchar();
	return cnt*f;
}
LL dis(int i,int j){
	LL x=(LL)(a[i].x-a[j].x)*(a[i].x-a[j].x);
	LL y=(LL)(a[i].y-a[j].y)*(a[i].y-a[j].y);
	LL z=(LL)(a[i].z-a[j].z)*(a[i].z-a[j].z);
	return x+y+z;
}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int main(){
	T=read();
	while(T--){
		n=read(),h=read(),r=read();
		memset(a,0,sizeof(a)),tot1=0,tot2=0;
		for(int i=1;i<=n;i++)fa[i]=i;
		for(int i=1;i<=n;i++)
			a[i].x=read(),a[i].y=read(),a[i].z=read();
		for(int i=1;i<=n;i++)//将可行的点存在数组里
			if(a[i].z-r<=0) able1[++tot1]=i;
		for(int i=1;i<=n;i++)
			if(a[i].z+r>=h) able2[++tot2]=i;
		for(int i=1;i<=n;i++)
			for(int j=i;j<=n;j++)
				if(dis(i,j)<=(LL)4*r*r){ //合并
					int x=find(i),y=find(j);
					if(x!=y) fa[x]=y;
				}
		for(int i=1;i<=tot1;i++)
			for(int j=1;j<=tot2;j++)
				if(find(able1[i])==find(able2[j])){cout<<"Yes"<<endl;goto die;}
		cout<<"No"<<endl;
		die:;
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值