Wireless Network

来源:互联网 发布:解放军信大分配知乎 编辑:程序博客网 时间:2024/05/16 07:32

Wireless Network

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3
Problem 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并查集的应用,所要做的操作就是需要进行判断是否在距离范围之内,再加入并查集,所做的优化可以是对已经修复的电脑进行,标记,检测是否可以连接是直接从标记的节点中查找其他的就是并查集的一些具体的实现代码如下:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>using namespace std;#define maxn 10010//节点个数int set1[maxn];//并查集数组int N,D;struct node{    int x,y;   int flag;//标记是否修好   int link;    int num;//节点编号}done[maxn];int b[maxn];//已修复的电脑数int fa(int x)// 路径压缩,及查找父节点{    if(set1[x]==x)        return x;    else        return set1[x]=fa(set1[x]);}void build(int x,int y)//连接节点,{    int x1=fa(x);    int y1=fa(y);    if(x1!=y1)        set1[x1]=y1;        //cout<<"set1[x1]"<<set1[x1]<<endl;}double distant(int x,int y){    return sqrt(x*x+y*y);}void inse(int n,int m){    for(int i=1;i<n&&b[i]!=m;i++)    {        int x1=done[b[i]].x-done[m].x;        int y1=done[b[i]].y-done[m].y;        //cout<<"x1->"<<x1<<"->"<<"done.x"<<done[b[i]].x<<endl;       // cout<<"y1->"<<y1<<"->"<<"done.y"<<done[b[i]].y<<endl;        if(distant(x1,y1)<=D)        {            build(b[i],m);        }    }}int main(){    int k=1;    char str[3];    int a,c;    for(int i=0;i<maxn;i++)        set1[i]=i;    scanf("%d %d",&N,&D);    for(int i=1;i<=N;i++)    {        scanf("%d %d",&a,&c);        done[i].x=a;        done[i].y=c;        done[i].flag=0;        done[i].num=i;    }    while(scanf("%s",str)!=EOF)    {        if(str[0]=='O')        {            int m;            cin>>m;            done[m].flag=1;            done[m].link=0;            b[k]=m;            k++;            //检测修复好的编号是否可以加入并查集            inse(k,m);        }        else if(str[0]=='S')        {            int x,y;            scanf("%d %d",&x,&y);            if(fa(x)==fa(y))            {                // cout<<"x>>"<<fa(x)<<endl;               // cout<<"y>>"<<fa(y)<<endl;                printf("SUCCESS\n");            }            else            {                //cout<<"x>>"<<fa(x)<<endl;                //cout<<"y>>"<<fa(y)<<endl;                printf("FAIL\n");            }        }    }}

 
0 0