• POJ--2236 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
    

    Sample Output

    FAIL
    SUCCESS

     

         题意:有n台电脑,初始全部损坏。输入O:x,修理x号电脑。S:x,y询问x,y是否可以联系。两个电脑可以联系必须保证距离<=d,或者通过与它相连通的电脑来建立联系,但是这个距离是保证。

         解析:修理一台电脑,就遍历其他电脑,如果满足距离<=d而且另一台电脑已被修理,就把它们放入同一集合。询问时查询两者父亲节点,一样就可以。注意FAIL,别输成FALL。。。

    而且我加入了路径压缩和dis[][]后时间并没有多大改观,也就优化了几十秒。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=1111;
    bool vis[maxn];
    int pr[maxn];
    ll n,d;
    struct node
    {
        int x,y;
    }st[maxn];
    void first()
    {
        memset(vis,false,sizeof(vis));
    }
    int find(int x)
    {
        if(x!=pr[x])
            pr[x]=find(pr[x]);
            return pr[x];
    }
    void join(int x,int y)
    {
        int f1=find(x);
        int f2=find(y);
        if(f1!=f2)
        {
            pr[f1]=f2;
        }
        return ;
    }
    int main()
    {
        scanf("%lld%lld",&n,&d);    
        first();
        d=d*d;
        for(int i=1;i<=n;i++)
            {
                scanf("%d%d",&st[i].x,&st[i].y);
                pr[i]=i;
            }
        char ch;
        while(scanf("%c",&ch)!=EOF)
        {
            
            if(ch=='
    ')
                continue;
            if(ch=='O')
            {
                int id;
                scanf("%d",&id);
                vis[id]=true;
                for(int i=1;i<=n;i++)
                {
                    if(i!=id)
                    {
                        ll mid=(st[i].x-st[id].x)*(st[i].x-st[id].x)+(st[i].y-st[id].y)*(st[i].y-st[id].y);
                        if(mid<=d&&vis[i]==true)
                        {
                            join(id,i);
                        }
                    }
                }
            }
            else
            {
                int id1,id2;
                scanf("%d%d",&id1,&id2);
                int f1=find(id1);
                int f2=find(id2);
                if(f1==f2)
                    cout<<"SUCCESS"<<endl;
                else
                    cout<<"FAIL"<<endl;
            }
        }
    }
  • 相关阅读:
    解决Driver/library version mismatch
    ubuntu 16.04 nfs服务的搭建
    samba on ubuntu
    SSH连接下复制远程linux服务器文件到本地的命令(zz)
    R 语言入门(Ubuntu)
    new repository
    vlc play函数跟踪
    SSH连接下复制远程linux服务器文件到本地的命令(zz)
    c# HttpServer 的使用
    异步Udp监听关闭 出现异常,访问已释放的资源或者其他错误的解决方法
  • 原文地址:https://www.cnblogs.com/liyexin/p/12593489.html
Copyright © 2020-2023  润新知