把点与点的关系保存在连接矩阵里就好了,如果距离小于等于规定距离的话就赋值为1
然后不断做(做符合条件)合并操作即可……
View Code
#include<stdio.h>
#define N 1005
int f[N];
int d;
int ji[N];
struct data
{
int x,y;
}p[N];
bool map[N][N];
int dis(data a,data b)
{
int temp=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
if(temp<=(d*d))
return 1;
return 0;
}
int find(int pos)
{
if(f[pos]==-1)return pos;
return pos=find(f[pos]);
}
int un(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b)return 0;
f[a]=b;
return 1;
}
int main()
{
int n;
scanf("%d%d",&n,&d);
int i,j;
for(i=1;i<=n;i++)
{
f[i]=-1;
scanf("%d%d",&p[i].x,&p[i].y);
}
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(i==j)
{
map[i][i]=0;
continue;
}
map[i][j]=dis(p[i],p[j]);
map[j][i]=map[i][j];
}
}
getchar();
char rt;
int add=0,temp,a,b;
while(scanf("%c",&rt)!=EOF)
{
if(rt=='O')
{
scanf("%d",&temp);
for(i=1;i<=add;i++)
{
if(map[ji[i]][temp]==1)
un(ji[i],temp);
}
add++;
ji[add]=temp;
}
else if(rt=='S')
{
scanf("%d%d",&a,&b);
if(find(a)==find(b))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
//getchar();
}
}