POJ 2236 Wireless Network 【并查集的简单应用 判断是否在同一连通分量】
2013-04-13 13:07
525 查看
Wireless Network
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
Sample Output
Source
POJ Monthly,HQM
题意:给你 N 台坏了的电脑的坐标 ,和一个距离范围 d .
(在距离范围内的电脑可以相互通信,每台电脑也可以连接两台不同的电脑,使他们之间能够通信)
输入任意次数操作:
O x 表示修理好编号为 x 台的电脑
S x y 判断电脑 x 和 y 是否能够通信 。能则 SUCCESS ,不能则 FAIL
算法:并查集简单应用,判断两点是否在同一连通分量
思路:并查集部分很好处理。
关键是判断两台电脑是否在给定范围内,这里要用一个map[][]数组标记连通性,用一个used[]数组标记是否修理过。
每修一台电脑,则连接【合并】这台电脑和已经距离这台电脑 d 范围内的已经修好了的电脑所在的集合即可。
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 13361 | Accepted: 5659 |
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
POJ Monthly,HQM
题意:给你 N 台坏了的电脑的坐标 ,和一个距离范围 d .
(在距离范围内的电脑可以相互通信,每台电脑也可以连接两台不同的电脑,使他们之间能够通信)
输入任意次数操作:
O x 表示修理好编号为 x 台的电脑
S x y 判断电脑 x 和 y 是否能够通信 。能则 SUCCESS ,不能则 FAIL
算法:并查集简单应用,判断两点是否在同一连通分量
思路:并查集部分很好处理。
关键是判断两台电脑是否在给定范围内,这里要用一个map[][]数组标记连通性,用一个used[]数组标记是否修理过。
每修一台电脑,则连接【合并】这台电脑和已经距离这台电脑 d 范围内的已经修好了的电脑所在的集合即可。
H | Accepted | 1164 KB | 1125 ms | C++ | 1714 B | 2013-04-10 18:48:34 |
#include<cstdio> #include<cmath> #include<cstring> const double esp = 1e-7; const int maxn = 1000+10; int p[maxn]; //父节点 bool used[maxn]; //标记改点是否修理 bool map[maxn][maxn]; //标记连通性 struct Point { double x, y; }point[maxn]; int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } void Union(int x, int y) { int fx = find(x); int fy = find(y); if(fx == fy) return; p[fx] = fy; //随意 } double dist(Point a, Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { int n, d; while(scanf("%d%d", &n, &d) != EOF) { memset(map, false, sizeof(map)); memset(used, false, sizeof(used)); for(int i = 1; i <= n; i++) { scanf("%lf%lf", &point[i].x, &point[i].y); } for(int i = 1; i <= n; i++) { p[i] = i; map[i][i] = true; for(int j = i+1; j <= n; j++) { if(dist(point[i], point[j])-d < esp) { map[i][j] = true; //在距离范围内 map[j][i] = true; } } } char c; int x, y; getchar(); while(scanf("%c", &c) != EOF) { if(c == 'O') { scanf("%d%*c", &x); if(!used[x]) //以前未被修理 { for(int i = 1; i <= n; i++) //依次判断 { if(used[i] && map[i][x]) Union(x, i); } used[x] = true; } } else if(c == 'S') { scanf("%d%d%*c", &x, &y); if(find(x) == find(y)) printf("SUCCESS\n"); //连通 else printf("FAIL\n"); //不连通 } } } return 0; }
相关文章推荐
- POJ 2236 Wireless Network 【并查集的简单应用 判断是否在同一连通分量】
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- poj 2236 加点 然后判断某两点是否连通
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- (用树结构支持并查集8.2.2)POJ 1703 Find them, Catch them(并查集的简单使用: 判断两个元素是否属于同一集合)
- (使用树结构来支持并查集操作8.4.2)POJ 2236 Wireless Network(判断两个元素是否在同一个集合&&有条件的合并)
- poj 2236 Wireless Network (简单的并查集应用)
- (判断是否为弱联通分量) poj 2762
- poj 1611 The Suspects 初级->数据结构->简单并查集的应用
- POJ 2236 Wireless Network(简单并查集)
- hdu 2120 Ice_cream's world I(判断是否有环,简单的并查集)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- Bridge - 判断图是否连通 - 图的遍历 or 并查集搜索 or 强连通算法
- POJ 2236 (简单并查集) Wireless Network
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- 连通分量个数(并查集的应用)
- POJ 2942 ( 补图+判断奇圈(交叉染色法)+Tarjan(点连通分量))
- poj 1611 简单并查集的应用
- HDU-1863(最小生成树+并查集判断是否连通)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)