poj 2236 Wireless Network
来源:互联网 发布:淘宝店铺红包幸运抽奖 编辑:程序博客网 时间:2024/06/05 19:33
提题意是n台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能。若两台电脑能相互通信,
则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑。
给出所有电脑的坐标位置,对其进行两种可能的操作,O x表示修复第x台,S x y表示判断x y之间能否通信,
若能输出SUCCESS,否则输出FALL。
思路:判断图的连通性问题,可以借助并查集。首先,电脑之间若能通信,则大前提就是两台电脑都是维修过的,
因此处理联通问题时,必须在已修好的电脑中进行。由于通信可以传递,因此只要满足能够通信的条件,则新加入
的电脑就可以借助这个小网络与其中任意一台通信,这就是使用并查集的关键条件,因此,将满足距离关系的电脑
并入一个集合中,此后每加入一台电脑都与前面加入的所有电脑做一次联通性的判断,即只要距离小于d就并入集合即可。
#include <stdio.h>#include <math.h>int n, rank[1005], arr[1005], repaired[1005];struct Pos { int x, y;} pos[1005];void make(){ for (int i = 1; i <= n; ++i) rank[i] = 0, arr[i] = i;}int find(int x){ if (x != arr[x]) arr[x] = find(arr[x]); return arr[x];}void merge(int x, int y){ x = find(arr[x]); y = find(arr[y]); // 多向上找一层 if (x != y) { if (rank[x] < rank[y]) arr[x] = y; else { arr[y] = x; if (rank[x] == rank[y]) ++rank[x]; } }}double distance(int a, int b) // 注意标号转换一下{ Pos x, y; x = pos[a]; y = pos[b]; return sqrt(1.0*(x.x-y.x)*(x.x-y.x) + 1.0*(x.y-y.y)*(x.y-y.y));}int main(){ char ch; int i, x, y, cnt; double d; scanf("%d%lf", &n, &d); for (i = 1; i <= n; ++i) // 编号1到n.. scanf("%d%d", &pos[i].x, &pos[i].y); cnt = 0; make(); while (scanf(" %c%d", &ch, &x) != EOF) if (ch == 'O') { for (i = 0; i < cnt; ++i) // 与已修好的电脑合并 if (distance(repaired[i], x) <= d) merge(repaired[i], x); repaired[cnt++] = x; // 加入修好的集合中,逻辑其实恰好相反,是先修再合并 } else { scanf("%d", &y); if (find(x) == find(y)) // 判断连通性 printf("SUCCESS\n"); else printf("FAIL\n"); } return 0;}
0 0
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236 Wireless Network
- POJ-2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236 - Wireless Network
- poj 2236 Wireless Network
- POJ 2236 Wireless Network
- poj 2236 wireless network
- POJ--2236--Wireless Network
- POJ-2236-Wireless Network
- poj 2236 Wireless Network
- poj 2236 Wireless Network
- POJ 2236Wireless Network
- POJ 2236 Wireless Network
- POJ 2236 Wireless Network
- POJ 2236 Wireless Network
- iOS私有API之wifi扫描和wifi连接
- Multi-Instance Learning 和 Latent SVM
- linux 的 cat more less 命令有什么区别
- C#使用GDAL读取DEM数据
- iOS图片拉伸技巧
- poj 2236 Wireless Network
- listview小知识整理
- http服务器交互get,put,post,delete等说明
- Linux系统编程_4_标准I/O(附:清空缓冲区方法)
- iOS 后台运行实现
- 解决SmartUpload的中文乱码问题——超简单!
- 草稿
- android控制wifi状态
- listview android:cacheColorHint,android:listSelector属性作用