题意:给出图中每个点的坐标与最大连通距离,之后给出维修后的点,与一系列问题,问题求两点之间是否连通,如果两点之间绝对距离小于连通距离d或者两点可以通过其他能够到达的点连通起来则表示成功
思路:用并查集,每次维修将点放入vector,并与当前已维修的能够到达的点连通起来,提问时直接返回两点是否连通
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
int n, u[1005];
double d;
vector<int> v;
struct node {
double x, y;
node(double x = 0, double y = 0) : x(x), y(y) {}
}total[1005];
double dist(node a, node b)
{
double t = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
return sqrt(t);
}
int ufind(int x)
{
if (x != u[x])
u[x] = ufind(u[x]);
return u[x];
}
void unite(int x, int y)
{
if (dist(total[x], total[y]) <= d)
u[ufind(x)] = ufind(y);
}
int main()
{
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> total[i].x >> total[i].y;
u[i] = i;
}
char c;
while (cin >> c) {
if (c == 'O') {
int t;
cin >> t;
v.push_back(t);
for (int i = 0; i < v.size(); i++) {
if (v[i] != t) {
unite(t, v[i]);
}
}
}
else {
int n1, n2;
cin >> n1 >> n2;
if (ufind(n1) == ufind(n2))
cout << "SUCCESS" << endl;
else
cout << "FAIL" << endl;
}
}
return 0;
}