题意:先给出一些笛卡尔坐标系内的一些坏电脑的坐标,电脑通信范围是d,坏电脑是无法完成通信的,O是修一台电脑的指令, S是查询两台电脑之间是否可以通信的指令,
解题思路:每修好一台电脑,就查询在d的范围内有没有修好的电脑,若有修好的电脑就unite在一起,查询时看一下两台电脑是否同根就行了。简单并查集。
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.
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
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 1050
const int INF = 0x3f3f3f3f;
int parent[MAX_N];
bool isgood[MAX_N];//记录修好的电脑,没修好的电脑就算在修好的电脑的距离内,也无法通信
struct node
{
int x, y;
}a[MAX_N];
void init(int n)
{
for(int i = 0 ; i < n + 5 ; i++)
{
parent[i] = i;
}
memset(isgood, false, sizeof(isgood));
}
int find(int a)
{
if(parent[a] != a)
{
parent[a] = find(parent[a]);
}
return parent[a];
}
void unite(int a, int b)
{
int fa = find(a);
int fb = find(b);
if(fa == fb) return;
parent[fb] = fa;
}
int same(int a, int b)
{
return find(a) == find(b);
}
int main()
{
int n, d;
n = read();
d = read();
init(n);
for(int i = 1 ; i <= n ; i++)
{
a[i].x = read();
a[i].y = read();
}
char op[5];
while(scanf("%s", &op) != EOF)
{
if(op[0] == 'O')
{
int num = read();
isgood[num] = true;
for(int i = 1 ; i <= n ; i++)
{
if(i == num) continue;
int x = a[num].x - a[i].x;
int y = a[num].y - a[i].y;
if(isgood[i] && x * x + y * y <= d * d)
{
unite(i, num);
}
}
}
else
{
int a = read();
int b = read();
if(same(a, b))
{
cout<<"SUCCESS"<<endl;
}
else
{
cout<<"FAIL"<<endl;
}
}
}
return 0;
}