POJ 2236 Wireless Network

题目链接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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值