zoukankan      html  css  js  c++  java
  • poj2236(Wireless Network)并查集

    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

    题意:给你n个坏的电脑的坐标,如果两个修好的电脑距离不大于d,他们之间连通。两台电脑可以通过第三台电脑间接连通。给出若干次询问,一种是修哪台电脑,一种是询问两台电脑是否连通。


    题解:典型的并查集,合并时我也没有加rank数组优化,可以自己加一下,不过10s时间绰绰有余了。


    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    const int maxn = 1010;
    
    int f[maxn];
    int px[maxn], py[maxn];
    int vis[maxn];
    int n, d;
    
    int Find(int x)
    {
        return x == f[x] ? x : (f[x] = Find(f[x]));
    }
    
    void join(int x, int y)
    {
        int fx = Find(x), fy = Find(y);
        f[fx] = fy;
    }
    
    int dis(int x, int y)
    {
        return (px[x]-px[y])*(px[x]-px[y])+(py[x]-py[y])*(py[x]-py[y]);
    }
    
    int main()
    {
        cin >> n >> d;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d", &px[i], &py[i]);
            f[i] = i;
        }
        memset(vis, 0, sizeof(vis));
        char op[5];
        while (~scanf("%s", op))
            if (op[0] == 'O')
            {
                int p;
                scanf("%d", &p);
                vis[p] = 1;
                for (int i = 1; i <= n; ++i)
                    if (i != p && vis[i] && dis(i, p) <= d*d)
                        join(i, p);
            }
            else
            {
                int x, y;
                scanf("%d%d", &x, &y);
                int fx = Find(x), fy = Find(y);
                if (fx == fy)
                    printf("SUCCESS
    ");
                else
                    printf("FAIL
    ");
            }
        return 0;
    }
    


  • 相关阅读:
    不得不说微信之怪现状
    新手运营如何做广告位投放
    网络营销人员如何培养创新技能
    GoDaddy开通中文客服电话,沟通不再是问题
    为zblog FCKeditor编辑器添加设置字体格式h1 h2功能
    真实手机访问本地电脑网站失败的原因
    数据库简总
    简单总结
    GUI图形用户界面学习
    集合框架
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203955.html
Copyright © 2011-2022 走看看