Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 24497   Accepted: 10213

Description

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

Source

 #include <iostream>
#include <cstdio> #define MAX_N 1000+5 using namespace std; struct point{int x;int y;int jud;};
point p[];
int par[MAX_N];//父节点
int depth[MAX_N];//深度 void init(int n){
for(int i=;i<=n;i++){
par[i]=i;
depth[i]=;
}
}
int find_father(int t){
if(t==par[t]){
return t;
}else{
return par[t]=find_father(par[t]);
//实现了路径压缩
}
}
void unite(int t1,int t2){
int f1=find_father(t1);
int f2=find_father(t2);
if(f1==f2){
return ;
}
if(depth[f1]<depth[f2]){
par[f1]=f2;
}else{
par[f2]=f1;
if(depth[f1]==depth[f2]){
depth[f1]++;
//记录深度
}
}
} bool same(int x,int y){
return find_father(x)==find_father(y);
} int distanc_2(point a,point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} int main()
{
int n,d;
char c;
int t1,t2;
scanf("%d %d",&n,&d);
init(n);
for(int i=;i<=n;i++){
scanf("%d %d",&p[i].x,&p[i].y);
p[i].jud=;
}
getchar();
while(scanf("%c",&c)!=EOF){
if(c=='O'){
scanf("%d",&t1);
getchar();
p[t1].jud=; for(int i=;i<=n;i++){
if(i!=t1&&p[i].jud==){
if(distanc_2(p[t1],p[i])<=d*d){
unite(t1,i);
}
}
}
}else{
scanf("%d %d",&t1,&t2);
getchar();
if(same(t1,t2)){
printf("SUCCESS\n");
}else{
printf("FAIL\n");
}
}
}
return ;
}

最新文章

  1. DotNet的JSON序列化与反序列化
  2. 解决VirtualBox只能安装32位系统的问题
  3. 当call/apply传的第一个参数为null/undefined的时候js函数内执行的上下文对象是什么呢?
  4. seajs封装js方法
  5. VUE 入门笔记
  6. x square x cube
  7. WPF 获取IP地址
  8. ruby学习总结01
  9. ISO中运行时简单使用及KVC补充
  10. C#实现XML文件数据库存储
  11. 如何在C#中实现图片缩放
  12. vector的内存分配与释放
  13. Weblogic虚拟目录
  14. iOS 开源一个高度可定制支持各种动画效果,支持单击双击,小红点,支持自定义不规则按钮的tabbar
  15. FastJson--阿里开源的速度最快的Json和对象转换工具
  16. AngularJS1.X学习笔记10-自定义指令(下)
  17. 「ZJOI Day2」游记
  18. qt无法定位程序输入点 于动态链接库 qt5core.dll
  19. 二分 poj 3273
  20. 浅谈sql中的in与not in,exists与not exists的区别以及性能分析

热门文章

  1. Javascript:来一个AJAX封装函数
  2. 通过实战理解C语言精要——函数篇
  3. 浅谈php生成静态页面
  4. jprofiler_监控远程linux服务器的JVM进程(实践)
  5. Centos7下安装python,查看python版本
  6. NSUserDefaults:熟悉与陌生(转)
  7. bzoj2555: SubString
  8. ARM学习 之 如何在向内核写入系统调用
  9. 【BZOJ-3039&amp;1057】玉蟾宫&amp;棋盘制作 悬线法
  10. &lt;!DOCTYPE html&gt;作用