入门题80分求助
查看原帖
入门题80分求助
341245
Sunflower_ac楼主2021/12/28 22:22

入门题,WA on#5#7,用并查集写的,还不知道哪里错了……

//luoguP3958
#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long 
using namespace std;
const int maxn=1005;
int fa[maxn],s1[maxn],s2[maxn];

struct Node{
	int x,y,h;
}a[maxn];

inline int read()
{
	int 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-48;ch=getchar();}
	return x*f;
}

int cmp(Node a,Node b)
{
	return a.h<b.h;
}

int find(int x)
{
	if(x!=fa[x])fa[x]=find(fa[x]);
	return fa[x];
}

inline void insert(int x,int y)
{
    x=find(x);
	y=find(y);
	fa[x]=y;
}

int dis(Node a,Node b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.h-b.h)*(a.h-b.h);
}

signed main()
{
	int T;
	T=read();
	while(T--)
	{
		int n,h,r;
		n=read();h=read();r=read();
        memset(a,0,sizeof(a));
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        int cnt1=0,cnt2=0;
        for(int i=1;i<=n;i++)fa[i]=i;
        //cout<<cnt1<<endl;
		for(int i=1;i<=n;i++)
		{
			a[i].x=read();a[i].y=read();a[i].h=read(); 
			//cout<<a[i].x<<endl;
			if(a[i].h+r>=h)s1[++cnt1]=i;
			if(a[i].h-r<=0)s2[++cnt2]=i;
			for(int j=1;j<=i;j++)
			{
				if ((a[i].x-a[j].x)*(a[i].y-a[j].y)+(a[i].h-a[j].h)*(a[i].h-a[j].h)>4*r*r) continue;
				if(dis(a[i],a[j])<=4*r*r)insert(i,j);
			}
		}
		bool f=false;
		//cout<<f<<endl;
		for(int i=1;i<=cnt1;i++)
		{
			for(int j=1;j<=cnt2;j++)
			{
				if(find(s1[i])==find(s2[i]))f=true;
			}
			if(f==true)break;
		}
		if(f==true) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
2021/12/28 22:22
加载中...