P3958 [NOIP2017 提高组] 奶酪(并查集)

题目链接

思路:

将相交或相切的用并查集维护起来,最后看上表面跟下表面能否在同一个连通块。

代码:



#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=5e5+100;
ll n,h,r;
struct node{
	ll x,y,z;	
}a[maxn];

ll cul(node a,node b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}

int root[maxn];

int Find(int x){
	if(x!=root[x]) return root[x]=Find(root[x]);
	return root[x];
}

int main(){
	int T=read;
	while(T--){
		n=read,h=read,r=read;
		rep(i,1,n){
			a[i].x=read,a[i].y=read,a[i].z=read;
		}
		rep(i,1,n+2) root[i]=i;
		rep(i,1,n){
			if((a[i].z-r)<=0){
				int fx=Find(i),fy=Find(n+1);
				if(fx!=fy) root[fx]=fy;
			}
			if((a[i].z+r)>=h){
				int fx=Find(i),fy=Find(n+2);
				if(fx!=fy) root[fx]=fy;
			}
			rep(j,i+1,n){
				if(cul(a[i],a[j])<=(2*r)*(2*r)){
					int fx=Find(i),fy=Find(j);
					if(fx!=fy) root[fx]=fy;
				}
			}
		}
		if(Find(n+1)==Find(n+2)) puts("Yes");
		else puts("No");
	}
	return 0;
}












posted @ 2021-06-08 22:08  OvO1  阅读(111)  评论(0编辑  收藏  举报