【解题报告】 【NOIP2017】 奶酪

【NOIP2017】 奶酪

题目:奶酪

解题思路:

同样的,可以用dfs做,但是有没有发现,用并查集做会更加地,简单?

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
struct node{
	long long high,low;
	long long x,y,z;
};
long double dist(node p,node q)
{
	return (long double)sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y)+(p.z-q.z)*(p.z-q.z));
}
//并查集
int f[100010];
int get(int x)
{
	return f[x]=(x==f[x]? x:get(f[x]));
}
void merge(int x,int y)
{
	f[get(x)]=get(y);
}
void init()
{
	for(int i=1;i<=100000;i++)
	f[i]=i;
}
//并查集
int T; 
int main()
{
	cin>>T;
	while(T--)
	{
		init();
		long long n,h,r;
		cin>>n>>h>>r;
		node a[1005];
		for(int i=1;i<=n;i++)
		cin>>a[i].x>>a[i].y>>a[i].z; 
		int highest[1005]={0};
		int tot1=0;
		int lowest[1005]={0};
		int tot2=0; 
		for(int i=1;i<=n;i++)
		{
			a[i].high=a[i].z+r;
			a[i].low= a[i].z-r;
			if(a[i].high>=h)
			{
				++tot1;
				highest[tot1]=i;
			}
			if(a[i].low<=0)
			{
				++tot2;
				lowest[tot2]=i;
			}
			for(int j=i;j<=n;j++)
			{
				if(dist(a[i],a[j])<=2*r)
				merge(i,j);
			}
		}
		bool flag=false;
		for(int i=1;i<=tot1;i++)
		{
			for(int j=1;j<=tot2;j++)
			{
				if(get(highest[i])==get(lowest[j]))
				{
					flag=true;
					cout<<"Yes"<<endl;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) continue;
		else
		cout<<"No"<<endl;
	}
	return 0;
}
posted @ 2019-09-07 23:29  wweiyi  阅读(140)  评论(1编辑  收藏  举报
js脚本