题目链接:
题目:
Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions:33981 | Accepted: 14116 |
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(编号1~N)和两台计算机可以直接通信的最大距离d,下面n行输入对应编号i的坐标xi和yi。从n+1行开始输入操作:
1.“O p”(1 <= p <= N),表示修复计算机p。 注意这是大写O不是0。
2.“S p q”(1 <= p,q <= N),这意味着测试计算机p和q是否可以通信。
显然这是个并查集问题,如果两台电脑都被修复且其距离在最大距离内,就可以将其连起来。
AC代码如下:
#include <stdio.h>
#include <math.h>
#define maxn 1001
struct Node
{
int x,y,flag;
} a[maxn];
int pre[maxn],d;
int Find(int x)
{
return x==pre[x]?x:Find(pre[x]);
}
void is_join(int q,int p)
{
int n=Find(q),m=Find(p);
int z=(a[q].x-a[p].x)*(a[q].x-a[p].x)+(a[q].y-a[p].y)*(a[q].y-a[p].y);
if (a[q].flag&&z<=d*d&&n!=m)
{//由题可知q已被修复,如果 a[q].flag为真则两者均被修复
//z为两台电脑之间距离的平方,如果z<=d*d则其能进行通信
//如果n!=m代表两者还有连起来,将他们连起来
//上面三个同时成立时,将p的最上级赋为q的最上级
pre[n]=m;
}
}
int main()
{
int n,e,f;
char c;
scanf("%d%d",&n,&d);
for (int i=1; i<=n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].flag=0;
pre[i]=i;
}
getchar();
while(~scanf("%c ",&c))
{
if (c=='O')
{
scanf("%d",&e);
if (a[e].flag)
continue;
a[e].flag=1;
for (int i=1; i<=n; i++)
is_join(i,e);
}
else if (c=='S')
{
scanf("%d%d",&e,&f);
if (Find(e)==Find(f))
printf("SUCCESS\n");
else
printf("FAIL\n");
}
}
return 0;
}