poj 2236 Wireless Network

来源:互联网 发布:双色球软件2016破解 编辑:程序博客网 时间:2024/06/11 15:45

题目描述
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

大致题意:首先输入n和d,n表示有n台编号1~n的电脑,d表示两台能联系上的电脑的最大距离。接下来n行每行输入两个数x,y表示编号1~n的电脑的坐标。接下来有一系列的两种操作,当输入为O x,x为一个整数时,表示修复编号为x的电脑,当输入为S x y,x,y分别表示两个整数,表示查询编号x和y的两台电脑能否联系上,如果能则输出SUCCESS否则输出FAIL。 一开始所有的电脑都是坏的,两台好的电脑可以通过他们所能联系上的好电脑进行联系。

思路:简单的并查集,每次修复一个电脑,就遍历其与其他修好的电脑是否在同一个集合中,如果不是的话再判断他们之间的距离是否小于等于d,如果是则将其加入。然后查询的时候判断一下两个电脑是否有在同一集合中即可。
(注意操作时输入的字母O而不是数字0。。。。。)

代码如下

#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<map>using namespace std;int pre[1005];int vis[1005];//判断是否已经修好int w[1005][2];int N,d;int find(int x){    int r=x;   while (pre[r]!=r)   r=pre[r];   int i=x; int j;   while(i!=r)   {       j=pre[i];       pre[i]=r;       i=j;   }   return r;}int main(){    for(int i=0;i<1005;i++)    pre[i]=i;    memset(vis,0,sizeof(vis));    cin>>N>>d;       for(int i=1;i<=N;i++)      cin>>w[i][0]>>w[i][1];      getchar();      char c;      while(cin>>c)      {        if(c=='O')        {            int x;            cin>>x;            vis[x]=1;            for(int i=1;i<=N;i++)            {                if(i!=x&&vis[i]==1)                {                    int dx=w[x][0]-w[i][0];                    int dy=w[x][1]-w[i][1];                    dx*=dx;                    dy*=dy;                    if(dx+dy<=d*d)                    {                        int f1=find(i);                        int f2=find(x);                        if(f1!=f2)                        {                            pre[f2]=f1;                          }                      }                  }              }          }          else           {            int x,y;            cin>>x>>y;            getchar();            int f1=find(x);            int f2=find(y);            if(f1==f2)            cout<<"SUCCESS"<<endl;            else             cout<<"FAIL"<<endl;          }      }   return 0;}
0 0
原创粉丝点击