标签:wireless network poj 2236 并查集
Language:
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 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台电脑损坏了,现在要将它们修复通讯,给出了这n台电脑的坐标位置,两台电脑i和j能够通讯的条件是:i和j都已经被修好的并且i,j之间的距离dis[i][j]<=d。有两种操作:1. O x 表示修好电脑x;2. S x y 表示检查电脑x和y是否联通,若联通输出SUCCESS,否则FALL。
思路:先把n台电脑之间的距离求出来,然后进行并查集操作,当修复电脑x时,检查其他电脑是否有已经修好的并且距离小于d的,有的话就将x和这台连起来。询问时之间看两个的father是否一样。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Computer { double x,y; }st[maxn]; bool ok[maxn]; int father[maxn]; double dis[maxn][maxn]; char op[2]; int n; double d; double Distance(Computer a,Computer b) { return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); } void init() { int i; mem(ok,false); FRL(i,0,maxn) father[i]=i; } int find_father(int x) { if (x!=father[x]) father[x]=find_father(father[x]); return father[x]; } int main() { int i,j; while (~scanf("%d%lf",&n,&d)) { FRE(i,1,n) scanf("%lf%lf",&st[i].x,&st[i].y); FRE(i,1,n) //求n个点之间的距离 { FRE(j,i,n) { if (i==j) dis[i][j]=0; else dis[i][j]=dis[j][i]=Distance(st[i],st[j]); } } init(); int a,b; while (~scanf("%s",op)) { if (op[0]=='O') { sf(a); ok[a]=true; int fa=find_father(a); FRE(i,1,n) //寻找其他已经修好的电脑,看能否相连 { if (i!=a&&ok[i]&&dis[i][a]<=d) father[ find_father(i) ]=fa; } } else { sff(a,b); int fa=find_father(a); int fb=find_father(b); if (fa==fb) pf("SUCCESS\n"); else pf("FAIL\n"); } } } return 0; }
Wireless Network (poj 2236 并查集)
标签:wireless network poj 2236 并查集
原文地址:http://blog.csdn.net/u014422052/article/details/44516629