zoukankan      html  css  js  c++  java
  • 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 } 
  • 相关阅读:
    [转]rdlc报表中表达式的使用--switch和IIF范例
    [转]关于ASP.NET(C#)程序中TEXTBOX下动态DIV跟随[AJAX应用]
    CodeForces
    NYOJ306 走迷宫(dfs+二分搜索)
    全心全意为人民服务体如今我们软件设计上
    2014年麦克阿瑟基金奖,张益唐入围(62万美金用于个人支配)
    Android中SharedPreferences函数具体解释
    drupal7中CKEditor开启上传图片功能
    JBoss+Ant实现EJB无状态会话bean实例
    c#读取xml文件配置文件Winform及WebForm-Demo具体解释
  • 原文地址:https://www.cnblogs.com/daremo/p/14122500.html
Copyright © 2011-2022 走看看