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

Sample Input

O
O
O
S
O
S
Sample Output FAIL
SUCCESS

题意:

  一张图上分布着n台坏了的电脑,并知道它们的坐标。两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连。给出操作“O x”表示修好x,给出操作“S x y”,请你判断x和y在此时有没有连接上。

思路:

  并查集的简单应用,将修好的做一下标记,修好一台,与每一台做了标记的遍历检查,遍历时要加上距离这个判断条件。

代码:

#include<iostream>
#include<stack>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int pre[];
int logo[];
struct node
{
int x;
int y;
} edge[];
int find(int x)
{
if(x!=pre[x])
pre[x]=find(pre[x]);
return pre[x];
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fy]=fx;
}
}
double dis(struct node a,struct node b)//求两点之间的距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int n;
double maxdis;
cin>>n>>maxdis;
for(int i=; i<=n; i++)
{
cin>>edge[i].x>>edge[i].y;
}
for(int i=; i<=n; i++)
{
pre[i]=i;
logo[i]=;
}
char ch;
while(~scanf("%c",&ch))
{
int x,y;
if(ch=='O')
{
cin>>x;
logo[x]=;//修好的标记为1
for(int i=; i<=n; i++)
{
if((dis(edge[i],edge[x])<=maxdis)&&logo[i]==)//如果修好了,并且距离不大于规定距离,则可以并
merge(i,x);
}
}
if(ch=='S')
{
cin>>x>>y;
if(find(x)==find(y))
cout<<"SUCCESS"<<endl;
else
cout<<"FAIL"<<endl;
}
}
}

POJ2236 Wireless Network 并查集简单应用的更多相关文章

  1. POJ2236 Wireless Network 并查集

    水题 #include<cstdio> #include<cstring> #include<queue> #include<set> #include ...

  2. POJ 2236 Wireless Network (并查集)

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

  3. Wireless Network 并查集

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  4. poj 2236 Wireless Network (并查集)

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

  5. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  6. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  7. poj2236 Wireless Network(并查集直接套模板

    题目地址:http://poj.org/problem?id=2236 题目大意:n台电脑都坏了,只有距离小于d且被修好的电脑才可以互相联系,联系可传递.输入n和d,n个点的坐标x y.两个操作:O ...

  8. POJ-2236 Wireless Network 顺便讨论时间超限问题

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 26131   Accepted: 108 ...

  9. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

随机推荐

  1. jsp传到java的control层的方法

    jsp传到java的control层的方法1.form表单 用<input type="submit">提交,提交到后台的参数在form表单内<form meth ...

  2. postgis 中的距离计算

    最近在做一个项目,有一个功能想要实现类似于查询附近的人的功能.由于项目的原因数据库只能使用 postgresql,空间查询就使用了 postgis 来实现. 具体业务像这样:业务需要返回附近距自己 1 ...

  3. 源码安装zabbix_server服务端

    按照上一篇安装lnmp环境:http://www.cnblogs.com/armo/p/6067716.html 保证lnmp正常运行,然后安装zabbix_server 安装依赖 yum -y in ...

  4. sql求和isnull注意事项

    如果不用isnull函数判断则计算出来如果有一列是null 则相加就是null,如 两列:1 null 1+null = nullselect sum(ISNULL(jinE,0)+ISNULL(qi ...

  5. 《JavaScript高级程序设计》笔记二

    第二章 在HTML中使用JavaScript 要想把JavaScript放到网页中,就必须涉及到Web的核心语言HTML.向HTML页面中插入JavaScript的主要方法,就是使用<scrip ...

  6. [编织消息框架][netty源码分析]10 ByteBuf 与 ByteBuffer

    因为jdk ByteBuffer使用起来很麻烦,所以netty研发出ByteBuf对象维护管理内存使用ByteBuf有几个概念需要知道1.向ByteBuf提取数据时readerIndex记录最后读取坐 ...

  7. Swift自增和自增运算

    自增和自增运算 和 C 语言一样,Swift 也提供了方便对变量本身加1或减1的自增(++)和自减(--)的运算符.其操作对象可以是整形和浮点型. ‌ var i = ++i // 现在 i = 1 ...

  8. php后台数组foreach嵌套循环

    <?php foreach($list as $key=>$val){ ?> <tr class="over_odd"> <td align=& ...

  9. Python基础入门教程,Python学习路线图

    给大家整理的这套python学习路线图,按照此教程一步步的学习来,肯定会对python有更深刻的认识.或许可以喜欢上python这个易学,精简,开源的语言.此套教程,不但有视频教程,还有源码分享,让大 ...

  10. jsp注册页面验证,easyui的jsp+js表单验证

    1.1下面的代码是写在Js里面的,就直接写进去不用什么其他东西,这样一个表单验证就好了(1.2图) $.extend($.fn.validatebox.defaults.rules, { phone: ...