POJ—2236 Wireless Network

简介: POJ—2236 Wireless Network

问题:

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的可以建立通信,修复计算机之后看看能不能两个计算机之间可以建立通信。对于修复一台电脑可以把该电脑标记,然后去寻找当前计算机与剩余的计算机之间是否满足通信的条件,既该电脑被修复且之间的距离小于d,然后让满足条件的其他电脑认该电脑为祖宗。

程序代码:

#include<stdio.h>
#include<math.h>
int f[50000]={0},n,m,d,a[100100],x[50000],y[50000];
int flag;
void init()
{
  int i;
  for(i=1;i<=n;i++)
    f[i]=i;
  
}
int juli(int p,int q)
{
  int sum;
  return sum=(x[p]-x[q])*(x[p]-x[q])+(y[p]-y[q])*(y[p]-y[q]);
}
int getf(int v)
{
  if(f[v]==v)
    return v;
  else
  {
    f[v]=getf(f[v]);
    return f[v];
  }
} 
void merge(int v)
{
  int t1,t2,sum,j;
  t1=getf(v);
  for(j=1;j<=n;j++)
  {
    if(a[j]==1&&v!=j)
    {
      if(sqrt(juli(v,j)*1.0)<=d)
      { 
        t2=getf(j);
        if(t1!=t2)
        {
          f[t2]=t1;
        //  printf("%d %d\n",t2,t1);
        }
      } 
    }
  } 
}
int main()
{
  int k,t,c,i;
  char s;
  while(scanf("%d%d",&n,&d)!=EOF)
  {
    init();
    for(i=1;i<=n;i++)
      scanf("%d%d",&x[i],&y[i]);
    while(scanf("%c",&s)!=EOF)
    {
      if(s=='O')
      {
        scanf("%d",&m);
        a[m]=1;
        merge(m); 
      }
      if(s=='S')
      {
        scanf("%d%d",&k,&t);
        if(getf(k)==getf(t))
          printf("SUCCESS\n");  
        else
          printf("FAIL\n"); 
      }
    } 
  }
  return 0;
}
相关文章
|
存储 关系型数据库 MySQL
第八章,索引的创建与设计原则(2)
第八章,索引的创建与设计原则
60 0
|
Linux Windows
fatal error C1083 无法打开包括文件 “sys/time.h” No such file or directory
fatal error C1083 无法打开包括文件 “sys/time.h” No such file or directory
1055 0
|
存储 缓存 负载均衡
揭秘淘宝286亿海量图片存储与处理架构,互联网营销
  【IT168 专稿】8月27日下午,在IT168系统架构师大会存储与系统架构分论坛上,淘宝网技术委员会主席,淘宝网核心工程师章文嵩向我们详细介绍了淘宝网图片处理与存储系统的架构。章文嵩博士的演讲日程包括了淘宝的整个系统架构、淘宝图片存储系统架构,淘宝网独立开发的TFS集群文件系统,前端CDN系统以及淘宝网在节能服务器方面的应用和探索。
2034 0
|
1天前
|
人工智能 运维 安全
|
3天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
363 123
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
6天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
540 107
|
2天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
187 127
|
2天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
222 124

热门文章

最新文章