Wireless Network

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 Code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define pi acos(-1.0)
#define mj
#define inf 0x3f3f3f
typedef double db ;
typedef long long ll;
using namespace std;
const int N=1e3+;
const int mod=1e9+;
int f[N],dis[N],d;
bool has[N];
pair<int ,int > po[N];
vector<int> e[N];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void unit(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
f[fx]=fy;
}
int main()
{
int n,d;
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++){
f[i]=i;
int x,y;
scanf("%d%d",&x,&y);
po[i].first=x;
po[i].second=y;
}
for(int i=;i<n;i++){
for(int j=i+;j<=n;j++){
int x1=po[i].first,y1=po[i].second,x2=po[j].first,y2=po[j].second;
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=d*d) {e[i].push_back(j);e[j].push_back(i);}
}
}
char s[];
while(scanf("%s",s)==){
if(s[]=='O'){
int x;
scanf("%d",&x);
has[x]=;
for(int i=;i<e[x].size();i++){
int v=e[x][i];
if(has[v]){
unit(v,x);
}
}
}
else
{
int x,y;
scanf("%d%d",&x,&y);
// printf("%d find:%d %d find:%d\n",x,find(x),y,find(y));
if(find(x)==find(y)) puts("SUCCESS");
else puts("FAIL");
}
}
return ;
}

poj 2236的更多相关文章

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

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

  2. 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解

    不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...

  3. poj 2236【并查集】

    poj 2236 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical t ...

  4. poj 2236 Wireless Network (并查集)

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

  5. (并查集) Wireless Network --POJ --2236

    链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  6. Poj(2236),简单并查集

    题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...

  7. [并查集] POJ 2236 Wireless Network

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

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

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

  9. POJ 2236:Wireless Network

    描述 n台电脑,如果两台电脑间的距离的d范围内,则两台电脑能够连通. 如果AB连通,BC连通,则认为AC连通. 已知电脑台数N,最大距离d,以及每个电脑的坐标.有如下两种操作: O i 表示修复编号为 ...

  10. POJ 2236 Wireless Network(并查集)

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

随机推荐

  1. 判断移动端设备: navigator.userAgent.toLowerCase()

    判断你的浏览设备: navigator.userAgent.toLowerCase(); (返回当前用户所使用的是什么浏览器,将获得的信息变成小写) function browserRedirect( ...

  2. 贴一份用delphi修改注册表改网卡MAC地址的代码

    //提示:此代码需要use Registry, Common; function WriteMAC(model:integer):integer; var reg:TRegistry; begin r ...

  3. LIS算法

    LIS(Longest Increasing Subsequence)最长上升(不下降)子序列. 1. O(n^2) #include<cstdio> #include<algori ...

  4. 基于basys2驱动LCDQC12864B的verilog设计图片显示

    话不多说先上图 前言 在做这个实验的时候在网上找了许多资料,都是关于使用单片机驱动LCD显示,确实用单片机驱动是要简单不少,记得在FPGA学习交流群里问问题的时候,被前辈指教,说给我最好的指教便是别在 ...

  5. 正则匹配所有的a标签

    <a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)</a>分组1和分组2即为href和value解释: ...

  6. EF6多线程与分库架构设计之Repository

    1.项目背景 这里简单介绍一下项目需求背景,之前公司的项目基于EF++Repository+UnitOfWork的框架设计的,其中涉及到的技术有RabbitMq消息队列,Autofac依赖注入等常用的 ...

  7. TV端:通过遥控器的点击实现图片的上下左右抖动的效果

    做TV端有一段时间了,我看到别的TV上有一个通过遥控器的触摸板来控制一张图片的相应方向的抖动,感觉听新奇的,就试着做了一个分享一下: 转载注明出处:http://www.cnblogs.com/hyy ...

  8. IP分类以及特殊IP

     一.IP分类 点分十进制数表示的IPv4 地址分成几类,以适应大型.中型.小型的网络.这些类的不同之处在于用于表示网络的位数与用于表示主机的位数之间的差别.IP地址分成五类,用字母表示:       ...

  9. 【微信开发】玩转PHP 数组用法!

    数组的起始下标可以不从0开始,例子为从2开始. $data = array(2=>'A','B','C');     运行结果:$data = array(2=>'A',3=>'B' ...

  10. react native 添加第三方插件react-native-orientation(横竖屏设置功能 android)

    Installation 1.install  rnpm Run  npm install -g rnpm 2.via rnpm Run rnpm install react-native-orien ...