zoukankan      html  css  js  c++  java
  • poj 2236 Wireless Network

                                                                                                      Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 24176   Accepted: 10058

    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

    题意:共有N台坏电脑,每台电脑放置的位置都是一个坐标,且每台电脑被修好后能‘直接’的联系到半径为d范围内的电脑(两台电脑都要被修理好才能相互联系)。修理员每次都会进行操作,操作'O'代表修理电脑,操作'S'代表测试两台电脑之间能否相互联系。
    思路:并查集,对于某一台刚修理好的电脑,在d范围内寻找修好的电脑并将这些电脑和刚修好的电脑放在一个集合中即可。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N_MAX = 1002;
    pair<int, int>cordinate[N_MAX];//表示每台计算机坐标
    int par[N_MAX];
    int Rank[N_MAX];
    bool is_fixed[N_MAX];
    bool is_within_area[N_MAX][N_MAX];
    void init(const int&n) {
        for (int i = 0;i < n;i++) {
            par[i] = i;
            Rank[i] = 0;
        }
    }
    
    int find(const int&x) {
        if (par[x] == x) {
            return x;
        }
        else return par[x] = find(par[x]);
    }
    
    bool same(const int&x, const int&y) {
        return find(x) == find(y);
    }
    
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)return;
        if (Rank[x] < Rank[y]) {
            par[x] = y;
        }
        else {
            par[y] = x;
            if (Rank[x] == Rank[y])Rank[x]++;
        }
    }
    
    int square(const int&x) {
        return x*x;
    }
    
    int main() {
        int N, d;
    
        scanf("%d%d", &N, &d);
        for (int i = 0;i < N;i++)
            scanf("%d%d", &cordinate[i].first, &cordinate[i].second);//每台计算机的坐标
        init(N);//初始化N台电脑
        for (int i = 0;i < N;i++) {//对于每一台电脑i而言,is_within_area[i][j]代表电脑j是否在i的范围内
            for (int j = i;j < N;j++) {
                if ((square(cordinate[j].first - cordinate[i].first) + square(cordinate[j].second - cordinate[i].second)) <= square(d)) {
                    is_within_area[i][j] = true;
                    is_within_area[j][i] = true;
                }
            }
        }
    
        char operation;
        int com_1, com_2;
        while (scanf("%c", &operation) != EOF) {
            if (operation == 'O') {
                scanf("%d", &com_1);
                com_1--;
                is_fixed[com_1] = true;
                for (int i = 0;i < N;i++) {//com_1已经修好,搜索在com_1的d范围内有多少台电脑可以与之连通
                    if (i == com_1)continue;
                    if (is_fixed[i] && is_within_area[com_1][i])
                        unite(com_1, i);
                }
            }
            else if (operation == 'S') {
                scanf("%d%d", &com_1, &com_2);
                com_1--;com_2--;
                if (same(com_1, com_2))
                    printf("SUCCESS
    ");
                else printf("FAIL
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    IOS
    XCode
    Android Studio
    Android Studio
    Cordova
    Delphi
    Cordova
    Delphi
    JQuery Mobile
    twitter ads_campaign management(图示)
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/5903183.html
Copyright © 2011-2022 走看看