原题:
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:
- “O p” (1 <= p <= N), which means repairing computer p.
- “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台电脑,编号从1至n,每一个都有自己的二维坐标位置。这些电脑一开始就是坏的,可以再修复后与距离小于D的电脑连接,只要两台电脑都可以共同连接一个电脑,那么这两台电脑之间也可以连接。现在可以执行两种操作:
1.O i 修复第i台电脑。
2.S i j 查询第i台和第j台电脑之间是否可以连接,可以则输出SUCCESS,不可以输出FAIL。
题解:
并查集问题,每一次修复一台电脑根据D来判断是否可以归并至一组,查询时直接看两台电脑是否为一组即可。
附上AC代码:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int n,d,f[1005];
bool vis[1005];
//电脑结构体
struct point
{
int x,y;
}com[1005];
//计算两点间距离
double dis(int i,int j)
{
point a,b;
a=com[i],b=com[j];
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//找出其祖先
int findf(int i)
{
if(f[i]!=i)
return findf(f[i]);
return f[i];
}
//将两个电脑归并至一组
void setf(int x,int y)
{
int a=findf(x),b=findf(y);
if(a!=b)
f[b]=a;
}
//设置第i台电脑可以使用,并且找出他可以归并的组,将其归并到一起
void change(int i)
{
for(int j=1;j<=n;j++)
{
if(i==j)continue;
if(vis[j]&&dis(i,j)<=d)
{
setf(i,j);
}
}
}
//检查两个是否是一组的
void check(int i,int j)
{
if(findf(i)==findf(j))
cout<<"SUCCESS"<<endl;
else
cout<<"FAIL"<<endl;
}
//测试过程用的
void test()
{
for(int i=1;i<=n;++i)
{
cout<<f[i]<<" ";
if(i%10==9)cout<<endl;
}
cout<<endl;
}
int main()
{
ios::sync_with_stdio(false);
int tmp1,tmp2;
char op;
memset(vis,0,sizeof(vis));//初始化
cin>>n>>d;
for(int i=1;i<=n;++i)
{
cin>>com[i].x>>com[i].y;
f[i]=i;
}
while(cin>>op)
{
if(op=='O')
{
cin>>tmp1;
vis[tmp1]=1;
change(tmp1);
}
else
{
cin>>tmp1>>tmp2;
check(tmp1,tmp2);
}
//test();
}
return 0;
}
欢迎评论!