POJ-2236-Wireless Network [并查集]
来源:互联网 发布:国产电视推荐 知乎 编辑:程序博客网 时间:2024/06/15 03:32
题目传送门
题意:
一个地图上分布着N台电脑,并给出N台电脑的坐标,如果两者之间距离小于等于d就可以通信,可以间接通信。
给出操作”O x”为修好x, 操作”S x y”为询问x y是否可以通信。输出”FAIL”或”SUCCESS”
思路:
如果输入”O x”就枚举所有已经出现过的点,判断两点之间距离是否小于等于d,如果小于等于d,就将这两个点合并。
如果输入”S x y”就求x y的父节点,相同即成功,不同即失败。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;struct node{ int x,y;}Point[1200];int vis[1200];int Father[1200];int n,d;void init(){ for (int i = 0; i < 1200; i++) { Father[i] = i; } memset(vis,0,sizeof(vis));}int Find(int x){ while (Father[x]!=x) x = Father[x]; return x;}bool Dis(int a, int b){ int p = Point[a].x - Point[b].x; int q = Point[a].y - Point[b].y; if (p*p+q*q<=d*d) return true; return false;}int main(void){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d %d", &n, &d); init(); for (int i = 1; i <= n; i++) scanf("%d %d", &Point[i].x, &Point[i].y); char ch; while (cin>>ch) { if (ch=='O') { int r; scanf("%d", &r); vis[r] = 1; for (int i = 1; i <= n; i++) { if (i!=r && vis[i] && Dis(i,r)) { int p = Find(i); int q = Find(r); Father[p] = q; } } } else { int r1,r2; scanf("%d %d", &r1, &r2); if (Find(r1)==Find(r2)) 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 2236 Wireless Network(并查集)
- poj 2236 Wireless Network 并查集
- POJ 2236--Wireless Network【并查集】
- poj 2236 Wireless Network 并查集
- what引导的宾语从句
- 机器学习笔记——正则化线性回归
- 进程池和线程池
- Spark 之 RDD
- Weird Rounding
- POJ-2236-Wireless Network [并查集]
- [LeetCode]437. Path Sum III(求二叉树中路径和等于sum的数量)
- lightoj1138Trailing Zeroes (III) 二分
- 九个步骤让你成为PHP专家
- 今年暑假不AC
- 剑指offer-扑克牌顺子
- CSS文本与字体
- HDU 1599 find the mincost route (floyd)
- Fragment懒加载