POJ 2236 Wireless Network [并查集+几何坐标 ]
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.
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.
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
[题意]:
二维平面有N台坏的电脑,给出所有电脑的坐标(X,Y)。然后给出2种指令:
O U 指令表示修复编号为U的电脑(电脑只有被修复之后才能进行数据连接)。
S U V 指令要求你返回 编号U电脑和 编号V电脑是否可以进行数据连接(只有U和V电脑同时被修复且连通才行)。
现在给出一个距离d,只有两台被修复的电脑且他们之间的距离<=d时才可以进行连通。且如果A与C连通,C与B连通,那么A与B可以通过C连通(也算A与B连通了)。
现在要你回答每一条S U V指令,如果U与V连通,输出SUCCESS ,否则输出FAIL。
[分析]:
每修复一台电脑,就把它与所有距离它不超过D且已经被修复的电脑连通(即合并两者的连通分量)。每次查询只需要看被查询的两台电脑是否被修复且都在同一个连通分量即可。
[代码]:
#include<iostream> #include<algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxn=1005; int fa[maxn],vis[maxn]; int n,d; struct node { int x,y; }p[maxn]; void init() { for(int i=1;i<=n;i++) fa[i]=i; memset(vis,0,sizeof(vis)); } int find(int x) { return fa[x]==x?x:find(fa[x]); } void join(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) fa[fx]=fy; } bool dis(int x, int y){ float a = p[x].x - p[y].x; float b = p[x].y - p[y].y; if(a * a + b * b <= d * d) return true; else return false; } int main() { char s[5]; int x,y,a,b; scanf("%d%d",&n,&d); for(x=1;x<=n;x++) scanf("%d %d",&p[x].x , &p[x].y); init(); while(~scanf("%s",s)) { if(s[0] == 'O') { scanf("%d",&x); vis[x]=1; for(y=1;y<=n;y++) { if(vis[y] && dis(x,y)) join(x,y); } } else { scanf("%d %d",&x, &y); if(vis[x] && vis[y] && find(x) == find(y)) printf("SUCCESS\n"); else printf("FAIL\n"); } } }