当天不会并查集,手写的dfs居然得了70……
真菜orz
这道题用并查集,贼快
#include<bits/stdc++.h>
const int N=1000+5;
using namespace std;
int father[N];
int T,n,tmp1,tmp2;
double x[N],y[N],z[N],r,h;
bool ok;
vector<int> top,bot;
int bcj(int x){
if(x==father[x]) return x;
return father[x]=bcj(father[x]);
}
double check(double x1,double x2,double y1,double y2,double z1,double z2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
}
int main(){
scanf("%d",&T);
for(int q=1;q<=T;q++){
top.clear();
bot.clear();
ok=false;
memset(z,0,sizeof(z));
memset(y,0,sizeof(y));
memset(x,0,sizeof(x));
scanf("%d%lf%lf",&n,&h,&r);
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
if(z[i]-r<=0)
bot.push_back(i);
if(z[i]+r>=h)
top.push_back(i);
}
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j && check(x[i],x[j],y[i],y[j],z[i],z[j])<=2*r){
tmp1=bcj(i);
tmp2=bcj(j);
if(tmp1!=tmp2)
father[tmp1]=tmp2;
}
for(int i=0;i<top.size();i++){
for(int j=0;j<bot.size();j++)
if(bcj(top[i])==bcj(bot[j])){
ok=true;
break;
}
if(ok)
break;
}
if(ok) printf("Yes\n");
else printf("No\n");
}
return 0;
}
加油……