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台坏的电脑,给出所有电脑的坐标(X,Y)。然后给出2种指令:

O U 指令表示修复编号为U的电脑(电脑只有被修复之后才能进行数据连接)。

S U V 指令要求你返回 编号U电脑和 编号V电脑是否可以进行数据连接(只有U和V电脑同时被修复且连通才行)。

现在给出一个距离d,只有两台被修复的电脑且他们之间的距离<=d时才可以进行连通。且如果A与C连通,C与B连通,那么A与B可以通过C连通(也算A与B连通了)。

现在要你回答每一条S U V指令,如果U与V连通,输出SUCCESS ,否则输出FAIL。

[分析]:

每修复一台电脑,就把它与所有距离它不超过D已经被修复的电脑连通(即合并两者的连通分量)。每次查询只需要看被查询的两台电脑是否被修复且都在同一个连通分量即可。

[代码]:
#include<iostream>
#include<algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int fa[maxn],vis[maxn];
int n,d;
struct node
{
int x,y;
}p[maxn]; void init()
{
for(int i=;i<=n;i++)
fa[i]=i;
memset(vis,,sizeof(vis));
} int find(int x)
{
return fa[x]==x?x:find(fa[x]);
} void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fx]=fy;
} bool dis(int x, int y){
float a = p[x].x - p[y].x;
float b = p[x].y - p[y].y;
if(a * a + b * b <= d * d) return true;
else return false;
}
int main()
{
char s[];
int x,y,a,b; scanf("%d%d",&n,&d); for(x=;x<=n;x++)
scanf("%d %d",&p[x].x , &p[x].y); init();
while(~scanf("%s",s))
{
if(s[] == 'O')
{
scanf("%d",&x);
vis[x]=;
for(y=;y<=n;y++)
{
if(vis[y] && dis(x,y))
join(x,y);
}
}
else
{
scanf("%d %d",&x, &y);
if(vis[x] && vis[y] && find(x) == find(y))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}

09/15

 

POJ 2236 Wireless Network [并查集+几何坐标 ]的更多相关文章

  1. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  2. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

  3. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  4. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  5. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  6. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  7. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  9. poj 2236 Wireless Network 【并查集】

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 706 ...

随机推荐

  1. BZOJ4537 HNOI2016最小公倍数(莫队+并查集)

    考虑边只有一种权值的简化情况.那么当且仅当两点可以通过边权<=x的边连通,且连通块内最大边权为x时,两点间存在路径max为x的路径.可以发现两种权值是类似的,当且仅当两点可以通过边权1<= ...

  2. hdu6121 build a tree(树)

    题解: 可以考虑每一层结点的子树大小 必定满足下面的情况,即 a,a,a,a,a,a,b,c,c,c,c........ 然后每一层依次往上更新,结果是不变的 一共有logn层,所以依次扫上去,统计结 ...

  3. [洛谷P1879][USACO06NOV]玉米田Corn Fields

    题目大意:有一个$n\times m$的矩阵,$(1 \leq m \leq 12; 1 \leq n \leq 12)$,想在其中的一些格子中种草,一些格子不能种草,且两块草地不相邻.问有多少种种植 ...

  4. android OTA升级包制作

    0.签名 java -Xmx2048m -jar out/host/linux-x86/framework/signapk.jar -w build/target/product/security/t ...

  5. 使用babel把es6代码转成es5代码

    第一步:创建一个web项目 使用命令:npm init 这个命令的目的是生成package.json. 执行第二步中的命令后生成的package.json的文件的内容是: { "name&q ...

  6. Boke光纤交换机的snmp配置

    今天我配置了一下Boke光纤交换机e-300的snmp trap的配置 敲击help命令你会发现配置snmp的命令共有四个 snmpconfig                      Config ...

  7. 移动端list布局,左边固定,右边自适应

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. 纯手工 CheckboxTree 实现

    数据结构及页面显示格式: INSERT INTO AS_CombRules VALUES('', '', '', '', '', '', '') 实现 CheckboxTree 功能: html代码: ...

  9. loj6030 「雅礼集训 2017 Day1」矩阵

    传送门:https://loj.ac/problem/6030 [题解] 以下把白称为0,黑称为1. 发现只有全空才是无解,否则考虑构造. 每一列,只要有0的格子都需要被赋值1次,所以设有x列有含有0 ...

  10. [Leetcode Week3]Course Schedule

    Course Schedule题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule/description/ Descript ...