• 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;
            }
        }
    }
  • 相关阅读:
    word20170108逛景点 Sightseeing有用的词和句子
    c# List 分页问题
    VUE界面,this.form.xxx=1赋值后,界面效果没有变化
    SQL Server使用索引视图来实现“物化视图”
    .NET CORE 实现异步处理
    当请求接口提示网络错误Error:Network Error的时候
    SheetJS js-xlsx :js实现导出Excel模板
    增加索引分析
    聚集索引与非聚集索引的总结
    Dynamics CRM-无法识别安全令牌的颁发者,若要接受来自此颁发者的安全令牌,请将 IssuerNameRegistry 配置为返回此颁发者的有效名称
  • 原文地址:https://www.cnblogs.com/liyexin/p/12593489.html
Copyright © 2020-2023  润新知