POJ 2236 Wireless Network(并查集)

题目链接:

       POJ 2236 Wireless Network

题目:

Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions:33981 Accepted: 14116

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(编号1~N)和两台计算机可以直接通信的最大距离d,下面n行输入对应编号i的坐标xi和yi。从n+1行开始输入操作:

1.“O p”(1 <= p <= N),表示修复计算机p。   注意这是大写O不是0。

2.“S p q”(1 <= p,q <= N),这意味着测试计算机p和q是否可以通信。 

       显然这是个并查集问题,如果两台电脑都被修复且其距离在最大距离内,就可以将其连起来。

AC代码如下:

#include <stdio.h>
#include <math.h>
#define maxn 1001
struct Node
{
    int x,y,flag;
} a[maxn];
int pre[maxn],d;
int Find(int x)
{
    return x==pre[x]?x:Find(pre[x]);
}
void is_join(int q,int p)
{
    int n=Find(q),m=Find(p);
    int z=(a[q].x-a[p].x)*(a[q].x-a[p].x)+(a[q].y-a[p].y)*(a[q].y-a[p].y);
    if (a[q].flag&&z<=d*d&&n!=m)
    {//由题可知q已被修复,如果 a[q].flag为真则两者均被修复
     //z为两台电脑之间距离的平方,如果z<=d*d则其能进行通信
     //如果n!=m代表两者还有连起来,将他们连起来
     //上面三个同时成立时,将p的最上级赋为q的最上级
        pre[n]=m;
    }
}
int main()
{
    int n,e,f;
    char c;
    scanf("%d%d",&n,&d);
    for (int i=1; i<=n; i++)
    {
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].flag=0;
        pre[i]=i;
    }
    getchar();
    while(~scanf("%c ",&c))
    {
        if (c=='O')
        {
            scanf("%d",&e);
            if (a[e].flag)
                continue;
            a[e].flag=1;
            for (int i=1; i<=n; i++)
                is_join(i,e);
        }
        else if (c=='S')
        {
            scanf("%d%d",&e,&f);
            if (Find(e)==Find(f))
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值