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

    Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 18066   Accepted: 7618

    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



    今天学了并查集,并查集的模板题。
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    #include <cstring>
    #include <cctype>
    #include <cstdlib>
    #include <cmath>
    #include <ctime>
    using    namespace    std;
    
    const    int    SIZE = 1005;
    int        FATHER[SIZE],RANK[SIZE];
    int        N,D;
    pair<int,int>    G[SIZE];
    vector<int>    OK;
    double        DIS[SIZE][SIZE];
    
    void    ini(int);
    int    find_father(int);
    void    unite(int,int);
    bool    same(int,int);
    double    dis(pair<int,int>,pair<int,int>);
    int    main(void)
    {
        int    x,y;
        char    ch;
    
        while(scanf("%d%d",&N,&D) != EOF)
        {
            ini(N);
            for(int i = 1;i <= N;i ++)
                scanf("%d%d",&G[i].first,&G[i].second);
            for(int i = 1;i <= N;i ++)
                for(int j = 1;j <= N;j ++)
                    DIS[i][j] = dis(G[i],G[j]);
    
            while(scanf(" %c",&ch) != EOF)
                if(ch == 'O')
                {
                    scanf("%d",&x);
                    for(int i = 0;i < OK.size();i ++)
                        if(DIS[x][OK[i]] <= D)
                            unite(x,OK[i]);
                    OK.push_back(x);
                }
                else
                {
                    scanf("%d%d",&x,&y);
                    printf("%s
    ",same(x,y) ? "SUCCESS" : "FAIL");
                }
        }
    
        return    0;
    }
    
    void    ini(int n)
    {
        for(int i = 1;i <= n;i ++)
        {
            FATHER[i] = i;
            RANK[i] = 0;
        }
    }
    
    int    find_father(int n)
    {
        if(FATHER[n] == n)
            return    n;
        return    FATHER[n] = find_father(FATHER[n]);
    }
    
    void    unite(int x,int y)
    {
        x = find_father(x);
        y = find_father(y);
    
        if(x == y)
            return    ;
        if(RANK[x] < RANK[y])
            FATHER[x] = y;
        else
        {
            FATHER[y] = x;
            if(RANK[x] == RANK[y])
                RANK[y] ++;
        }
    }
    
    bool    same(int x,int y)
    {
        return    find_father(x) == find_father(y);
    }
    
    double    dis(pair<int,int> a,pair<int,int> b)
    {
        return    sqrt(pow(a.first - b.first,2) + pow(a.second - b.second,2));
    }
    View Code
  • 相关阅读:
    WinAPI: ExtTextOut 扩展的文本输出
    WinAPI: SetTextCharacterExtra 设置字符间距
    WinAPI: DrawTextEx 多功能文本绘制
    WinAPI: sndPlaySound 播放 wav 文件
    WinAPI: SetTextColor 设置设备环境的文本颜色
    WinAPI: DrawText 将文本绘制到指定的矩形中
    WinAPI: GetTextColor 获取设备环境的文本颜色
    WinAPI: GetTextAlign 获取绘图环境的文本对齐方式
    ulimit限制之nproc问题
    基于EPOLL写的HTTP服务器(加入了线程池)
  • 原文地址:https://www.cnblogs.com/xz816111/p/4529031.html
Copyright © 2011-2022 走看看