用集合表示是否可以相连接,一个集合里的都是可以互相连接的
当一个节点修好后,更新能与它相连的点可以通过遍历所有点
#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=(1001+5);
int p[maxn];
double d[maxn][maxn];
int N,maxd;
int getPar(int x){
return p[x]==x?x:p[x]=getPar(p[x]);
}
struct computer{
int x,y;
bool repaired;
computer(int x=0,int y=0,int r=false):x(x),y(y),repaired(r){}
}pc[maxn];
double getdist(computer x,computer y){
return hypot(x.x-y.x,x.y-y.y);
}
void init()
{
for(int i=1;i<=N;i++)
for(int j=i+1;j<=N;j++){
double dis=getdist(pc[i],pc[j]);
d[i][j]=d[j][i]=dis;
}
}
void update(int u)
{
for(int v=1;v<=N;v++){
if(v!=u&&d[u][v]<=maxd&&pc[v].repaired)
{
int up=getPar(u);
int vp=getPar(v);
if(up!=vp) p[up]=vp;
}
}
}
int main()
{
cin>>N>>maxd;
int x,y;
for(int i=1;i<=N;i++){
cin>>x>>y;
p[i]=i;
pc[i]=computer(x,y);
}
init();
char cmd[10];int xp,yp;
while(scanf("%s",cmd)!=EOF)
{
if(cmd[0]=='O'){
scanf("%d",&x);
pc[x].repaired=true;
update(x);
}
else {
scanf("%d%d",&x,&y);
xp=getPar(x);
yp=getPar(y);
if(xp!=yp) cout<<"FAIL"<<endl;
else cout<<"SUCCESS"<<endl;
}
}
return 0;
}