POJ- 2236 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

题意:有n台计算机,最开始都是坏的,如果两台计算机的距离小于等于d的话,就说明他们之间可以连接(当然前提是有一台计算机已经被修复了),现在有两个操作,O p 代表修复p这台计算机,S p q代表检查p和q这两台计算机是否可以连接(不一定是直接连接,可以通过某些中间计算机的帮助)。

思路:因为最开始所有计算机都是坏的,所以当修复了某台计算机以后,让所有已经修复的计算机与它相连且他们之间的距离要小于等于d。检查的时候就只需要检查这两台计算机的父亲是否相同就行了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=10010;
#define eps 1e-8

struct node
{
    int x,y;
} a[maxn];
int n,d;
int pre[maxn],vis[maxn];

int Find(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(i!=r)
    {
        j=pre[i];
        pre[j]=r;
        i=j;
    }
    return r;
}

void join(int x,int y)
{
    int fx=Find(x),fy=Find(y);
    if(fx!=fy)
        pre[fx]=fy;
}

int judge(int x1,int y1,int x2,int y2)
{
    if(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=d*d)
        return 1;
    else return 0;
}

int main()
{
    cin>>n>>d;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i].x>>a[i].y;
        pre[i]=i;    //初始化每个的祖先都为本身
    }
    char c;
    int X,Y,Z;
    while(cin>>c)
    {
        if(c=='O')
        {
            cin>>Z;
            vis[Z]=1;     //记录该台计算机已经被修复
            for(int i=1; i<=n; i++)
            {
                if(vis[i]&&judge(a[Z].x,a[Z].y,a[i].x,a[i].y)&&i!=Z)  //如果i已经被修复且i与Z之间的距离小于d的话就将他们合并
                    join(Z,i);
            }
        }
        else
        {
            cin>>X>>Y;
            if(Find(X)==Find(Y)) cout<<"SUCCESS"<<endl;
            else cout<<"FAIL"<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值