以电脑之间的连通关系建立并查集,连接符合条件的电脑并检查连通性即可。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int MAXN = 1001;
int par[MAXN+5];
int high[MAXN+5];
bool status[MAXN+5];
P com[MAXN+5];
int dis(P a, P b)
{
return pow(a.first-b.first, 2) + pow(a.second-b.second, 2);
}
void init(int n)
{
for (int i = 1; i <= n; i++) {
par[i] = i;
high[i] = 0;
}
}
int find(int x)
{
if (x == par[x]) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (high[x] < high[y]) {
par[x] = y;
}
else {
par[y] = x;
if (high[x] == high[y]) {
high[x]++;
}
}
}
bool connect(int x, int y)
{
return find(x) == find(y);
}
int main()
{
int n, d;
scanf("%d%d", &n, &d);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &com[i].first, &com[i].second);
}
getchar();
init(n);
int ch;
while ((ch = getchar()) != EOF) {
int x, y;
if (ch == 'O') {
scanf("%d", &x);
status[x] = true;
for (int i = 1; i <= n; i++) {
if (status[i] == true && dis(com[x], com[i]) <= pow(d, 2)) {
if (i == x) {
continue;
}
unite(x, i);
}
}
}
else {
scanf("%d%d", &x, &y);
if (connect(x, y)) {
puts("SUCCESS");
}
else
puts("FAIL");
}
getchar();
}
return 0;
}