Wireless Network POJ - 2236
思路:并查集,把维修过且距离在d之内的放入同一个集合,之后判断
具体代码如下
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1010;
struct Point{ //存各个点的坐标
int x, y;
}point[N];
int n, d;
int p[N];
int visit[N], cnt;//visit存的是已经维修过的点
bool st[N];//st数组存该点有没有维修过
int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
bool cmp(int x, int y){ //比较距离,开根号之后可能会损失精度,这里直接比较平方
double x1 = point[x].x - point[y].x;
double y1 = point[x].y - point[y].y;
return (x1*x1 + y1*y1 <= d*d);
}
int main(){
cin >> n >> d;
for(int i=1; i<=n; ++i) p[i] = i;
for(int i=1; i<=n; ++i){
int a, b;
cin >> a >> b;
point[i] = {a, b};
}
char op[2];
while(cin >> op){
int x1, x2;
if(*op == 'O'){
cin >> x1;
for(int i=0; i<cnt; ++i){
if(cmp(visit[i], x1)){
int px = find(visit[i]), py = find(x1);
p[px] = py;
}
}
visit[cnt++] = x1;
st[x1] = true;
}else{
cin >> x1 >> x2;
if(st[x1] && st[x2] && find(x1) == find(x2)) puts("SUCCESS");
else puts("FAIL");
}
}
return 0;
}