• POJ 2236 Wireless Network 【并查集】


    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 24091   Accepted: 10022

    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
    

    Source

    POJ Monthly,HQM

    题意:有N台坏掉的笔记本电脑,只有修好的并且距离相距不超过d的两台笔记本可以连接,给你每台笔记本的位置和两种操作:

    1.“O x”表示修好第x台电脑。

    2.“S x y”测试第x台电脑与第y台电脑之间是否联通,是打印“SUCCESS”,否“FAIL”。

    思路:并查集,每次解放一个点,并检查是否可以加入集合。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    //typedef long long Long;
    //typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-6;
    const double Pi = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 1e4 + 5;
    int par[MAXN], len[MAXN], vis[MAXN];
    struct node{
        double x, y;
    } loc[MAXN];
    void init(int x) {
        for (int i = 0; i <= x; i++) {
            par[i] = i; len[i] = 0; vis[i] = 0;
        }
    }
    int find_root(int x) {
        if (x == par[x]) return x;
        return par[x] = find_root(par[x]);
    }
    void unite(int x, int y) {
        int fx = find_root(x);
        int fy = find_root(y);
        if (fx == fy) return;
        if (len[fx] < len[fy]) {
            par[fx] = fy;
        }
        else {
            par[fy] = fx;
            if (len[fx] == len[fy]) len[fx]++;
        }
    }
    double dis(node a, node b) {
        return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
    }
    double d;
    int n, a, b;
    char s[2];
    int main() {
        scanf("%d%lf", &n, &d);
        init(n); int cnt = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%lf %lf", &loc[i].x, &loc[i].y);
        }
        while (scanf("%s", s) != EOF) {
            if (s[0] == 'O') {
                scanf("%d", &a);
                for (int i = 0; i < cnt; i++) {
                    if (dis(loc[vis[i]], loc[a]) < ESP + d) {
                        unite(a, vis[i]);
                    }
                }
                vis[cnt++] = a;
            }
            else {
                scanf("%d%d", &a, &b);
                int tx = find_root(a);
                int ty = find_root(b);
                if (tx == ty) printf("SUCCESS
    ");
                else printf("FAIL
    ");
            }
        }
        return 0;
    }
    
    

     

  • 相关阅读:
    C#判断网络链接状态
    C# 创建临时文件(转帖)
    C# 很久以前几个常用类
    正则附表
    如何判断WebBrowser浏览器网页加载完成
    控件阴影
    C# 使用WM_COPYDATA传输数据(两个窗体间通信)
    C# 调用POST请求
    改变无边框窗体的尺寸大小和移动无边框窗体
    IT学习网站
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770800.html
Copyright © 2020-2023  润新知