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
并查集维护。
题意
有一个计算机网络的所有线路都坏了,网络中有n台计算机,现在你可以做两种操作,修理(O)和检测两台计算机是否连通(S),只有修理好的计算机才能连通。连通有个规则,两台计算机的距离不能超过给定的最大距离D(一开始会给你n台计算机的坐标)。检测的时候输出两台计算机是否能连通。
思路
先预处理输入所有电脑之间的距离,每次修复一个电脑后,访问所有已经修复电脑的距离于刚修复电脑的距离,如果小于D,
两个电脑并到一个集合里。
查询时直接get即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
const int M = 1000+100;
double x[M],y[M];
double D[M][M];
int rep[M],fa[M];
bool vis[M];
int get(int x)
{
if(x==fa[x])return x;
return fa[x]=get(fa[x]);
}
void merge(int x,int y)
{
fa[get(x)]=get(y);
}
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++)
cin>>x[i]>>y[i],fa[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
D[i][j]=dis(x[i],y[i],x[j],y[j]);
// cout<<D[i][j]<<" ";
// if(j==n)cout<<endl;
}
string s;
int cnt=0;
while(cin>>s)
{
if(s[0]=='S')
{
int l,r;
cin>>l>>r;
if(get(l)!=get(r))
puts("FAIL");
else
puts("SUCCESS");
}
else
{
int p;
cin>>p;
for(int i=1;i<=cnt;i++)
{
if(D[p][rep[i]]<=d)
merge(p,rep[i]);
}
if(!vis[p])
rep[++cnt]=p;
vis[p]=true;
}
}
return 0;
}