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 }
  • 相关阅读:
    selinux 设置的彻底理解 并要 熟练经常的使用
    关于linux下自定义的 alias文件和自定义函数库的通用写法(只适合自己的)
    linux下关于mysql的命令的用法
    彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server
    php的内核组成模块和运行原理
    彻底了解 suid, sgid ,sticky权限
    php编程疑难解决-1
    再次安装fedora23的一些遗留问题的解决
    word如何替换行首?
    php高级开发参考地址
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5595079.html
Copyright © 2011-2022 走看看