https://vjudge.net/problem/POJ-2236

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

版本一 :

使用数组

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define maxn 50010
using namespace std;
int n;
double d;
double dx[],dy[];
int pre[],repair[];
double dis(int a,int b)
{
return sqrt((dx[a]-dx[b])*(dx[a]-dx[b])+(dy[a]-dy[b])*(dy[a]-dy[b]));
}
int Find(int x)
{
if(x!=pre[x])
return pre[x]=Find(pre[x]);
}
int Union(int x,int y)
{
pre[Find(x)]=Find(y);
}
int main()
{
cin>>n>>d;
for(int i=;i<=n;i++)
{
cin>>dx[i]>>dy[i];
pre[i]=i;
}
char k;
int p,q,len=;
getchar();
while(~scanf("%c",&k))
{
if(k=='O')
{
cin>>p;
repair[len++]=p;
for(int i=;i<len-;i++)
if(dis(p,repair[i])<=d)
Union(repair[i],p);
}
else
{
cin>>p>>q;
if(Find(p)==Find(q))
cout<<"SUCCESS"<<endl;
else
cout<<"FAIL"<<endl;
}
getchar();
}
return ;
}

版本二:

使用结构体

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define maxn 1005
using namespace std;
int n;
double d;
struct node
{
double x,y;
int pre;//根节点
int ranks;//树的高度
bool repair;//是否修复
}link[maxn];
int Find(int x)
{
if(x!=link[x].pre)
return link[x].pre=Find(link[x].pre);
}
double dis(int i,int j)
{
return sqrt((link[i].x-link[j].x)*(link[i].x-link[j].x)+(link[i].y-link[j].y)*(link[i].y-link[j].y));
}
void Union(int i,int j)
{
//if(link[i].repair&&link[j].repair)
// return 0;
int root1=Find(i),root2=Find(j);
//if(root1==root2)return 0;
if(link[root1].ranks>link[root2].ranks)
{
link[root2].pre=root1;
link[root1].ranks+=link[root2].ranks;
}
else
{
link[root1].pre=root2;
link[root2].ranks+=link[root1].ranks;
}
//return 1;
} int main()
{
cin>>n>>d;
for(int i=;i<=n;i++)
{
cin>>link[i].x>>link[i].y;
link[i].ranks=;
link[i].pre=i;
link[i].repair=;
}
char k;
int p,q;
getchar();
while(~scanf("%c",&k))
{
if(k=='O')
{
cin>>p;
link[p].repair=;
for(int i=;i<=n;i++)
{
if(Find(i)!=Find(p)&&link[i].repair&&dis(i,p)<=d)
Union(p,i);
}
}
else
{
cin>>p>>q;
if(Find(p)==Find(q))
cout<<"SUCCESS"<<endl;
else
cout<<"FAIL"<<endl;
}
getchar();
}
return ;
}

题目大意:

n个站点,已知站点的坐标。

‘O'操作 每次修通一个站点,该站点与距离该站点小于d且也为修通的站点联通;

’S'操作 查询两个站点是否联通。

思路:

并查集的题。设置距离函数,如果满足距离条件则将两个点合并。如果查询到的两点根节点相同则能联通。

POJ2236的更多相关文章

  1. POJ-2236(并查集)

    Wireless NetWork POJ-2236 需要注意这里的树的深度需要初始化为0. 而且,find函数需要使用路径压缩,这里的unint合并函数也使用了优化(用一开始简单的合并过不了). #i ...

  2. 并查集 poj2236

    网址:http://poj.org/problem?id=2236 题意:有n台坏的电脑,如果每两台电脑的距离不能超过d,那么这两台电脑有联系,用字符串O 表示标记第x台电脑维修了,用S判断从X到y是 ...

  3. poj2236(并查集)

    题目链接: http://poj.org/problem?id=2236 题意: 有n台计算机, 已知每台计算机的坐标, 初始时所有计算机都是坏的, 然后修复其中一些计算机, 已修复的计算机距离不超过 ...

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

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

  5. POJ2236 Wireless Network

    解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后,   进行一次for循环,进行相关调整.同时注意输入输出格式,见代码: #include<cstdio> #incl ...

  6. POJ2236 Wireless Network 并查集

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

  7. poj2236 基础并查集

    题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...

  8. poj2236(并查集)

    题目连接 题意:一张图上分布着n台坏了的电脑,并知道它们的坐标.两台修好的电脑如果距离<=d就可以联网,也可以通过其他修好的电脑间接相连.给出操作“O x”表示修好x,给出操作“S x y”,请 ...

  9. poj2236无线网络

    这一题的大意:在救灾当中需要用网络,这堆人就用笔记本建了一个无线网,但是来,互相通信都是有距离限制的,一台电脑只能和距离他为d的电脑通信,然后一台电脑也可以通过几台电脑搭成线这样通信.然后就是输入每台 ...

随机推荐

  1. AOP,过滤器,监听器,拦截器【转载】

    面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ,我们知道,面向对象的特点是继承.多态和封装.而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配 ...

  2. CSS之HTML meta viewport属性详解

    什么是Viewport 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(viewport)比屏幕宽,这样就不用把每个网页挤到很小的窗口中(这样会破坏没有针对手机 ...

  3. Pycharm2017常用快捷键

    Pycharm 的快捷键可以在[文件]-[设置]中自定义(见上图). 下方是根据网上资料整理的官方默认快捷键设置. 常用快捷键 Ctrl + / 行注释/取消行注释 Tab / Shift + Tab ...

  4. 洗礼灵魂,修炼python(25)--自定义函数(6)—从匿名函数进阶话题讲解中解析“函数式编程”

    匿名函数进阶 前一章已经说了匿名函数,匿名函数还可以和其他内置函数结合使用 1.map map():映射器,映射 list(map(lambda x:x*2,range(10))) #把range产生 ...

  5. CSS| 框模型-輪廓

    轮廓(outline)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用.CSS outline 属性规定元素轮廓的样式.颜色和宽度. 相關屬性 outline-color 属性 ou ...

  6. Scrapy爬取遇到的一点点问题

    学了大概一个月Scrapy,自己写了些东东,遇到很多问题,这几天心情也不大好,小媳妇人也不舒服,休假了,自己研究了很久,有些眉目了 利用scrapy 框架爬取慕课网的一些信息 步骤一:新建项目 scr ...

  7. .net core 导出Excel(epplus 创建excel )

    [Route("getopenfrequencyexcel")] [HttpGet] public IActionResult GetOpenFrequencyExcel(int ...

  8. 【PAT】1083 是否存在相等的差(20 分)

    //这题不是我耍流氓,实在太简单,只能直接贴代码了,凑个数 #include<stdio.h> int aaa[10005]={0}; int main(){ int N;scanf(&q ...

  9. nginx服务器开启缓存、反向代理

    一.反向代理配置 1.反向代理服务器配置如下 反向代理就是需要这一行proxy_pass来完成.当我们要访问后端web服务器的时候,我们只需要访问代理服务器就可以了,此时代理服务器就充当后端web服务 ...

  10. MATLAB用二分法、不动点迭代法及Newton迭代(切线)法求非线性方程的根

    MATLAB用二分法.不动点迭代法及Newton迭代(切线)法求非线性方程的根 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 一.实验原理 二.实验步骤 ...