Wireless Network
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 25980 Accepted: 10817
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.
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
Source
POJ Monthly,HQM
题意:有N台电脑,电脑修好并且之间距离小于等于d才可以通讯。给出两种操作,O代表修点,S代表测试两台电脑是否可以通讯
题解:简单并查集应用题,做好坐标之间的存放判断,修好的电脑的标志存放即可
#include
#include
#include
#define MAXN 1050
using namespace std;
//x,y:电脑的坐标
//len:目前修过的电脑的数量(包括重复的)
//fa:并查集
//repaired:存放修过的电脑的ID
int x[MAXN],y[MAXN],len=0,fa[MAXN],repaired[MAXN]={0};
//得到距离
double getDis(int i,int com_a){
return sqrt((double)((x[i]-x[com_a])*(x[i]-x[com_a])
+(y[i]-y[com_a])*(y[i]-y[com_a])));
}
//找到父亲
int father(int fx) {
if (fx!=fa[fx])
fa[fx]=father(fa[fx]);
return fa[fx];
}
//加入集合
void join(int a,int b){
int fx= father(a), fy = father(b);
fa[fx]=fy;
}
int main()
{
int N,d,i;
scanf("%d%d",&N,&d);
for(i=1;i<=N;i++){
scanf("%d%d",&x[i],&y[i]);
//初始化并查集
fa[i]=i;
}
char op;
int com_a,com_b;
while(scanf("%c",&op)!=EOF){
if(op=='O'){
scanf("%d",&com_a);
repaired[++len]=com_a;
for(int i=1;i<=len;i++){
if(repaired[i]!=0&&repaired[i]!=com_a){
double dis=getDis(repaired[i],com_a);
//距离判断
if(dis<=(double)d){
join(repaired[i],com_a);
}
}
}
}
else if(op=='S'){
scanf("%d%d",&com_a,&com_b);
if(father(com_a)==father(com_b)){
printf("SUCCESS\n");
}
else{
printf("FAIL\n");
}
}
}
}