POJ2236 Wireless Network

来源:互联网 发布:威客网络兼职可靠吗 编辑:程序博客网 时间:2024/05/23 16:48

                                                Wireless Network
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 28851 Accepted: 11950

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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

Sample Output

FAILSUCCESS
题目大意是说一场地震把网络弄坏了,然后有几台电脑,修好以后若是满足条件便可以实现相连。给出每一台电脑的坐标,若是他们的距离小于给定的数,便可以实现连接


这道题需要用到并查集,每次输入O时,就计算当前电脑与已经连接了的电脑的距离,若是满足条件就将他们放在一起

如果是用c语言的输入输出要小心字母,幸好我用的是c++的~~~~~红红火火恍恍惚惚

#include<iostream>#include<cstring>using namespace std;int tree[1010],book[1010];//用book数组记录电脑是否有被修复struct Node{  int x,y;}node[1010];int n,m;int find(int x){  return x==tree[x]?x:tree[x]=find(tree[x]);}void merge(int x,int y){  int a=find(x);  int b=find(y);  if(a!=b) tree[b]=a;}bool judge(int x,int y){  int a=node[x].x-node[y].x;  int b=node[x].y-node[y].y;  //两点间的距离公式,计算两台电脑间是否可以相连  if(a*a+b*b<=m*m) return true;  return false;}int main(){  cin>>n>>m;  memset(book,0,sizeof(book));  for(int i=1;i<=n;i++) tree[i]=i;  for(int i=1;i<=n;i++) cin>>node[i].x>>node[i].y;  char ch;  while(cin>>ch){    if(ch=='O'){      int p;      cin>>p;      book[p]=1;      for(int i=1;i<=n;i++){        if(i!=p&&book[i]&&judge(i,p)){          //若满足条件,则将两台电脑合并          merge(i,p);        }      }    }    else if(ch=='S'){      int p,q;      cin>>p>>q;      //判断两台电脑是否能相连      //看他们的祖先是否相同      //若相同则可以      int a=find(p);      int b=find(q);      if(a==b) cout<<"SUCCESS"<<endl;      else cout<<"FAIL"<<endl;    }  }}



原创粉丝点击