题目大意:有n台坏掉的电脑,给出每台电脑的坐标,然后每次询问输入0(字符) x,表示电脑x恢复正常,输入S x y 询问x和y是否可以联网。只要是x和y的距离小于距离d,那么就可以联网,如果有个中介c使得x到c的距离小于d,y到c的距离小于d,那么x和y也可以联网。

题解:当修复好一台电脑后,然后判断与之前修好的电脑的距离,小于d的话,用并查集连在一起。(没敢这样想,感觉这样会T....)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=2E4+;
int n,d;
int arrx[N],arry[N],pre[N];
int cnt[N];
int pos=;
int dis(int a,int b){
return pow(arrx[a]-arrx[b],)+pow(arry[a]-arry[b],)<=d*d;
}
int find(int x){
return x==pre[x]? x:pre[x]=find(pre[x]);
}
void unite(int x,int y){
x=find(x);y=find(y);
pre[x]=y;
}
bool check(int x,int y){
return find(x)==find(y);
}
int main(){
cin>>n>>d;
for(int i=;i<=n;i++) cin>>arrx[i]>>arry[i];
char s;
for(int i=;i<=n;i++) pre[i]=i;
while(cin>>s){
if(s=='S'){
int x,y;
scanf("%d%d",&x,&y);
if(check(x,y)) cout<<"SUCCESS"<<endl;
else cout<<"FAIL"<<endl;
}
else {
int x;cin>>x;
cnt[++pos]=x;
for(int i=;i<pos;i++){
if(dis(x,cnt[i])){
unite(x,cnt[i]);
}
}
}
}
return ;
}
05-19 03:55