题目链接:http://poj.org/problem?id=2236
题意:给你N台电脑,从1-N。 给一个距离d,表示两台计算机的最大通信距离,超过则两台计算机无法通信。
有两种操作: “O”表示修好了该计算机, “S”表示测试两台计算机是否能够通信;
解题思路:并查集水题,将能够通信的并在一起就行了。
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 1100
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
}p[N];
int F[N];
typedef Point Vector; //Vector 为 Point的别名
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}
double Dot(Vector A, Vector B){return A.x*B.x + A.y*B.y; }
double Length(Vector A){return sqrt(Dot(A, A));}
int fun(int x)
{
if(F[x] != x)
F[x] = fun(F[x]);
return F[x];
}
int main ()
{
int n, d;
scanf("%d %d", &n, &d);
for(int i = 1; i <= n; i++){
scanf("%lf %lf", &p[i].x, &p[i].y);
F[i] = i;
}
int ha[N], cnt = 1;
char ch;
getchar();
while(scanf("%c", &ch) != EOF)
{
int a, b;
if(ch == 'S')
{
scanf("%d %d", &a, &b);
int fa = fun(a);
int fb = fun(b);
if(fa == fb)
puts("SUCCESS");
else
puts("FAIL");
}
if(ch == 'O')
{
scanf("%d", &a);
for(int i = 1; i < cnt; i++)
{
if(Length(p[ha[i]]-p[a]) <= d)
{
int fa = fun(ha[i]);
int fb = fun(a);
if(fa != fb)
F[fb] = fa;
}
}
ha[cnt++] = a;
}
getchar();
}
return 0;
}