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
有n台损坏的电脑,现要将其逐台修复,且使其相互恢复通信功能。若两台电脑能相互通信,则有两种情况,一是他们之间的距离小于d,二是他们可以借助都可到达的第三台已修复的电脑。给出所有电脑的坐标位置,对其进行两种可能的操作,O x表示修复第x台,S x y表示判断x y之间能否通信,若能输出SUCCESS,否则输出FALL。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int father[1010];
int rep[1010]; //记录修好的设备
struct node
{
int i,j;
}num[1010];
int Find(int x)
{
if(x!=father[x])
{
return father[x]=Find(father[x]);
}
return father[x];
}
void Union(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y)
{
father[x]=y;
}
}
bool Dist(int i,int j,int d) //设备距离
{
int x=(num[i].i-num[j].i)*(num[i].i-num[j].i);
int y=(num[i].j-num[j].j)*(num[i].j-num[j].j);
if(x+y<=d*d)
{
return true;
}
return false;
}
int main()
{
int n,d;
char ch;
scanf("%d%d",&n,&d);
for (int i=1; i<=n; i++)
{
scanf("%d%d",&num[i].i,&num[i].j);
}
for (int i=1; i<=n; i++)
{
father[i]=i;
}
int ans=0;
int x,y;
getchar();
while (scanf("%c%d",&ch,&x)!=EOF)
{
if(ch=='O')
{
rep[ans++]=x;
for (int i=0; i<ans; i++) //在修好的设备中进行筛选合并
{
if(Dist(rep[i], x, d))
{
Union(rep[i], x);
}
}
}
else
{
scanf("%d",&y);
if(Find(x)!=Find(y))
{
printf("FAIL\n");
}
else
{
printf("SUCCESS\n");
}
}
getchar();
}
return 0;
}