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
题意:网络中的电脑全部瘫痪,如果计算机A和计算机B可以直接通信,或者有一台计算机C可以同时与计算机A和计算机B通信,那么计算机A和计算机B就可以通信。每台计算机只能与不超过d米远的计算机直接通信,在网络修复的过程中,工作人员可以随时进行两种操作,一种是修复一台计算机,另一种是测试两台计算机是否可以通信。
第一行包含两个整数N和d ,这里N为计算机数量,从1到N, D为两台计算机直接通信的最大距离。在接下来的N行中,每一行都包含两个整数xi, yi ,这是N台计算机的坐标。从(N+1)第一行到输入末尾,依次执行操作。
"O p" (1 <= p <= N)表示修理计算机p。
“S p q”(1 <= p, q <= N),即测试计算机p和q是否可以通信。
回答所有的测试操作,如果两台计算机可以通信,则打印“成功”;如果不能通信,则打印“失败”
题解:用并查集模板来写,需要注意一下精度的问题
代码如下:
Select Code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define p 1e-6
using namespace std;
int f[1010],a[1010],h;
struct Ti
{
double x,y;
}ti[1010];
int gef(int v)
{
return v==f[v]?v:f[v]=gef(f[v]);
}
void marge(int u,int v)
{
int t1,t2;
t1=gef(u);
t2=gef(v);
if(t1!=t2)
f[t2]=t1;
}
double aa(Ti a,Ti b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int n,i;
double d;
scanf("%d%lf",&n,&d);
for(i=1;i<=n;i++)
scanf("%lf%lf",&ti[i].x,&ti[i].y);
char c[2];
h=0;
for(i=0;i<=1010;i++)
f[i]=i;
memset(a,0,sizeof(a));
while(scanf("%s",c)!=EOF)
{
if(c[0]=='O')
{
int m;
scanf("%d",&m);
for(i=0;i<h;i++)
{
if(aa(ti[a[i]],ti[m])<=d+p)
marge(a[i],m);
}
a[h++]=m;
}
if(c[0]=='S')
{
int k1,k2;
scanf("%d%d",&k1,&k2);
if(gef(k1)==gef(k2))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}