NOIp2017D2T1(luogu3968) 奶酪 (并查集)

并查集。

判相切或相交的时候可以两边同时平方,就不需要double和开根号了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 #include<cmath>
 7 #include<ctime>
 8 #define LL long long int
 9 #define inf 0x3f3f3f3f
10 #define pw2(x) ((x)*(x))
11 using namespace std;
12 const int maxn=1010;
13 
14 LL rd(){
15    LL x=0;char c=getchar();int neg=1;
16    while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
17    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
18    return x*neg;
19 }
20 
21 LL pos[maxn][3],r,h;
22 int fa[maxn],N;
23 
24 int getf(int x){return x==fa[x]?x:fa[x]=getf(fa[x]);}
25 void add(int a,int b){fa[getf(b)]=getf(a);}
26 
27 
28 inline bool judge(LL x1,LL y1,LL z1,LL x2,LL y2,LL z2){
29     return pw2(x1-x2)+pw2(y1-y2)+pw2(z1-z2)<=pw2(r)*4;
30 }
31 
32 int main(){
33     int i,j,k;
34     for(int t=rd();t;t--){
35         N=rd(),h=rd(),r=rd();
36         for(i=0;i<=N+1;i++) fa[i]=i;
37         for(i=1;i<=N;i++){
38             pos[i][0]=rd(),pos[i][1]=rd(),pos[i][2]=rd();
39             if(fabs(pos[i][2])<=r) add(0,i);
40             if(fabs(pos[i][2]-h)<=r) add(N+1,i);
41         }for(i=1;i<N;i++){
42             for(j=i+1;j<=N;j++){
43                 if(judge(pos[i][0],pos[i][1],pos[i][2],pos[j][0],pos[j][1],pos[j][2])) add(i,j);
44             }
45         }
46         if(getf(0)==getf(N+1)) printf("Yes\n");
47         else printf("No\n");
48     }
49     return 0;
50 }

猜你喜欢

转载自www.cnblogs.com/Ressed/p/9588510.html