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.
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
解题报告:
这道题用了并查集的知识,思路并不难想,但是有的细节需要注意一下,比如:在修好某个计算机时,接下来会和其他已经修好的计算机分别计算距离,并存入数组中。所以,如果已经修好就不用再进行这些操作,否则可能超时。再就是,有的题目中计算距离注意看int能否存下,如果太大要用long long.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<string.h>
#include<set>
using namespace std;
int dl[1005]={0};//记录修好电脑的id
int xf[1005]={0};//记录电脑是否修好,修好的值为1,否则为0
int f[1005];
int n,d;
struct node{
int u,v;
int w;
}edge[800000];
struct nod{
int id;
int x,y;
}c[1005];
void init()
{
for(int i=0;i<=n;i++)
{
f[i]=i;
}
}
int getf(int v)
{
if(f[v]!=v)
f[v]=getf(f[v]);
return f[v];
}
void merge(int u,int v)
{
int t1=getf(u);
int t2=getf(v);
if(t1!=t2)
{
f[t2]=t1;
}
}
int join(int u,int v)
{
int t1=getf(u);
int t2=getf(v);
if(t1==t2)
{
return 1;
}
else return 0;
}
int main()
{
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)
{
int t1,t2;
scanf("%d%d",&c[i].x,&c[i].y);
c[i].id=i;
}
getchar();
init(); //不要忘记加
char op;
int end=0;
while(scanf("%c",&op)!=EOF)
{
if(op=='O')
{
int t1;
scanf("%d",&t1);
if(xf[t1]==1)continue;//如果已经修好就不进行后面求距离的操作,能节约时间
xf[t1]=1;
for(int i=0;i<end;i++)
{
int di=(c[dl[i]].y-c[t1].y)*(c[dl[i]].y-c[t1].y)+(c[dl[i]].x-c[t1].x)*(c[dl[i]].x-c[t1].x);
if(di<=d*d)//距离的平方,注意d也要求平方
merge(dl[i],t1);
}
dl[end++]=t1;
}
else if(op=='S')
{
int t1,t2;
scanf("%d%d",&t1,&t2);
if(join(t1,t2)==1)
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}