POJ - 2236 Wireless Network(并查集)

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 33620 Accepted: 13978

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

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1010

using namespace std;

int pre[N];
int vis[N]; /// 记录电脑是否被修好

struct node
{
    int x,y;
} m[N]; ///记录电脑坐标

void Init(int n)
{
    for(int i=1; i<=n; i++)
    {
        pre[i] = i;
        vis[i] = 0; ///初始化所有电脑都是坏的
    }
}

int find(int x)
{
    if(pre[x] == x)
        return x;
    return pre[x] = find(pre[x]);
}

bool dis(int a,int b,int d)
{
    int dx = m[a].x - m[b].x;
    int dy = m[a].y - m[b].y;
    if(dx*dx + dy*dy <= d*d)   ///距离在d之内,可以连接
        return true;
    return false;
}

void join(int x,int y)
{
    int a = find(x);
    int b = find(y);
    if(a != b)
    {
        pre[a] = b;
    }
}

int main()
{
    int n,d;
    char op;
    scanf("%d %d",&n,&d);
    Init(n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d %d", &m[i].x, &m[i].y);
    }
    while(cin >> op)
    {
        if(op == 'O')
        {
            int pc;
            scanf("%d",&pc);
            vis[pc] = 1;
            for(int i=1;i<=n;i++)
            {
                ///如果电脑被修好,并且两台电脑距离在d之内,则连接它们
                if(vis[i] && i != pc && dis(i,pc,d))
                {
                    join(i,pc);
                }
            }
        }
        else if(op == 'S')
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(find(a) == find(b))
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    }
    return 0;
}

反思:

题目大意:地震的余震破坏了所有电脑的网络,现在开始修复网络,由于硬件限制,两台计算机只有在一定的距离内才能通信,如果A和B可以通信,C和A可以通信,则C可以和B通信。现在有两种操作:修复,输入O p,修复p电脑;测试,输入S p q,测试p和q之间是否可以通信。N为电脑数量,d是可以通信的最大距离,接下来N行分别包含电脑的坐标xi,yi,再接下来是一系列操作。

并查集问题。这里用了结构体存电脑的坐标,也可以用两个数组。需要一个额外的函数dis判断两台电脑之间是否小于距离d,只有距离小于d且被修好的电脑之间才能进行通信,否则不能通信。测试时,寻找两台电脑的祖先结点,如果相同则说明连通,可以通信,否则通信失败。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值