• poj 2236


    Wireless Network

    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 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
    思路:有距离条件的并查集,先写一个判断函数来判断是否可以加入,之后按题意查找即可。
    代码:
     1 Source Code
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 #define pi acos(-1.0)
     9 #define mj
    10 #define inf 0x3f3f3f
    11 typedef double db ;
    12 typedef long long  ll;
    13 using namespace std;
    14 const int N=1e3+5;
    15 const int mod=1e9+7;
    16 int f[N],dis[N],d;
    17 bool  has[N];
    18 pair<int ,int > po[N];
    19 vector<int>  e[N];
    20 int find(int x)
    21 {
    22     return f[x]==x?x:f[x]=find(f[x]);
    23 }
    24 void unit(int x,int y)
    25 {
    26     int fx=find(x),fy=find(y);
    27     if(fx!=fy)
    28         f[fx]=fy;
    29 }
    30 int main()
    31 {
    32     int n,d;
    33     scanf("%d%d",&n,&d);
    34     for(int i=1;i<=n;i++){
    35         f[i]=i;
    36         int  x,y;
    37         scanf("%d%d",&x,&y);
    38         po[i].first=x;
    39         po[i].second=y;
    40     }
    41     for(int i=1;i<n;i++){
    42         for(int j=i+1;j<=n;j++){
    43             int x1=po[i].first,y1=po[i].second,x2=po[j].first,y2=po[j].second;
    44             if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=d*d) {e[i].push_back(j);e[j].push_back(i);}
    45         }
    46     }
    47     char s[2];
    48     while(scanf("%s",s)==1){
    49         if(s[0]=='O'){
    50             int x;
    51             scanf("%d",&x);
    52             has[x]=1;
    53             for(int i=0;i<e[x].size();i++){
    54                 int  v=e[x][i];
    55                 if(has[v]){
    56                     unit(v,x);
    57                 }
    58             }
    59         }
    60         else
    61         {
    62              int x,y;
    63              scanf("%d%d",&x,&y);
    64 //             printf("%d find:%d %d find:%d
    ",x,find(x),y,find(y));
    65              if(find(x)==find(y)) puts("SUCCESS");
    66              else puts("FAIL");
    67         }
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    windows下编译php7图形库php_ui.dll
    php 图形用户界面GUI 开发
    使用PHPStorm 配置自定义的Apache与PHP环境
    公用代码实现两个表的拼接(部分代码)
    WCF学习心得--客户端获取服务端自定义类数据
    动态规划入门——Eddy's research II
    linux route命令学习
    软件度量都该度个啥?(5)——被吹得最多的六西格玛
    VSS的运用小内容(针对于vs2008版本)(小的问题都是,仅供参考--只针对于菜鸟级的)
    cocos2d-x学习日志(13) --A星寻路算法demo
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6504812.html
Copyright © 2020-2023  润新知