题目
Wireless Network POJ - 2236
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:
- “O p” (1 <= p <= N), which means repairing computer p.
- “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台电脑,d表示电脑能够连接距离,前n行为电脑坐标,字母o表示维修电脑,电脑一开始都是坏的,只有修好的才能用,s查询两台电脑能否连接,使用并查集将新修好的电脑连接到已修好的电脑中。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
struct node
{
int x, y;
int vis;
}s[10000];
int sfd[10000];
int n, d;
int find(int x)
{
while (sfd[x] != x)
{
x = sfd[x];
}
return x;
}
void unid(int x)
{
s[x].vis = 1;
for (int i = 1; i <= n; i++)
{
if (s[i].vis && i != x)
{
double red = sqrt((s[x].x - s[i].x) * (s[x].x - s[i].x) + (s[x].y - s[i].y) * (s[x].y - s[i].y));
if (red <= d)
{
int ii = find(i);
sfd[ii] = x;
}
}
}
}
void chaxun(int x, int y)
{
if (find(x) == find(y))
{
cout << "SUCCESS" << endl;
}
else
cout << "FAIL" << endl;
}
int main()
{
cin >> n >> d;
for (int i = 1; i <= n; i++)
{
cin >> s[i].x >> s[i].y;
s[i].vis = 0;
}
char w;
for (int i = 1; i <= n; i++)
{
sfd[i] = i;
}
while (cin >> w)
{
if (w == 'O')
{
int q;
cin >> q;
unid(q);
}
if (w == 'S')
{
int i, j;
cin >> i >> j;
chaxun(i, j);
}
}
}