POJ Wireless Network (并查集)

Wireless Network




Description

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.

Input

The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:
1. "O p" (1 <= p <= N), which means repairing computer p.
2. "S p q" (1 <= p, q <= N), which means testing whether computer p and q can communicate.

The input will not exceed 300000 lines.

Output

For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.

Sample Input

4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4

Sample Output

FAIL
SUCCESS

题目大意两台电脑可以直接对话也可以间接对话,间接对话需要经过中间若干台电脑,题意有点隐晦,不过看样例可以分析出来。然后就变成了一个标准的并查集问题。

解题思路开一个数组标记当前编号的电脑是否已经被修好,然后对两台都被修好并且在一定范围内的电脑跑并查集,将他们连接起来。然后每次询问的时候查看当前两台电脑是否在同一个集合中即可。

AC代码

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int n,m;
int ans;
P p[maxn];//储存每一台电脑的坐标
int ok[maxn];//判断当前电脑是否被修好
ll dis[maxn][maxn];//保存两者之间距离 x^2 + y^2
int city[maxn];//并查集数组
int getcity(int point)
{
    return city[point] == point ? point : city[point] = getcity(city[point]);
}

bool connect(int point1,int point2)
{
    int t1 = getcity(point1);
    int t2 = getcity(point2);
    if(t1!=t2)
    {
        city[t2] = t1;
        return true;
    }
    return false;
}

void init()
{
    for(int i =1 ; i<=n ; i++)
    {
        city[i] = i;
    }
}
int main()
{
    //freopen("test.txt","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int d;
    cin>>n>>d;
    for(int i = 1; i<=n ; i++)
    {
        cin>>p[i].first>>p[i].second;
    }
    for(int i = 1; i<=n ; i++)//预处理电脑两两之间的距离
    {
        for(int j = 1; j<=n ; j++)
        {
            ll temp = (p[i].first - p[j].first)*(p[i].first - p[j].first) + (p[i].second - p[j].second)*(p[i].second - p[j].second);
            dis[i][j] = temp;
        }
    }
    init();
    char ch;
    while(cin>>ch)
    {
        if(ch=='O')
        {
            int pos;
            cin>>pos;
            ok[pos] = 1;
            for(int i = 1; i<=n ; i++)
            {
                if(ok[i] && dis[pos][i] <= 1LL*d*d)
                    connect(pos,i);
            }
        }
        else
        {
            int r , s;
            cin>>r>>s;
            if(ok[r] && ok[s] && getcity(r)==getcity(s))
                cout << "SUCCESS" << endl;
            else
                cout << "FAIL" << endl;
        }
    }
    //cout << ans << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值