• 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 }
  • 相关阅读:
    [Codeforces967C]Stairs and Elevators(二分查找)
    [Codeforces976E]Well played!(贪心)
    [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)
    [HDU2222]Keywords Search(AC自动机)
    [BZOJ1030][JSOI2007]文本生成器(AC自动机+DP)
    基于ZooKeeper实现——分布式锁与实现
    基于Redis实现——分布式锁与实现
    java 接口请求返回通用json
    Java SHAA加密
    (转)一些牛人榜样,多看看他们写的东西(后续整理牛人的blog等)
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6504812.html
Copyright © 2020-2023  润新知