Wireless Network poj 2236

来源:互联网 发布:装修效果图设计软件 编辑:程序博客网 时间:2024/06/07 07:21
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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4
Sample Output
FAILSUCCESS
题意
  有一个计算机网络的所有线路都坏了,网络中有n台计算机,现在你可以做两种操作,修理(O)和检测两台计算机是否连通(S),只有修理好的计算机才能连通。连通有个规则,两台计算机的距离不能超过给定的最大距离D(一开始会给你n台计算机的坐标)。检测的时候输出两台计算机是否能连通。
 
  思路
  每次修理好一台计算机的时候就遍历一下所有修好的计算机,看距离是否<=D,如果符合说明可以连通,将两台计算机所在集合合并。
  每次检查的时候判断一下这两台计算机是否在同一集合中即可。
 
  注意
  1、坐标之后给出的计算机编号都是n+1的。例如O 3,他实际上修理的是编号为2的计算机,因为计算机是从0开始编号的。
  2、比较距离的时候注意要用浮点数比较,否则会WA。
  3、"FAIL"不要写成"FALL"。
  4、字符串输入的时候注意处理好回车,空格等情况。
  5、注意N的范围(1 <= N <= 1001),最大是1001,不是1000。是个小坑
#include<iostream>#include <cstring>using namespace std;int pre[1010],x[1010],y[1010];int flag[1010];int n,d;int find(int n){    int r=n;    while(pre[r]!=r)        r=pre[r];    int i=n,j;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void join(int x,int y){     if(find(x)==find(y))      return;     else      pre[find(x)]=find(y);}int main(){int j,m,l;char ch;cin>>n>>d;for(int i=1;i<=n;i++){cin>>x[i]>>y[i];}    for(int i=1;i<=n;i++)pre[i]=i; memset(flag,0,sizeof(flag)); while(cin>>ch) {        switch(ch){           case 'O':                   cin>>j;                     flag[j]=1;                      for(int i=1;i<=n;i++)                        if(i!=j&&flag[i]&&((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]))<=d*d)                            join(i,j);                       break;            case 'S':                       cin>>m>>l;                       if(find(m)==find(l))                                cout<<"SUCCESS"<<endl;                          else                              cout<<"FAIL"<<endl;                        break;} }       return 0;}
原创粉丝点击