奶酪
并查集版子
1 #include<bits/stdc++.h>//万能头,最好不要用 2 using namespace std;//不加本代码爆零 3 int f[1001];//并查集 4 int find(int x){ 5 if (x!=f[x]) f[x]=find(f[x]); 6 return f[x]; 7 }//查找+路径压缩 8 double dis(long long x,long long y,long long z,long long x1,long long y1,long long z1){ 9 return sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1)); 10 }//两点距离公式 11 long long x[100001],y[100001],z[100001]; 12 int f1[100001],f2[100001]; 13 //f1记录与顶面相交的洞的序号 14 //f2记录与底面相交的洞的序号 15 int main(){ 16 int t; 17 scanf("%d",&t); 18 int n,h; 19 long long r; 20 for (int i=1;i<=t;i++){ 21 scanf("%d%d%ld",&n,&h,&r); 22 int tot1=0; 23 int tot2=0; 24 for (int j=1;j<=n;j++){ 25 f[j]=j; 26 } 27 for (int j=1;j<=n;j++){ 28 scanf("%ld%ld%ld",&x[j],&y[j],&z[j]); 29 if (z[j]+r>=h){ 30 tot1++; 31 f1[tot1]=j; 32 } 33 if (z[j]-r<=0){ 34 tot2++; 35 f2[tot2]=j; 36 } 37 for (int k=1;k<=j;k++){ 38 if (dis(x[j],y[j],z[j],x[k],y[k],z[k])<=2*r){ 39 int a1=find(j); 40 int a2=find(k); 41 if (a1!=a2) 42 f[a1]=a2; 43 } 44 } 45 } 46 int s=0; 47 for (int j=1;j<=tot1;j++){ 48 for (int k=1;k<=tot2;k++){ 49 if (find(f1[j])==find(f2[k])){ 50 s=1; 51 break; 52 } 53 } 54 if (s==1) break; 55 } 56 if (s==1) cout<<"Yes"<<endl; 57 else cout<<"No"<<endl; 58 } 59 return 0; 60 }