无线电网络

  题目大意:就是地震后,所有的电脑都坏了,现在可以修复,而且要重新连成一个网络,两台电脑之间最大连接距离为D,两台电脑可以有中继电脑,按O修复电脑,按S测试两台电脑是否有链接,如果有就输出SUCCESS,不行就FAIL

  一看到电脑,我就联想到了查并集,这一题也不例外,我们维护在距离内的查并集就可以了,具体思路不难,每一次修复都扫描所有的节点看能否连接即可

  

 #include <iostream>
#include <functional>
#include <algorithm>
#include <math.h>
#define MAX 1004 using namespace std;
typedef int Position;
typedef struct _set
{
int x;
int y;
}Point; static Point Comp[MAX];
static Position Set[MAX];
static bool used[MAX];
static int max_dist; Position Find(Position);
void Unite_Set(Position, Position);
void Repaired(const int, Position);
void Test(Position, Position);
double Dist_Cal(Position, Position); int main(void)
{
int n, tmp_p, tmp_x, tmp_y;
char choice;
while (~scanf("%d%d", &n, &max_dist))
{
memset(Set, -, sizeof(Set));
memset(used, , sizeof(used));
for (int i = ; i <= n; i++)
scanf("%d%d", &Comp[i].x, &Comp[i].y);
getchar();
while (~scanf("%c", &choice))
{
if (choice == 'O')
{
scanf("%d", &tmp_p);
Repaired(n, tmp_p);
}
else
{
scanf("%d%d", &tmp_x, &tmp_y);
Test(tmp_x, tmp_y);
}
getchar();
}
}
return ;
} double Dist_Cal(Position i, Position j)
{
return sqrt((double)((Comp[i].x - Comp[j].x)*(Comp[i].x - Comp[j].x)) +
(double)((Comp[i].y - Comp[j].y)*(Comp[i].y - Comp[j].y)));
} void Unite_Set(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y);
if (px != py)
{
if (Set[px] < Set[py])//按大小求并
{
Set[px] += Set[py];
Set[py] = px;
}
else
{
Set[py] += Set[px];
Set[px] = py;
}
}
} Position Find(Position x)
{
if (Set[x] < )
return x;
else return Set[x] = Find(Set[x]);
} void Repaired(const int n, Position x)
{
used[x] = ;
for (int i = ; i <= n; i++)
{
if (i == x || !used[i]) continue;
if (Dist_Cal(x, i) <= (double)max_dist)
Unite_Set(x, i);
}
} void Test(Position x, Position y)
{
Position px, py;
px = Find(x);
py = Find(y); if (px == py) puts("SUCCESS");
else puts("FAIL");
}

                    

DisJSet:Wireless Network(POJ 2236)的更多相关文章

  1. Wireless Network(POJ 2236)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20724   Accepted: 871 ...

  2. Day5 - B - Wireless Network POJ - 2236

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

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

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

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

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  5. A - Wireless Network POJ - 2236

    题目大意:有n台坏掉的电脑,给出每台电脑的坐标,然后每次询问输入0(字符) x,表示电脑x恢复正常,输入S x y 询问x和y是否可以联网.只要是x和y的距离小于距离d,那么就可以联网,如果有个中介c ...

  6. A - Wireless Network POJ - 2236-kuangbin带你飞

    A - Wireless Network POJ - 2236 Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 50348 ...

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

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

  8. [并查集] POJ 2236 Wireless Network

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

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

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

随机推荐

  1. PR 不能手动修改素材尺寸的解决方法

    选中素材,然后再特效控制台那边点击一下运动就可以在预览窗口直接用鼠标调整画面大小和位移了.

  2. 获取手机的gps定位

    只要手机有GPS模块,可以用HTML5的Geolocation接口获取 在HTML5中,geolocation作为navigator的一个属性出现,它本身是一个对象,拥有三个方法: - getCurr ...

  3. 点击Cell中的按钮时,如何取所在的Cell

    4.点击Cell中的按钮时,如何取所在的Cell:-(void)OnTouchBtnInCell:(UIButton *)Btu{CGPoint point = btn.center;point = ...

  4. Bsoj 1322 第K小数

    第K小数 Description 现在已有N个整数,你有以下三种操作: 1 A:表示加入一个值为A的整数: 2 B:表示删除其中值为B的整数: 3 K:表示输出这些整数中第K小的数: Input 第一 ...

  5. Android学习笔记02-Mac下编译java代码

    在Mac OS上配置JDK 1.7. 一 下载 Mac版本的JDK1.7 从以下下载地址,下载Mac版本的JDk1.7 安装文件 jdk-7u79-macosx-x64.dmg. http://www ...

  6. Android:Touch和Click的区别

    http://blog.csdn.net/hufeng882412/article/details/7310142 针对屏幕上的一个View控件,Android如何区分应当触发onTouchEvent ...

  7. Jenkins 搭建U3D自动发布 Android

    工具 [u3d相关的PostProcessBuildPlayer,PerformBuild.cs] 1.Jenkins 开源包  Java -jar jenkins.war,参考链接 http://w ...

  8. (翻译)初学者的object-C指南

    初学者的object-C指南 英文原文:http://blog.teamtreehouse.com/the-beginners-guide-to-objective-c-language-and-va ...

  9. Yii2 如何使用事件

    原文地址:http://www.fancyecommerce.com/2016/04/29/yii2-%E4%BD%BF%E7%94%A8event-1-%EF%BC%8C%E5%A6%82%E4%B ...

  10. Percona XtraDB Cluster(转)

    Percona XtraDB Cluster是针对MySQL用户的高可用性和扩展性解决方案,基于Percona Server .其包括了Write Set REPlication补丁,使用Galera ...