P3958 奶酪

题目链接嘿嘿嘿

这道题用并查集来维护每一组数据,在遍历完之后进行处理

 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 long long dis(long long x,long long y,long long z,long long x1,long long y1,long long z1){
 9     return (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%lld",&n,&h,&r);//long long不开的话...
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("%lld%lld%lld",&x[j],&y[j],&z[j]);//long long不开的话...
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 ((x[j]-x[k])*(x[j]-x[k])+(y[j]-y[k])*(y[j]-y[k])>4*r*r) continue;
39                 //防止爆long long的特判。 
40                 if (dis(x[j],y[j],z[j],x[k],y[k],z[k])<=4*r*r){
41                     int a1=find(j);
42                     int a2=find(k);
43                     if (a1!=a2) f[a1]=a2;
44                 }
45             }
46         }
47         int s=0;
48         //看看每一个中是否有洞连接上下面
49         for (int j=1;j<=tot1;j++){
50             for (int k=1;k<=tot2;k++){
51                 if (find(f1[j])==find(f2[k])){
52                     s=1; 
53                     break;
54                 }
55             }
56             if (s==1) break;
57         }
58         if (s==1) cout<<"Yes"<<endl;
59         else cout<<"No"<<endl;
60     }
61     return 0;
62 } 

 

posted @ 2019-09-18 00:52  喵呜,颜儿ღ  阅读(113)  评论(0编辑  收藏  举报