zoukankan      html  css  js  c++  java
  • POJ-2236 Wireless Network( 并查集 )

    题目链接:http://poj.org/problem?id=2236

    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
    
    题目大意:已知电脑互相通信的距离的,且电脑可以间接通信。现有若干台坏电脑,以及若干操作,这些操作分为修电脑以及检查两台电脑能否通信,对于检查操作,如果可以,则输出“SUCCESS”,否则输出“FAIL”
    解题思路:用并查集来处理可以可以互相通信的电脑,考虑到修好的电脑可能间接连接两个集合,因此更新并查集时以修好的电脑为父亲

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<iomanip>
     6 #include<map>
     7 
     8 using namespace std;
     9 
    10 int n, d, coor[1005][2], father[1005];
    11 bool flag[1005];
    12 
    13 bool com( int a, int b ){
    14     int dx = coor[a][0] - coor[b][0];
    15     int dy = coor[a][1] - coor[b][1];
    16     if( dx * dx + dy * dy <= d * d ) return true;
    17     else return false;
    18 }
    19 
    20 int findfather( int x ){
    21     while( x != father[x] ) x = father[x];
    22     return x;
    23 }
    24 
    25 int main(){
    26     ios::sync_with_stdio( false );
    27 
    28     cin >> n >> d;
    29     for( int i = 1; i <= n; i++ )
    30         cin >> coor[i][0] >> coor[i][1];
    31     memset( flag, false, sizeof( flag ) );
    32     char order;
    33     int pa, pb;
    34     while( cin >> order ){
    35         if( order == 'O' ){
    36             cin >> pa;
    37             flag[pa] = true;
    38             father[pa] = pa;
    39             for( int i = 1; i <= n; i++ ){
    40                 if( flag[i] && com( i, pa ) ){
    41                     father[findfather( i )] = pa;
    42                 }
    43             }
    44         }
    45         else{
    46             cin >> pa >> pb;
    47             if( flag[pa] && flag[pb] && findfather( pa ) == findfather( pb ) )
    48                 cout << "SUCCESS" << endl;
    49             else cout << "FAIL" << endl;
    50         }
    51     }
    52 
    53     return 0;
    54 }
  • 相关阅读:
    lists 函数整理
    orddict 练习
    github 的使用
    wxListCtrl 例子 二
    Erlang eunit
    Erlang 中 Tuple 使用 以及 List 模块意外
    Erlang Json
    模块和包
    Mysql作为zabbix数据库ibdata1文件太高解决
    用户管理和数据库安全
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5595079.html
Copyright © 2011-2022 走看看