Wireless Network
Time Limit: 10000MS |
| Memory Limit: 65536K |
Total Submissions: 20537 |
| Accepted: 8631 |
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台电脑,给出这些电脑的坐标xi,yi。 由于电缆受损,在距离为d之内的两台电脑才能通信,若距离超过d,但中间有媒介电脑也可以保持通信。 刚开始这些电脑都是坏的,O p 表示修复第p台电脑, S q p 表示询问q 与 p电脑是否能够通信,是输出SUCCESS 否输出FALL。
题解:很明显的并查集问题, 唯一注意的地方是,只有同时保证 电脑被修复了和电脑之间的距离小于d才能合并。
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
int tree[1010];
int sign[1010];//标记被修复的电脑
//double map[1010][1010];
int mark[1010][1010];//标记距离范围在d之内的两台电脑
struct node
{
int x,y;
}cpu[1010];
int n,d;
int find(int r)
{
if(tree[r]==r)
return r;
else
return tree[r]=find(tree[r]);
}
void merge(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
tree[fa]=fb;
}
void setmap()//以点建图
{
int i,j;
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
double dis=sqrt((cpu[i].x-cpu[j].x)*(cpu[i].x-cpu[j].x)*1.0+(cpu[i].y-cpu[j].y)*(cpu[i].y-cpu[j].y)*1.0);
if(dis<d+0.00000001)//注意修复精度啊,会wa的啦
mark[i][j]=mark[j][i]=1;
}
}
}
int main()
{
int i,a,b;
char s[5];
while(scanf("%d%d",&n,&d)!=EOF)
{
for(i=1;i<=n;++i)
scanf("%d%d",&cpu[i].x,&cpu[i].y);
for(i=1;i<=n;++i)
tree[i]=i;
memset(mark,0,sizeof(mark));
memset(sign,0,sizeof(sign));
setmap();
while(scanf("%s",s)!=EOF)
{
if(s[0]=='O')
{
scanf("%d",&a);
sign[a]=1;
for(i=1;i<=n;++i)
{
if(mark[i][a]&&sign[i])
merge(a,i);
}
}
else
{
scanf("%d%d",&a,&b);
if(find(a)==find(b))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
}
return 0;
}