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
解题思路:
首先我们统计每个结点的所有在D范围的结点,即将这个结点半径内的所有结点进行保存
然后根据操作,O表示修理某结点,我们就将这个结点与它范围内所有修理完毕的结点连到同一个并查集上去
S操作时判断它们在不在同一个并查集即可
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAXN = 1010;
int N, D;
struct smPos {
int x, y;
}smPoses[MAXN];
int smFather[MAXN];
bool smRepaired[MAXN]; //是否被修理
vector<int> Gr[MAXN];
//平面坐标集
double smgetEularPath(smPos sa, smPos sb) {
return sqrt(pow(1.0*(sa.x - sb.x), 2) + pow(1.0*(sa.y - sb.y), 2));
}
//初始化并查集
void initsmSet() {
for (int i = 1; i <= N; ++i) {
smFather[i] = i; //并查集初始化
}
}
//查找
int smFind(int x) {
if (smFather[x] != x) {
smFather[x] = smFind(smFather[x]); //查找父节点
}
return smFather[x];
}
//是否在并查集上
bool isSames(int a, int b) {
int fa = smFind(a);
int fb = smFind(b);
if (fa == fb) return true;
else return false;
}
//合并
void smUnit(int a, int b) {
int fa = smFind(a);
int fb = smFind(b);
if (fa != fb) {
smFather[fa] = fb;
}
}
//更新
void smUpdate(int x) {
smRepaired[x] = true;
for (int i = 0; i < Gr[x].size(); ++i) {
if (smRepaired[Gr[x][i]] && !isSames(x, Gr[x][i])) {
smUnit(x, Gr[x][i]); //把符和长度范围的放到一条并查集上去
}
}
}
int main() {
cin >> N >> D;
for (int i = 1; i <= N; ++i) {
cin >> smPoses[i].x >> smPoses[i].y;
}
initsmSet(); //初始化并查集
memset(smRepaired, false, sizeof(smRepaired));
//统计两个点,两两之间是否符和范围
for (int i = 1; i <= N; ++i) {
for (int j = i + 1; j <= N; ++j) {
if (smgetEularPath(smPoses[i], smPoses[j]) <= D) {
Gr[i].push_back(j);
Gr[j].push_back(i); //插入元素
}
}
}
char op;
int oa, ob;
char c = getchar();
while (scanf("%c %d", &op, &oa) != EOF) {
if (op == 'O') { //维护
smUpdate(oa);
}
else{
scanf("%d", &ob);
if (isSames(oa, ob)) {
printf("SUCCESS\n");
}
else {
printf("FAIL\n");
}
}
char c = getchar();
}
system("PAUSE");
return 0;
}