Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 50348   Accepted: 20619

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

[Submit]   [Go Back]   [Status]   [Discuss]

题意

   有n个坏掉的卫星,每次操作‘O’可以修好一个,如果卫星之间都是‘修好的’状态且间距小于‘d',就可以通讯。

思路

   并查集的时候判断一下两卫星之间距离即可。

CODE

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream> #define dbg(x) cout << #x << "=" << x << endl using namespace std;
const int maxn = ; int fa[maxn], dis[maxn];
int n,m,ans,cnt;
int d;
int canuse[maxn]; struct node {
int x, y;
}a[maxn]; void init()
{
for(int i = ; i <= n; i++) {
fa[i] = i;
}
} double cal(node a, node b) {
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} int fid(int x)
{
int r = x;
while(fa[r] != r) {
//if(cal(a[fa[r]],a[r]) <= d)
r = fa[r];
}
int i,j;///路径压缩
i = x;
while(fa[i] != r) {
//if(cal(a[fa[i]],a[r]) <= d) {
j = fa[i];
fa[i] = r;
i = j; }
return r;
} void join(int r1, int r2)///合并
{
int fidroot1 = fid(r1), fidroot2 = fid(r2);
if(fidroot1 != fidroot2) {
fa[fidroot2] = fidroot1;
}
} int main()
{
scanf("%d %d",&n, &d);
init();
for(int i = ; i <= n; ++i) {
scanf("%d %d",&a[i].x, &a[i].y);
}
getchar();
char ch[];
int p,q;
int cnt = ;
while(scanf("%s", ch) != EOF) {
//dbg(ch[0]);
if(ch[] == 'O') {
scanf("%d",&p);
//getchar();
canuse[cnt++] = p;
for(int i = ; i < cnt-; ++i) {
if(cal(a[canuse[i]],a[p]) <= double(d)) {
join(canuse[i],p);
}
}
}
if(ch[] == 'S') {
scanf("%d %d",&p,&q);
//getchar(); //join(p,q);
if(fid(p) == fid(q)) {
puts("SUCCESS");
}
else {
puts("FAIL");
}
}
}
return ;
}

最新文章

  1. 移动App崩溃的测试用例设计
  2. Yii CModel中rules验证规则
  3. web.xml常用元素
  4. XP系统取消开机硬件检查
  5. Android反编译APK
  6. dg error
  7. 一个问题:关于类型转换Type Cast(汇编讲解 as 语法)
  8. Redis(1)在windows环境下的安装和测试
  9. 基于数据形式说明杜兰特的技术特点的分析(含Python实现讲解部分)
  10. Ansible批量修改root密码
  11. scanf函数的返回值
  12. asp.net 去掉小数点后面多余的0,本身为0则不显示
  13. SSL 原理及 https 配置
  14. 20个命令行工具监控 Linux 系统性能【转载】
  15. 『TensorFlow』流程控制之tf.identity
  16. p2751 Job Processing
  17. Java实现后缀表达式建立表达式树
  18. pyenv管理多python版本
  19. mysql hive sql 进阶
  20. 判断设备(PC,安Android,iOS)

热门文章

  1. TFT液晶显示屏之绘图板应用
  2. kms在线激活windows和office
  3. R12客户表结构分析
  4. 简单说说常用的css选择器
  5. VBA-FileToFileUpdate
  6. mssql sqlserver 如何将一个日期数据转换为&quot;年份-月份&quot;的格式呢?
  7. 解决Python3下map函数的显示问题
  8. NIO学习笔记,从Linux IO演化模型到Netty—— Netty零拷贝
  9. JavaScript中条件分支语句和循环语句的使用,用简洁的代码实现强大功能
  10. [PKUWC2018]Minimax [dp,线段树合并]