Wireless Network
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 29039 | Accepted: 12030 |
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
代码
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
//我们根据题意了解下面这些信息:
//1、如果连接的距离是d,没有电脑在这范围内,那么这台电脑是孤立的
//2、如果连接的距离是d,如果有电脑在这范围内,那么连接的这台电脑就可以说组成了一个更大的电脑
//什么是更大的电脑呢?就是我能连接到的电脑你也能连接到,而我们的范围就是能达到的最远距离。
//3、==> 推论 ==>我们可以把这些电脑看成一个集合,只要能达到这台电脑就把他加进这个集合,达不到就是孤立的。
//那这样的问题我们可以看成是并查集的问题。
int computer[1002]; //存放节点的根节点
int X[1002];
int Y[1002]; //存放电脑的坐标
int link[1002][1002]; //后加的,为了处理某个事件
int pow(int a){
return a*a;
}
int find(int a){
if(computer[a]!=a)
{
computer[a]=find(computer[a]);
}
return computer[a];
}
void make(int a,int b){
int C1=find(a);
int C2=find(b);
if(C1!=C2)
{
computer[C2]=C1;
}
}
void check(int p,int q){
int C1=find(p);
int C2=find(q);
if(C1==C2)
{
cout<<"SUCCESS"<<endl;
}
else
{
cout<<"FAIL"<<endl;
}
}
int main(){
int N;
double d; //分别代表有多少个节点,和每个节点能连接的最远距离 //d是double一开始我写的int真的该打了。
int fixed[1002];
cin>>N>>d;
for(int i=1;i<=N;i++) computer[i]=i; //初始化,每个根节点暂时都是自己
memset(X,0,sizeof(X));
memset(X,0,sizeof(Y)); //初始化,X,y的坐标都是0
memset(link,0,sizeof(link)); //link数组是我遇到问题之后加的
memset(fixed,0,sizeof(fixed));
for(int i=1;i<=N;i++)
{
cin>>X[i]>>Y[i];
} //初始化电脑坐标
char s[5]=""; //O or S
for(int i=1;i<=N;i++)
{
for(int j=i+1;j<=N;j++)
{
if( sqrt( (double) ( pow(X[i]-X[j])+pow(Y[i]-Y[j]) ) ) <= d )
{
link[i][j]=1;
link[j][i]=1;
}
}
}
//前面的都是初始化
while(scanf("%s",s)!=EOF)
{
if( strcmp(s,"O")==0 )
{
int p;
cin>>p;
fixed[p]=1;
//这道题稍微难的地方就是这里你怎么样判断你输入的这台电脑已经被包括进一个集合,即最上面的第2步
//你用什么数据来表示,用什么量来测量,这也是一小部分建模的一部分
//我们再定一个数组,link[N][N];link[1][2]=0代表0和1不连接,link[1][2]=1;代表1和2连接
for(int i=1;i<=N;i++)
{
if(link[i][p]==1 && fixed[i]==1)
{
make(i,p);
}
}
}
else
{
int p,q;
cin>>p>>q;
check(p,q);
}
}
return 0;
}
我fixed数组忘记初始化,我来来回回改了三次不知道哪里错了,sqrt((double))没加double又提交了两次,数组int[N]变量又提交了一次(现在规则是可以有变量的,可最好不要这么做),还有就是程序里不要有string类型的变量