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

 #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 ;
}


												

最新文章

  1. OpenShare新功能@2014年10月
  2. MyTask3
  3. LA 6450 Social Advertising
  4. Thread interrupt方法解析
  5. aix上使用裸设备安装oracle10g数据库
  6. mysql添加远程用户
  7. Java中String转换Double类型 Java小数点后留两位
  8. 列表(list)之一定义 添加 删除 排序 反转 索引等其他操作
  9. [POI2016]Nim z utrudnieniem
  10. JeeCMS v7 SSRF导致任意文件写入
  11. Vmware ESXi 的虚拟机的开机自启动
  12. System Monitor for Mac(系统监控工具)破解版安装
  13. 富文本编辑器kindeditor插件
  14. Tomcat get 中文乱码
  15. bnu 51636 Squared Permutation 线段树
  16. Ritchie Lawrence 批处理函数库中英文版
  17. 【BZOJ】1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富(dp)
  18. XDU 1031
  19. Mysql5.7基于日志主从复制
  20. Redis+Lua实现限流

热门文章

  1. PMP--1.7 项目治理
  2. 如何实现广告响应式滚动 OwlCarousel2
  3. python简明教程
  4. &lt;Wonder Woman&gt; 摘抄
  5. Umi 小白纪实(二)—— model 的注册与使用
  6. Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长
  7. MySQL 8 服务器组件
  8. Python 安装 virturalenv 虚拟环境
  9. 剑指offer-面试题7-重建二叉树-二叉树
  10. Math Magic ZOJ - 3662