建图,连接,查找相邻
原文连接
AC代码:
#include<iostream>
#include<string.h>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int n,T;
int topn[1005],pre[1005];
double h,r;
typedef struct node{
double x,y,z;
}node;
node nod;
vector<node> vec;
double cmp(const node &a,const node &b){
return a.z<b.z;
}
double clac(int n0,int n1){
node nod0=vec[n0];
node nod1=vec[n1];
double dis=(nod0.x-nod1.x)*(nod0.x-nod1.x)+(nod0.y-nod1.y)*(nod0.y-nod1.y)+
(nod0.z-nod1.z)*(nod0.z-nod1.z);
return sqrt(dis);
}
int find(int r){
int x=r;
while(r!=pre[r]){
r=pre[r];
}
while(x!=r){
int y=pre[x];
pre[x]=r;
x=y;
}
return r;
}
void Union(int x,int y){
int fa=find(x);
int fb=find(y);
if(fa!=fb){
pre[fa]=fb;
}
}
void init(){
int i,j,flag=-1;
vec.clear();
cin>>n>>h>>r;
for(i=0;i<n;i++){
cin>>nod.x>>nod.y>>nod.z;
vec.push_back(nod);
}
sort(vec.begin(),vec.end(),cmp);
i=vec.size()-1;
if(vec[0].z-r>0||vec[i].z+r<h){
cout<<"No"<<endl;
}
else {
for(i=0;i<1005;i++){
pre[i]=i;
}
for(i=0;i<vec.size();i++){
for(j=i+1;j<vec.size();j++){
double dis=clac(i,j);
if(dis<=2*r){
Union(i,j);
}
}
}
//cout<<"start"<<endl;
for(i=0;i<vec.size();i++){
if(vec[i].z-r>0) break;
j=vec.size()-1;
for(;j>=i;j--){
if(vec[j].z+r<h) break;
if(find(i)==find(j)){
flag=0;
break;
}
}
if(flag==0) break;
}
if(flag==-1){
cout<<"No"<<endl;
}
else if(flag==0){
cout<<"Yes"<<endl;
}
}
}
int main(){
int i,j;
cin>>T;
while(T--){
init();
}
return 0;
}