POJ 2236 Wireless Network【并查集】
发布日期:2021-07-01 02:50:38
浏览次数:104
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月23日 18时26分54秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2021-05-18
2019-04-30
基础架构系列篇-NGINX部署VUE
2019-04-30
基础架构系列篇-系统centos7安装kafka
2019-04-30
2021年不可错过的17种JS优化技巧(一)
2019-04-30
在 Vue 中用 Axios 异步请求API
2019-04-30
MySQL进阶查询(SELECT 语句高级用法)
2019-04-30
Mysql 之主从复制
2019-04-30
【NLP学习笔记】中文分词(Word Segmentation,WS)
2019-04-30
对于时间复杂度的通俗理解
2019-04-30
如何输入多组数据并输出每组数据的和?
2019-04-30
行阶梯型矩阵
2019-04-30
JAVA学习笔记6 - 数组
2019-04-30
【学习笔记】Android Activity
2019-04-30
location区段
2019-04-30
linux内存的寻址方式
2019-04-30
how2heap-double free
2019-04-30
how2heap-fastbin_dup_consolidate
2019-04-30
tf keras SimpleRNN源码解析
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30
xss-labs详解(上)1-10
2019-04-30