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 }
  • 相关阅读:
    看完这篇,网络面试稳了!
    Python 单元测试详解
    聊一聊,Python自动化测试框架
    测试妹纸说,你这用了几年的postman,只用了它的皮毛
    Win系统设置Apache Tomcat开机后台自动启动
    .Net Framework中的委托与事件——热水器事例
    Unity3d学习清单
    python 进制、ASCII码转换
    python正则 re 模块函数
    mysql 简单手工注入
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5703932.html
Copyright © 2011-2022 走看看