E - 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

题目需要好好理解下,主要的要注意的就是,嗯这两台电脑能被连接 首先它得被修好,其次就是他们之间的距离要小于等于d大体就是这样子

代码:

//我理解错题目意思了应该说只有在被修好的情况下 才可以开始说他们相连难怪无从下手
//前提条件是该电脑已经被修好了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
int n,d;
int pre[1010];
int posx[1010],posy[1010];
double dis[1010][1010];
bool flag[1010];
void init(int n)
{for(int i=1;i<=n;i++)pre[i]=i;
}
int find(int r)
{if(r==pre[r])return r;else return pre[r]=find(pre[r]);
}
void merge(int x,int y)
{int fx=find(x);int fy=find(y);if(fx==fy)return;elsepre[fx]=fy;
}
int main()
{scanf("%d%d",&n,&d);char str[200];for(int i=1;i<=n;i++){scanf("%d%d",&posx[i],&posy[i]);}//把他们的位置都输进去 那个i表示的就是哪个点;int k=0;for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){dis[i][j]=dis[j][i]=sqrt((posx[j]-posx[i])*(posx[j]-posx[i])+(posy[j]-posy[i])*(posy[j]-posy[i]));}}//这是他们的距离;init(n);//先进行一波操作int x,y;while(scanf("%s",str)!=EOF){if(str[0]=='O'){scanf("%d",&x);if(flag[x])continue;flag[x]=true;for(int i=1;i<=n;i++){if(flag[i]&&dis[x][i]<=d){merge(x,i);}}}else if(str[0]=='S'){scanf("%d%d",&x,&y);if(find(x)==find(y))printf("SUCCESS\n");else printf("FAIL\n");}}return 0;
}

E - Wireless Network(并查集)相关推荐

  1. POJ 2236 Wireless Network 并查集

    Wireless Network 并查集 Crawling in process... Crawling failed Time Limit:10000MS     Memory Limit:6553 ...

  2. POJ 2236 - Wireless Network ( 并查集 )

    题意 一个计算机网络里的计算机都坏了, 现在有两种操作, "O p"代表修复了p机器, "S p q"代表检查 p, q 两台机器是否连接( 直线距离<= ...

  3. CF-1023F.Mobile Phone Network(并查集缩点)

    CF-1023F.Mobile Phone Network(并查集缩点) 题目链接 题意 你手里有K条边还没有分配权值,已经存在M条边带权值,如何给你手中的边分配权值,使得K条边都在最后的最小生成树中 ...

  4. UVA 1329 Corporative Network(并查集:路径压缩)

    UVA 1329 Corporative Network(并查集:路径压缩) 2014年03月09日 00:19:33 阅读数:914 UVA 1329 Corporative Network(并查集 ...

  5. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  6. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  7. [kuangbin带你飞]专题五 并查集 A - Wireless Network

    A - Wireless Network 题目链接:https://vjudge.net/contest/66964#problem/A 题目: An earthquake takes place i ...

  8. java并查集计算机网络连通,poj2236 Wireless Network(并查集)

    题意:有n台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能.若两台电脑能相互通信,则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑.给出所有电脑的坐标位置,对 ...

  9. 【POJ - 2236】Wireless Network (并查集)

    题干: An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up ...

  10. (并查集)Wireless Network

    问题: 地震发生在东南亚. ACM(亚洲合作医疗团队)已经与膝上电脑建立了无线网络,但是一次意外的余震袭击,网络中的所有计算机都被打破了.计算机一个接一个地修复,网络逐渐开始工作.由于硬件限制,每台计 ...

最新文章

  1. android setting.java,Android Setting 启动流程总结
  2. 第一课.深度学习简介
  3. background 距离右边固定距离
  4. iOS架构-静态库.framework之资源文件打包bundle(6)
  5. form表单用js提前执行函数若不成功则不提交_如何用Jmeter做接口自动化测试?跟着操作一次就明白了...
  6. 上线清单 —— 20 个 Laravel 应用性能优化项
  7. 出现$ref的原因及解决方案
  8. 大学学好高数的爆炸性意义!
  9. java从键盘上录入任何整数,输出该整数的阶乘
  10. [leetcode]5169. 日期之间隔几天
  11. AudioSession property 详解及使用方法,包括检测是否有声音正在运行,音量更改等。...
  12. 矢量绘图软件:Sketch最新版82.1中文版
  13. 以太坊Whisper协议
  14. 红帽子安装oracle,红帽子AS4安装oracle9i
  15. js函数提升和变量提升_关于在js中提升的真相
  16. 混合高斯背景建模算法GMM
  17. 开发者收到“加料”的假 Offer,害上家被盗近 6.25 亿美元!
  18. 用你的浏览器来静态分析网站源码——初级漏洞赏金猎人指南
  19. 【Python】一、Python程序运行方式
  20. 给ubuntu中谷歌配置host

热门文章

  1. 在复杂度o(n)的要求下找到n个数的中位数(n为奇数)_啊这,一道找中位数的算法题把东哥整不会了…...
  2. @retention注解作用_前端初学SpringBoot系列(三)自定义注解的学习与练习
  3. Java多线程总结之线程安全队列Queue
  4. 咖啡品牌“三顿半”完成千万级Pre-A轮融资,峰瑞资本投资...
  5. 区块链+”来了,区块链金融将如何颠覆传统金融
  6. Java基础知识回顾--正则表达式
  7. CAS3.5.x(x1)支持OAuth2 server
  8. 网站遭遇DDoS***的解决方案
  9. MongoDB的web可视化管理工具
  10. 《哈利·波特》作者曾有自杀念头