zoukankan      html  css  js  c++  java
  • [北大机试F]:Wireless Network(并查集)

    总时间限制: 
    10000ms
     
    内存限制: 
    65536kB
    描述
    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.
    输入
    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.
    输出
    For each Testing operation, print "SUCCESS" if the two computers can communicate, or "FAIL" if not.
    样例输入
    4 1
    0 1
    0 2
    0 3
    0 4
    O 1
    O 2
    O 4
    S 1 4
    O 3
    S 1 4
    
    样例输出
    FAIL
    SUCCESS

    并查集,维修每台电脑时,暴力遍历查找与该电脑距离小于d的已维修电脑,将两者Union。查询时只需查看两台电脑是否在同一并查集中即可。
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstdlib>
     5 #include <cstring>
     6 using namespace std;
     7 int n,d;
     8 int x[1005],y[1005],vis[1005];
     9 int fa[1005];
    10 double dis(int xa,int ya,int xb,int yb){
    11     return sqrt((xa-xb)*(xa-xb) + (ya-yb)*(ya-yb));
    12 }
    13 
    14 int getfa(int x){
    15     return ((x == fa[x]) ? x : fa[x] = getfa(fa[x]));
    16 }
    17 
    18 void Union(int x,int y){
    19     int tx = getfa(x);
    20     int ty = getfa(y);
    21     fa[tx] = ty;
    22 }
    23 
    24 int main(){
    25     scanf("%d%d",&n,&d);
    26     for (int i = 1;i <= n;++i){
    27         scanf("%d%d
    ",x+i,y+i);
    28         fa[i] = i;
    29     }
    30     memset(vis,0,sizeof(vis));
    31     char q;
    32     int tx,ty;
    33     while(~scanf("%c %d",&q,&tx)){
    34         if (q == 'O'){
    35             vis[tx] = 1;
    36             for (int i = 1;i <= n;++i){
    37                 if (i == tx || !vis[i]) continue;
    38                 double dist = dis(x[i],y[i],x[tx],y[tx]);
    39                 if (dist < d || fabs(dist - d) < 1e-12)
    40                     Union(i,tx);
    41             }            
    42         }else{
    43             scanf("%d",&ty);            
    44             if (getfa(tx) == getfa(ty)){
    45                 puts("SUCCESS");
    46             }else puts("FAIL");
    47             
    48         }
    49         getchar();
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    java.lang.ExceptionInInitializerError异常分析
    项目中碰到的ExceptionInInitializerError异常
    获取全局上下文(getApplicationContext)_创建Shared Preference工具类_实现自动登录
    IntelliJ Idea 常用快捷键列表
    Qt 创建圆角、无边框、有阴影、可拖动的窗口 good
    去掉 Windows 中控件的虚线框(当当 element == QStyle::PE_FrameFocusRect 时,直接返回,不绘制虚线框)
    Qt 显示 GIF
    C++ 查看预处理后的源文件(查看真实代码)
    数据库访问工具 DBUtl(公孙二狗)
    Tornado
  • 原文地址:https://www.cnblogs.com/mizersy/p/12233316.html
Copyright © 2011-2022 走看看