Wireless Network POJ - 2236

//读取C的时候没有!=EOF 会出现TLE的错误..... 

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 10000+5;

pair<int, int> a[N];
int pre[N], store[N];
int map[N][N];

int n, k;

void init()
{

    for(int i=0; i<=N; i++){
        pre[i] = i;
    }

}

bool JudgeDist(int c, int b)
{

    int dx = a[c].first - a[b].first;
    int dy = a[c].second - a[b].second;
/*
    int length = (dx*dx+dy*dy);

    cout << "X :" <<  a[c].first << " " << a[b].first << endl;
    cout << "Y :" << a[c].second << " " << a[b].second << endl;
    cout << "dist :" << length << ":" << k << endl;
 */
    return (dx*dx+dy*dy) <= k*k;
}

int Find(int a)
{
    return (pre[a]==a) ? a : Find(pre[a]);
}
/*
int Find(int x)//查询
{
    while(pre[x] != x)
        x = pre[x];
    return x;
}
 */
void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);

    if(a != b)
        pre[b] = a;
    //   pre[Find(b)] = Find(a);
}


int main()
{

    cin >>  n >> k;
    for(int i=1; i<=n; i++){

        cin >> a[i].first >> a[i].second;
    }
    getchar();

    init();


    char c;
    int i = 0;
    while(~(c = getchar())){
        if(c == 'O'){

            int tmp;

            cin >> tmp;

            store[i++] = tmp;

            for(int j=0; j<i-1; j++){
//                cout << store[j] << endl;
                if(JudgeDist(store[j], tmp)){
                    Union(store[j], tmp);
//                    cout << "Find:" << Find(store[i]) << "   " << Find(tmp) << endl;

                }
            }
        }
        else if(c == 'S'){
            int a, b;

            cin >> a >> b;

    //        cout << Find(a) << "   " << Find(b) << endl;

            if(Find(a) == Find(b))
                cout<<"SUCCESS"<<endl;
            else
                cout<<"FAIL"<<endl;
        }
        getchar();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值