并查集-E - Wireless Network
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 4Sample Output
FAIL
SUCCESS
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std; int x[], y[];//存放电脑的横纵坐标
int dis[][];//存放电脑i和电脑j之间的距离
int parent[];//并查集的关系联系数组
int n, d, a, b;
int is[];//判断电脑是否已修复
char ask[]; void init(int n){//联系数组初始化
for(int i=; i<=n; i++)
parent[i] = i;
} int find(int x){
if(x == parent[x])
return x;
return parent[x] = find(parent[x]);
} void unionit(int a, int b){
int fa = find(a);
int fb = find(b);
parent[fb] = fa;
} int main(){
scanf("%d %d", &n, &d);
for(int i=; i<=n; i++){
scanf("%d %d",x+i, y+i);
is[i] = ;
}
for(int i=; i<=n; i++)//求两电脑间距离
for(int j=i+; j<=n; j++)
dis[i][j] = dis[j][i] = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]);//此处求出的距离未开根 init(n);
while(~scanf("%s",ask)){
if(ask[] == 'O'){
scanf("%d",&a);
if(is[a]) continue;//若电脑已修复,则没必要进行下面操作
is[a] = ;
for(int i=; i<=n; i++)//逐一遍历,找到在可连接范围内的电脑并与之联系
if(is[i] && dis[i][a] <= d*d)
unionit(i, a);
}else{
scanf("%d %d",&a, &b);
if(find(a) == find(b))//判断两台电脑是否可相互联系
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return ;
}
并查集-E - Wireless Network的更多相关文章
- [kuangbin带你飞]专题五 并查集 A - Wireless Network
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- (并查集) Wireless Network --POJ --2236
链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- 【并查集】POJ2236-Wireless Network
[题目大意] 已知每一台电脑只能与它距离为d的电脑相连通,但是两台电脑间可以以第三台作为媒介连接.现在电脑全被损坏.每次可以进行两个操作中的一个,或是修好一台电脑,或是查询两台电脑是否连通. [思路] ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...
随机推荐
- 测试.NET core MiddleWare 运行逻辑
话不多说,直接开整. 首先创建一个.NET CORE web 工程,创建完成之后,会自动创建相关文件如图(本示例基于.NET CORE 3.0): 打开Startup.cs可以看到在Configure ...
- Android实战项目——家庭记账本(七)
今天主要实现了登录注册功能的客户端和服务端,但由于短信接口调用出现问题,导致注册功能还不完整. 截止到今天,APP的功能已经基本完成,后续还会陆陆续续的完善各功能模块与服务端的交互,因为需要和云端关联 ...
- 笔记本磁盘中OEM分区的使用
(1).开机进入系统前,按F8,进入Windows 10的高级启动选项,选择“修复计算机”. (2).选择键盘输入方法. (3).如果有管理员密码,需要输入:如果没有设置密码,直接“确定”即可. (4 ...
- 伟大的悲剧——记CSP2019
伟大的悲剧——记CSP2019 就算伟大,依然悲剧…… 现在我好恨自己 我……差一点……就是省一了…… 这一点是多少呢? 2分! 1名! 省一65人,我第66! 唉……太悲催了…… jx的分数线居然还 ...
- 清北学堂—2020.1提高储备营—Day 2 afternoon(线段树、树状数组)
qbxt Day 2 afternoon --2020.1.18 济南 主讲:李佳实 目录一览 1.线段树 2.二叉搜索树(略过) 3.树状数组 总知识点:基础数据结构(本人初学感觉好难) 一.线段树 ...
- java设计模式学习笔记--接口隔离原则
接口隔离原则简述 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应建立在最小的接口上 应用场景 如下UML图 类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类 ...
- Windows通过DOS命令行设置IP地址
@rem 设置固定IP地址netsh interface ip set address "本地连接" static 192.168.1.200 255.255.255.0 192. ...
- 《茶余饭后小故事》MV*、MVC、MVP、MVVM的前世今生
今天我们讲讲历史,讲讲故事,不扯高深术语. MV*表示的意思是:M(Model逻辑层) + View(视图层) + *(中间者).上帝提出了这个逻辑与视图分离,用中间者进行连接的伟大思想,并将实现这个 ...
- 又一种获取redis cluster slots分布的小脚本
需要:昨晚学习了cluster slots命令,之前写的脚本,有可以换种方法获取到redis cluster slots 分布情况. cluster slots的结果如下: 4 5460 5.5.5. ...
- php 获取当前目录和当前文件夹
<?php /** * PHP获取路径或目录实现 */ //魔术变量,获取当前文件的绝对路径 echo "__FILE__: ========> ".__FILE__; ...