poj 2236

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 题意:有一堆坏电脑,每个电脑都有一个二维坐标。有两种操作,修一台电脑和在线询问两台电脑是否可以连通。若两台电脑相距小于等于d或者可以经过第三方电脑连通则连通。给出询问操作的答案。
题解:显然并查集。因为修和询问都是实时在线操作,所以修完一台就看看有没有能和它相连的。
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; int dx[maxn], dy[maxn], Rank[maxn], par[maxn], re[maxn];
double dis[maxn][maxn];
int N;
double D; void init()
{
for (int i = ; i <= N; i++) {
Rank[i] = ; par[i] = i;
}
} int find(int x)
{
if (par[x] == x) return x;
else return find(par[x]);
} void unite(int xx, int yy)
{
int x = find(xx); int y = find(yy);
if (x == y) return;
if (Rank[x] < Rank[y]) par[x] = y;
else {
par[y] = x;
if (Rank[x] == Rank[y]) Rank[x]++;
}
} bool same(int x, int y)
{
return find(x) == find(y);
} int main()
{
cin >> N;
cin >> D;
init();
for (int i = ; i <= N; i++) cin >> dx[i] >> dy[i];
for(int i=;i<=N;i++)
for (int j = i + ; j <= N; j++) {
dis[i][j] = dis[j][i] = sqrt((double)(dx[i] - dx[j])*(dx[i] - dx[j]) + (double)(dy[i] - dy[j])*(dy[i] - dy[j]));
}
char op; int p, q;
int cnt = ;
while (cin>>op)
{
if (op == 'O') {
cin >> p;
re[cnt++] = p;
for (int i = ; i < cnt - ; i++)
if (dis[re[i]][p] <= D)
unite(re[i], p);
}
else
{
cin >> p >> q;
if (same(p, q))
cout << "SUCCESS" << endl;
else cout << "FAIL" << endl;
}
}
return ;
}

最新文章

  1. linux中grep的应用
  2. CSS兼容各浏览器的hack
  3. Asp.net通过模板(.dot/Html)导出Word,同时导出图片
  4. UVa 11889 Benefit(数论)
  5. java 1.7
  6. [反汇编练习] 160个CrackMe之013
  7. C++ Dialog Box Command IDs
  8. JS中==和===的区别
  9. c# 菜单无限极分类-利用递归
  10. Windows Phone 独立存储资源管理器工具
  11. Oracle按不同时间分组统计
  12. spring boot + neo4j restful
  13. 浅谈Quartz.Net 从无到有创建实例
  14. Shell命令-文件及目录操作之chattr、lsattr
  15. 【hdu 6161】Big binary tree(二叉树、dp)
  16. Dividing the Path POJ - 2373(单调队列优化dp)
  17. 01-TCP/IP概述
  18. Android将Log写入文件
  19. (转)FFMPEG filter使用实例(实现视频缩放,裁剪,水印等)
  20. linux分析、诊断及调优的必备“杀器”之一

热门文章

  1. ms12-020复现-xp蓝屏
  2. [转]C#改变无边框窗体大小
  3. 2019-10-11-VisualStudio-配置多进程调试快捷键启动项目
  4. 2018-2-13-visual-Studio-无法调试,提示程序跟踪已退出
  5. Codeforces 142B(二分染色、搜索)
  6. agc014F Strange Sorting
  7. zabbix自定义监控redis
  8. GIT → 00:GIT学习大纲
  9. java开发岗位面试整理
  10. GIT生成公钥和私钥