POJ 2236 Wireless Network【并查集】
发布日期:2021-07-01 02:50:38
浏览次数:103
分类:技术文章
本文共 3429 字,大约阅读时间需要 11 分钟。
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 computerA
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 integersN
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: "O p" (1 <= p <= N)
, which means repairing computer p."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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
FAILSUCCESS
题意:有 N
台电脑坏了,两台电脑之间超过距离 d
就无法直接通信,但是可以借助其他电脑间接通信。电脑编号从 1~N
。然后进行多次操作:维护或者测试。
思路:时间限制是10000MS,于是很简单:每维护一台电脑,就标记为维护过,然后加入已维护的电脑集合,让它和所有维护过的电脑进行比较,如果距离足够短,就用并查集合并。每一次的测试操作,如果两台电脑在同一个集合中,就说明可以进行通信。
代码如下:
#include#include #include using namespace std;const int maxn = 1010;int father[maxn], repaired[maxn]; //并查集; 维护过的电脑 int len = 0, d; //距离d struct computer { double x, y; bool isRepaired;} c[maxn];inline void init(int n) { for (int i = 1; i <= n; ++i) { father[i] = -1; c[i].isRepaired = false; }}inline int find(int x) { return father[x] < 0 ? x : father[x] = find(father[x]);}void merge(int a, int b) { int x = find(a), y = find(b); if (x == y) return; double dist = sqrt((c[a].x - c[b].x) * (c[a].x - c[b].x) + (c[a].y - c[b].y) * (c[a].y - c[b].y)); if (dist <= d) { if (father[x] < father[y]) { father[x] += father[y]; father[y] = x; } else { father[y] += father[x]; father[x] = y; } }}inline bool query(int a, int b) { return find(a) == find(b); }int main() { char ch[2]; int n, p, q; scanf("%d%d", &n, &d); init(n); for (int i = 1; i <= n; ++i) scanf("%lf%lf", &c[i].x, &c[i].y); while (~scanf("%s%d", ch, &p)) { if (ch[0] == 'S') { scanf("%d", &q); if (!c[p].isRepaired || !c[q].isRepaired || !query(p, q)) printf("FAIL\n"); else printf("SUCCESS\n"); } else if (ch[0] == 'O') { c[p].isRepaired = true; repaired[len++] = p; //记录维护过的电脑的编号 for (int i = 0; i < len - 1; ++i) //和其他已经维护过的电脑比较 merge(repaired[i], p); } } return 0;}
用G++提交:
改成C++:转载地址:https://memcpy0.blog.csdn.net/article/details/108298380 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月28日 12时47分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
第09章 异常处理
2019-05-02
第10章 面向对象编程
2019-05-02
第11章 案例研究: 文本统计
2019-05-02
qml drag listview
2019-05-02
QT中文乱码的解
2019-05-02
netsh用法
2019-05-02
网上Qt多线程同步的一种普遍误识
2019-05-02
libcurl smtp发送邮件附件大小限制问题
2019-05-02
Qt中用QuaZip来压缩和解压缩文件
2019-05-02
第13章 Windows内存体系结构
2019-05-02
windows 和 linux 下c/c++内存分布(整理)
2019-05-02
Qt解析XML文件(QDomDocument)
2019-05-02
Qt图形视图框架
2019-05-02
Qt5中表格处理大数据量
2019-05-02
LeakCanary源码分析
2019-05-02
[转自]同步/异步与阻塞/非阻塞的区别
2019-05-02
单例模式(Singleton)
2019-05-02
ucOS 时钟中断(ISR)
2019-05-02
android Activity之间跳转。
2019-05-02