Wireless Network

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 43199   Accepted: 17800

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

 
本题思路:存储每个被维修的电脑,对于每台被维修的电脑,我们只需要判断他和已经修复的电脑之间可不可以通信,如果可以则将它们加入一个集合。判断两台电脑是否可以通信只需要判断他们是否在一个集合中,很简单的并查集基本操作。
 
参考代码:
 #include <cstdio>
#include <cmath>
using namespace std; const int maxn = + ;
struct coordinate {
int x, y;
} G[maxn];
int n, d, p, q, cnt, head[maxn], Rank[maxn];
int isrepair[maxn];
char operation; double dis(int u, int v) {
return sqrt((double)(G[u].x - G[v].x) * (G[u].x - G[v].x) + (G[u].y - G[v].y) * (G[u].y - G[v].y));
} int find(int x) {
if(head[x] == x) return x;
return head[x] = find(head[x]);
} void Union_set(int x, int y) {
int dx = find(x), dy = find(y);
if(dx == dy) return;
if(Rank[dx] > Rank[dy]) head[dx] = dy;
else {
head[dy] = dx;
if(Rank[dx] == Rank[dy])
Rank[dx] ++;
}
} bool Is_same(int u, int v) {
return find(u) == find(v);
} int main () {
cnt = ;
scanf("%d %d", &n, &d);
for(int i = ; i <= n; i ++)
scanf("%d %d", &G[i].x, &G[i].y);
for(int i = ; i <= n; i ++) {
head[i] = i;
Rank[i] = ;
}
while(~scanf("%c", &operation)) {
if(operation == 'O') {
scanf("%d", &p);
for(int i = ; i <= cnt; i ++) {
if(dis(p, isrepair[i]) <= d) {
Union_set(isrepair[i], p);
}
}
isrepair[cnt ++] = p;
}
else if(operation == 'S') {
scanf("%d %d", &p, &q);
if(Is_same(p, q))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return ;
}

POJ-2236.WireleseNetwork.(并查集)的更多相关文章

  1. poj 2236【并查集】

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

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

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

  3. POJ 2236 (简单并查集) Wireless Network

    题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第 ...

  4. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  5. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  6. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  7. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  8. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  9. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

随机推荐

  1. oracle数据库命令行查看存储过程

    之前有用过这种写法,转换大小写在赋给字段,但是没成功,偶然间发现别人有这么写,今天试了下确实可以

  2. 一个故事带你理解if __name__ == '__main__'

    如果你刚刚接触python,相信会在看别人的程序的时候会遇到if __name__ == '__main__'酱紫的语法,如果当时没看懂现在也一知半解的话,看下去,本文可以帮你解决这个问题. 大家都知 ...

  3. 通过nginx日志,统计最近两天的交易笔数

    #!/bin/bash yesterday=`date -d last-day +%Y-%m-%d` dayago=`date -d '2 days ago' +%Y-%m-%d` #echo $ye ...

  4. springcloud相关资料收集

    http://springboot.fun/  Spring  Boot 中文索引 http://springcloud.fun/   Spring Cloud 中文索引 https://spring ...

  5. JavaScript装饰者模式

    这里我们通过需求逐渐引出装饰者模式. 下面是一个关于几代汽车的不同逐渐体现装饰者模式的. 首先,我们先引入一个接口文件----目的为检验实现类是否完全实现接口中的方法,代码如下, //定义一个静态方法 ...

  6. java实现HTTP Basic认证

    这两天一直在调试EMQ的API,通过HTTP的GET请求,可以查询到订阅列表信息,在浏览器中测试时,需要输入用户名和密码,然后才能显示出结果,输错或者不输入会返回401错误. 通过浏览器输入用户名和密 ...

  7. mybatis学习 -每天一记 通用mapper 关于UUID回显的配置

    在使用通用mapper插入数据UUID回显 在使用通用mapper插入数据时,发现主键没有回显,我这里的主键是UUID的,解决方案是:配置一个MapperScannerConfigurer. @Bea ...

  8. c#中委托和事件(转)

    C# 中的委托和事件 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真 ...

  9. drf框架之分页器的用法

    1. 分页器分为:简单分页器与偏移分页器和加密分页器 2.实现一个简单的分页器的用法: # 简单分页 # 第一步,导入分页类 # from rest_framework.pagination impo ...

  10. Linux su命令

    本人以前一直习惯直接使用root,很少使用su,前几天才发现su与su -命令是有着本质区别的! 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行, ...