• POJ2236 Wireless Network(并查集)


    Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 55834   Accepted: 22702

    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


    (用了getchar每次交poj的题都要CE一次啊啊啊啊啊啊好烦)

    基础并查集,交上去发现跑了8.5s..总感觉哪里还能优化

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdio>
     6 #include <vector>
     7 using namespace std;
     8 #define MAXN 10003
     9 #define INF 0x3f3f3f3f
    10 
    11 typedef struct status{
    12     int x;
    13     int y;
    14 }p;
    15 p loc[1003];
    16 int repaired[1003];
    17 bool vis[1003];
    18 int fa[1003];
    19 
    20 int dis(p p1, p p2){
    21     return (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
    22 }
    23 
    24 int find(int x){
    25     
    26     return x == fa[x] ? x : (fa[x] = find(fa[x]));
    27 } 
    28 void join(int x, int y){
    29     
    30     x = find(x);
    31     y = find(y);
    32     
    33     if(x != y)
    34         fa[x] = y;
    35 } 
    36 int main(){
    37     int N, d, idx = 1, x, y;
    38     char ch;
    39     bool flag = false;
    40     cin >> N >> d;
    41     
    42     for(int i = 1; i<= N; ++ i){
    43         cin >> loc[i].x >> loc[i].y;
    44         fa[i] = i;
    45     }
    46     getchar();
    47     while(cin >> ch){
    48         if(ch == 'O'){
    49             cin >> x;
    50                 
    51             for(int i = 1; i < idx; ++ i){
    52                 if(dis(loc[x], loc[repaired[i]]) <= d * d){
    53                     join(repaired[i], x);
    54                 }
    55             }
    56             repaired[idx ++] = x;
    57             vis[x] = true;
    58         }
    59         else{
    60             cin >> x >> y;
    61             if(vis[x] && vis[y])
    62                 flag = true;
    63             x = find(x), y = find(y);
    64             cout << ( flag && x == y ? "SUCCESS" : "FAIL") << endl;
    65         }
    66         getchar();
    67     }
    68     
    69     
    70     return 0;
    71 } 
  • 相关阅读:
    CLR执行模式之程序集代码的执行
    CLR执行模式之托管代码程序集浅析
    第十章 使用变量的一般事项
    第九章伪代码编程过程 The PseudoCode Programming Process
    第八章防御式编程(代码大全读后)
    第七章实战高质量的子程序(代码大全第七章读后)
    一时看不明白大神的想法
    debain9 debian8 alpine 3.7 aliyun
    elk7
    ansible
  • 原文地址:https://www.cnblogs.com/daremo/p/14122500.html
Copyright © 2020-2023  润新知