Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 21610 | Accepted: 9052 |
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
Source
POJ Monthly,HQM
题意:
有n 个通信站,编号1-n,全部是坏掉的,而且有效通信范围为距离d,现在给出两种操作
1,O p 表示修好编号为p的通信站
2,S p q 表示测试p和q 通信站是否能通信,输出对应的结果
题解:
查找元素是否在同一个集合内,并查集的运用,不过,要注意给出了通信站的通信范围,以及对每个通信站的操作,需要进行标记处理,
当进行修理操作时,就把这一点和在它通信范围内的所有的已经修理好的点连接起来,查询的时候需要进行查找两个元素是否在同一个集合中....
/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
int pre[maxn],vis[maxn];
struct point
{
double x,y;
}p[maxn];
void init(int n)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;++i)
{
pre[i]=i;
}
}
bool ok(point a,point b,double d)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)<=d*d;//是否能覆盖到
}
int find(int x)
{
int r=x;
while(pre[r]!=r)
{
r=pre[r];
}
int i=x,j;
while(i!=r)
{
j=pre[i];pre[i]=r;i=j;
}
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
}
}
void add(int x,int n,double d)//修理通信站,与其他通信站连接
{
if(!vis[x])
{
vis[x]=1;
for(int i=1;i<=n;++i)
{
if(i!=x&&vis[i]&&ok(p[x],p[i],d))
{
join(x,i);//合并操作
}
}
}
}
int main()
{
int n;double d;
// freopen("shuju.txt","r",stdin);
scanf("%d%lf",&n,&d);
init(n);
for(int i=1;i<=n;++i)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
char s[10];
while(~scanf("%s",s))
{
if(s[0]=='O')
{
int x;
scanf("%d",&x);
add(x,n,d);
}
else
{
int x,y;
scanf("%d%d",&x,&y);
int fx=find(x),fy=find(y);
if(fx!=fy)
{
printf("FAIL\n");
}
else
{
printf("SUCCESS\n");
}
}
}
return 0;
}