并查集-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 ...
随机推荐
- opencv —— contourArea、arcLength 计算轮廓面积与长度
计算轮廓面积:contourArea 函数 double contourArea(InputArray contour, bool oriented = false); contour,输入的二维点集 ...
- 软件模拟spi的注意事项
前几天遇到了软件模拟spi的时候,读和写不一致的现象,后来仔细研究了一下,其实是时序性问题不对. spi的有四种时序,硬件实现的时候,很简单,初始化后直接调用api即可.但是软件模拟就比较麻烦. 举例 ...
- mysql 查询语句的执行顺序(重重点)
一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...
- python基础入门之一 —— 变量与运算符
1.标识符 由数字,字母,下划线组成 不能由数字开头 不能使用内置关键字 严格区分大小 2.数据类型 数值:int (整型) float(浮点型) 布尔型:True(真) False(假) str ( ...
- Redisson源码学习之RedissonFairLock
博客待整理,先只是把源码看了.... 后面不再备注redis中的命令含义了,这样备注写太多了不好阅读. package org.redisson; import java.util.Arrays; i ...
- RHEL7开机不能正常进入系统(图形化界面)
今天在重启RHEL7的虚拟机后一直无法正常开机,一直提示输入管理员密码,如下图所示: 输入密码后进入命令行模式,经排查出现此现象的问题是在挂载银盘的时候文件格式写错,在格式化硬盘的时候格式化的是xfs ...
- Bringing up interface eth0: Device eth0 does not seem to be presen
在公司的电脑虚拟机上安装了centos 6.5 ,然后我把他克隆下来用在家里电脑的虚拟机上,打开后查看ip,发现只有回环地址lo,没有eth0, 于是重启网络 输入 service network r ...
- EasyUI笔记(五)表单
本系列只列出一些常用的属性.事件或方法,具体完整知识请查看API文档 Form(表单) 创建一个简单的HTML表单.构建一个包含id.action和method值的表单元素. <form id= ...
- wxpython 简单例子:显示文本框的窗口显示鼠标位置
简单例子来自教程: #!/bin/env python import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__ ...
- Java商城秒杀系统的设计与实战视频教程(SpringBoot版)
课程目标掌握如何基于Spring Boot构建秒杀系统或者高并发业务系统,以及构建系统时采用的前后端技术栈适用人群Spring Boot实战者,微服务或分布式系统架构实战者,秒杀系统和高并发实战者,中 ...