A - Simple String Problem

Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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 //题意是,电脑都坏了,可以一个一个修复,电脑只能在一定的距离内才能连通,在询问是否连通的时候输出是否连通。
第一行是 n ,d ,d 是代表电脑能连通的最大距离,然后是 n 行坐标,在然后是命令 O 代表修复电脑,S 代表查询两个电脑是否连通 并查集简单的应用,压缩了路径就只用了1秒
1125 ms
 #include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std; struct Com
{
int x,y;
int on;
}com[];
int p[]; int find(int x)
{
if (x!=p[x])
p[x]=find(p[x]);
return p[x];
} int Distance(int a,int b,double x)
{
double j=(com[a].x-com[b].x)*(com[a].x-com[b].x);
double k=(com[a].y-com[b].y)*(com[a].y-com[b].y);
double l=sqrt(j+k);
if (x<l)
return ;
return ;
} int main()
{
int n,i;
double s;
scanf("%d%lf",&n,&s);
for (i=;i<=n;i++)
{
scanf("%d%d",&com[i].x,&com[i].y);
com[i].on=;
p[i]=i;
}
char str[];
int k;
while(scanf("%s",str)!=EOF)
{
if (str[]=='O')
{
scanf("%d",&k);
if (com[k].on==)
continue;
com[k].on=;
for (i=;i<=n;i++)
{
if (i==k||com[i].on==) //未修复
continue;
if (Distance(i,k,s))//距离超出
continue;
int fa=find(k);
int fb=find(i);
p[fa]=fb;
}
}
if (str[]=='S')
{
int a,b;
scanf("%d%d",&a,&b);
int fa=find(a),fb=find(b);
if (fa==fb)
{
printf("SUCCESS\n");
//没有这种情况
/* if (a!=b)
printf("SUCCESS\n");
else if (a==b&&com[a].on)
printf("SUCCESS\n");
else
printf("FAIL\n");*/
}
else
printf("FAIL\n");
}
}
return ;
}

 
 

最新文章

  1. [PHP][位转换积累]之异或运算的简单加密应用
  2. Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中
  3. C#:DataTable映射成Model
  4. hibernate延迟加载(get和load的区别)
  5. SQL语句 - 基本查询
  6. UVA 562 Dividing coins --01背包的变形
  7. ylbtech-LanguageSamples-Yield
  8. MFC之MessageBox用法
  9. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, VALID_ARCHS=armv7 armv7s)
  10. ARM裸编程系列---UART
  11. Android px、dp、sp之间相互转换 系统默认12 sp
  12. python的高级数组之稀疏矩阵
  13. Java框架spring 学习笔记(十一):aop相关概念
  14. ping百度域名时的收获
  15. CodeFroces-- 511div2 C. Enlarge GCD
  16. 74.CocoaPods安装和使用教程
  17. JAVA SE ArrayList 底层实现
  18. P3402 最长公共子序列
  19. Zeroc Ice 3.6.1 生成 vs2015 c++ 版本库及相关配置
  20. Linux下Google Test (GTest)测试环境搭建步骤

热门文章

  1. Windows下cwRsyncServer双机连续同步部署
  2. 116 - Unidirectional TSP(DP)
  3. 20160208.CCPP体系具体解释(0018天)
  4. Windows为什么双击打开‘我的电脑’, 没有了‘前进’‘ 后退’‘向上’等按钮?
  5. 如何安装Android SDK Emulator
  6. 阿里CI/CD、DevOps、分层自动化技术
  7. asp.net core mvc视频A:笔记3-4.母版页与部分视图
  8. ps选框工具全解
  9. 用第三方下载工具下载官方XCode独立安装包的方法
  10. 安装 redis [standlone模式]