http://poj.org/problem?id=2236
2.Idea
Using Disjoint set/Union-find set
3.Source
double x[1003], y[1003];
int n;
double d;
bool g[1003][1003];
bool on[1003];
int Parent[1004];
int Size[1004];
void make_set(int v) {
Parent[v] = v;
Size[v] = 1;
}
int find_set(int v) {
if (v == Parent[v])
return v;
return Parent[v] = find_set(Parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (Size[a] < Size[b])
swap(a, b);
Parent[b] = a;
Size[a] += Size[b];
}
}
double dist(double x1, double x2, double y1, double y2)
{
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int main()
{
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
make_set(i);
}
for(int i=0;i<n;i++)
for (int j = 0; j < n; j++) {
if (dist(x[i] , x[j] , y[i] , y[j]) <= d) {
g[i][j] = g[j][i] = true;
}
}
char c;
while (cin >> c) {
int p, q;
if (c == 'O') {
cin >> p; p--;
on[p] = 1;
for (int i = 0; i < n; i++) {
if (g[p][i] && p != i && on[i]) {
union_sets(p, i);
}
}
}
else if (c == 'S') {
cin >> p >> q; p--; q--;
if (find_set(p) == find_set(q)) {
cout << "SUCCESS" << endl;
}
else {
cout << "FAIL" << endl;
}
}
else {
break;
}
}
return 0;
}
No comments:
Post a Comment