zoukankan      html  css  js  c++  java
  • Wireless Network

    题意 : 就是 几台电脑,知道他们的坐标 ,两台修好的电脑如果距离<=d就可以联网, O p 代表 修理p电脑 S p q代表链接pq

    题解 : 并查集

    坑点 : FAIL 不要打成 FALL 我wa了的时候没找到原因,后来还是我看到别人的提醒才发现我也打成了FALL o(╯□╰)o

    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 #include<stdio.h>
      2 
      3 #include<iostream>
      4 
      5 #include<cmath>
      6 
      7 using namespace std;
      8 
      9 
     10 
     11 #define n 1010
     12 
     13 
     14 
     15 int pre[n],x[n],y[n];
     16 
     17 int m,d,k1,k2,i,r,r1;
     18 
     19 bool v[n];
     20 
     21 char ch;
     22 
     23 
     24 
     25 int find(int x)
     26 
     27 {
     28 
     29 if(x!=pre[x])
     30 
     31 pre[x]=find(pre[x]);
     32 
     33 return pre[x];
     34 
     35 }
     36 
     37 
     38 
     39 bool dis(int a,int b)//判断两台电脑之间的距离可不可以联网
     40 
     41 {
     42 
     43 int q=x[a]-x[b];
     44 
     45 int p=y[a]-y[b];
     46 
     47 if(q*q+p*p<=d*d)
     48 
     49 return true;
     50 
     51 else
     52 
     53 return false;
     54 
     55 }
     56 
     57 
     58 
     59 int main()
     60 
     61 {
     62 
     63 scanf("%d %d",&m,&d);
     64 
     65 for(i=1;i<=n;i++)//初始化
     66 
     67 {
     68 
     69 pre[i]=i;
     70 
     71 v[i]=false;
     72 
     73 }
     74 
     75 
     76 
     77 for(i=1;i<=m;i++)
     78 
     79 scanf("%d %d",&x[i],&y[i]);
     80 
     81 while(cin>>ch)
     82 
     83 {
     84 
     85 if(ch=='O')
     86 
     87 {
     88 
     89 scanf("%d",&r);
     90 
     91 v[r]=true;
     92 
     93 for(i=1;i<=m;i++)
     94 
     95 if(i!=r&&v[i]&&dis(i,r))
     96 
     97 {
     98 
     99 k1=find(r);
    100 
    101 k2=find(i);
    102 
    103 pre[k1]=k2;
    104 
    105 }
    106 
    107 }
    108 
    109 if(ch=='S')
    110 
    111 {
    112 
    113 scanf("%d %d",&r,&r1);
    114 
    115 k1=find(r);
    116 
    117 k2=find(r1);
    118 
    119 if(k1==k2)
    120 
    121 puts("SUCCESS");
    122 
    123 else
    124 
    125 puts("FAIL");
    126 
    127 }
    128 
    129 }
    130 
    131 return 0;
    132 
    133 }
  • 相关阅读:
    剑指Offer-11.二进制中1的个数(C++/Java)
    剑指Offer-10.矩形覆盖(C++/Java)
    剑指Offer-9.变态跳台阶(C++/Java)
    UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
    UVA1607 Gates 与非门电路 (二分)
    UVA 1451 Average平均值 (数形结合,斜率优化)
    UVA 1471 Defense Lines 防线 (LIS变形)
    UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
    UVA 11134 FabledRooks 传说中的车 (问题分解)
    UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
  • 原文地址:https://www.cnblogs.com/awsent/p/4267018.html
Copyright © 2011-2022 走看看