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. sql 先查出已知的数据或者需要的数据再筛选

    sql 先查出已知的数据或者需要的数据再筛选

  2. jquery/js iframe 元素操作

    1.判断id/ class 是否存在? <script> $(function(){ if(("#id_name").length()>0){ //如果id 存在 ...

  3. Div+Css中transparent制作奥运五环

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

  4. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  5. vim配置入门,到豪华版vim配置

    这几天一直研究vim的配置,许多版本总是不尽如人意,网上确实有许多优秀的文章值得参考,我的博客后面会贴上具有参考价值的博客链接,本文的将手把手教你配置一个功能详尽的vim. 首先你要明白的是linux ...

  6. 用PHP迭代器来实现一个斐波纳契数列

    斐波纳契数列通常做法是用递归实现,当然还有其它的方法.这里现学现卖,用PHP的迭代器来实现一个斐波纳契数列,几乎没有什么难度,只是把类里的next()方法重写了一次.注释已经写到代码中,也是相当好理解 ...

  7. pmap用法小计

    By francis_hao    Aug 4,2017   pmap-报告进程的内存映射.   概要 pmap [options] pid [...]   描述 pmap命令用来报告一个或多个进程的 ...

  8. [ZJOI2007]棋盘制作 (单调栈)

    [ZJOI2007]棋盘制作 题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8 \times 88×8大小的黑白相间 ...

  9. SCOI2008奖励关 [状压dp]

    题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...

  10. js获取当前url地址参数中文乱码问题

    网上看了一些关于此问题的文章,都说的不清不楚,有些更是乱七八糟,完全没法看,故此找了一篇能用的,借鉴作为笔记. //首先获取到当前页面的地址栏信息 var url = window.location. ...