并查集-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 ...
随机推荐
- DK1.5-JDK11各个新特性
摘要: 参考文献: https://blog.csdn.net/lsxf_xin/article/details/79712537 JDK各个版本的新特性 要了解一门语言,最好的方式就是要能从基础的版 ...
- PMP--0. 前言(闲言)
先说一句话给未来的自己:你一定会感谢你现在的努力,当你回看时,记得带着现在的心境和心愿.未来更好的明天. --2019.12.1禾木留 今天是正式发布的时间--2020.01.01,听着新年快乐的祝福 ...
- opencv —— remap 重映射
重映射的概念 重映射,就是把一幅图像中某位置的像素放置到另一个图片指定位置的过程. 实现重映射:remap 函数 将图像进行重映射几何变换,基于的公式为:dst (x, y) = src ( mapx ...
- ARC 064 F-Rotated Palindromes
题意 问有多少个长度为 \(N\) 且字符集大小为 \(K\) 的字符串可以通过回文串旋转 (把第一个字符移到最后)若干次得到.\(N,K\le 10^9\) 做法 设\(f_i\)为最小周期为\(i ...
- Python中关于日期的计算总结
1.获取当前时间的两种方法: 代码如下: import datetime,timenow = time.strftime("%Y-%m-%d %H:%M:%S")print now ...
- jsonArray jsonString list<Object> 之间转换
1.示例: package com.test.demo.pojo; import lombok.Data; import lombok.experimental.Accessors; /** * @p ...
- ftp 拉去远程文件脚本
ftp 拉去远程文件脚本 cat ftp.sh #!/bin/bash ftp -i -n 192.168.1.1 << EOF user ftpadmin gaofeng binary ...
- 对象的三大特性(封装、继承、多态)——java
一. 面向对象的三大特征 1. 封装---->减少了大量的冗余代码---->封装将复杂的功能封装起来,对外开放一个接口,简单调用即可.将描述事物的数据和操作封装在一起,形成一个类:被封装的 ...
- 轻量级RPC设计与实现第五版(最终版)
在最近一段时间里,通过搜集有关资料加上自己的理解,设计了一款轻量级RPC,起了一个名字lightWeightRPC.它拥有一个RPC常见的基本功能.主要功能和特点如下: 利用Spring实现依赖注入与 ...
- IO流学习之字节流(一)
IO流(Input/Output) 简介: 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数 ...