zoukankan      html  css  js  c++  java
  • POJ

    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

    题目链接:http://poj.org/problem?id=2236

    ******************************************

    题意:给你n台电脑,然后给出给各电脑坐标,距离不超过d才可以沟通。一开始电脑都是坏的,o操作代表修电脑,s操作判断x与y是否可以联通。 

    分析:s查询的时候就是简单的并查集,但对于修复更新的时候要注意距离的判断,符合要求的才可以。

    一开始FAIL 写成了FALL,唉

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<queue>
     5 #include<algorithm>
     6 #include<time.h>
     7 using namespace std;
     8 
     9 #define N 1550
    10 #define INF 0x3f3f3f3f
    11 
    12 int Father[N];
    13 
    14 struct node
    15 {
    16     int x,y,v;
    17 }a[N];///保存所有的村庄,v标记是否已经修复
    18 
    19 int Find(int x)
    20 {
    21     if(x != Father[x])
    22        x=Find(Father[x]);
    23     return x;
    24 }
    25 
    26 int Len(node a,node b)///求两个村庄的距离
    27 {
    28   int x=b.x-a.x;
    29   int y=b.y-a.y;
    30   return x*x+y*y;
    31 }
    32 
    33 int main()
    34 {
    35     int n,i,b,c,d;
    36     char s[10];
    37 
    38     scanf("%d %d", &n,&d);
    39     d=d*d;///因为距离只做判断不做计算,所以直接用平方数判断可以避免小数,更加精确
    40 
    41     for(i=1;i<=n;i++)
    42     {
    43         scanf("%d %d", &a[i].x,&a[i].y);
    44         a[i].v=0;
    45         Father[i]=i;
    46     }
    47 
    48     while(scanf("%s", s) != EOF)
    49     {
    50         if(s[0]=='O')
    51         {
    52             scanf("%d", &b);
    53             if(a[b].v==0)
    54             {
    55                 a[b].v=1;
    56                 for(i=1;i<=n;i++)
    57                 {
    58                     if(b!=i&&a[i].v&&Len(a[i],a[b])<=d)
    59                     {
    60                         int j=Find(i);
    61                         int k=Find(b);
    62 
    63                         Father[k]=j;
    64                     }
    65                 }
    66             }
    67 
    68         }
    69         else
    70         {
    71             scanf("%d %d", &b,&c);
    72             int j=Find(b);
    73             int k=Find(c);
    74             if(j==k)
    75                 printf("SUCCESS
    ");
    76             else
    77                 printf("FAIL
    ");
    78         }
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    2014年去哪儿网笔试题--一个10*10的矩阵(可以理解为棋盘),随时生成一组数据填入矩阵,任何一个位置的数字除4进行计算,按余数着色...
    2014年去哪儿网笔试题--给定一个整型数组,对这个整型素组排序,使得按序拼接数组各元素得到的值最小。
    剑指Offer:面试题25
    字符串模式匹配KMP算法
    堆排序算法原理
    HashMap和HashTable 学习
    JAVA的IO学习
    输出图片的php代码前面不能有空白行
    PHP中最容易忘记的一些知识点总结
    PHP中cookie与session总结
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5703932.html
Copyright © 2011-2022 走看看