zoukankan      html  css  js  c++  java
  • B

    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

    题意:由于某些原因所有电脑出现故障,现在进行修复,修复后在距离D范围内的电脑可以通信,现给出电脑
    的坐标,然后给出修复顺序
    询问两台电脑之间能否进行通讯。
    思路:并查集,根据修复,若距离在D内则进行合并操作。对于询问,只需要判断是否在一个集合里即可。
    
    AC:代码

      //这个题的repair数组还是很可以的,可以作为一种做题的小技巧      

    #include<iostream>    
    #include<cstdio>  
    #include<cmath> 
    #include<cstring>  
    #include<algorithm>  
    typedef long long LL;  
    using namespace std;  
    struct node  
    {  
        int x,y;  
    }comp[1010];  
      
    int N,D;  
    int repair[1010],num;  
    int par[1010],rank[1010];  
      
    void init()  
    {  
        for(int i=0;i<=N;i++)  
        {  
            par[i]=i;  
            rank[i]=0;  
        }  
        memset(repair,-1,sizeof(repair));  
        num=0;  
    }  
      
    int find1(int x)  
    {  
        if(par[x]==x)  
            return x;  
        else return par[x]=find1(par[x]);  
    }  
    void unite(int a,int b)  
    {  
        int x=find1(a);  
        int y=find1(b);  
        if(x==y)  
            return;  
        if(rank[x]<rank[y])  
            par[x]=y;  
        else  
        {  
            par[y]=x;  
            if(rank[x]==rank[y])  
                rank[x]++;  
        }  
    }  
    bool dis(int i,int j)  
    {  
        double dist=(comp[i].x-comp[j].x)*(comp[i].x-comp[j].x)+(comp[i].y-comp[j].y)*(comp[i].y-comp[j].y);  
        dist=sqrt(dist);  
        return dist<=D;  
    }  
    void is_link(int x)  
    {  
        for(int i=0;i<num;i++)  
        {  
            if(dis(repair[i],x))  
            {  
                unite(repair[i],x);  
                //return;  
            }  
        }  
    }  
    int main()  
    {  
        //freopen("in.txt","r",stdin);  
        char t;  
        int a,b;  
        scanf("%d%d",&N,&D);  
        for(int i=1;i<=N;i++)  
            scanf("%d%d",&comp[i].x,&comp[i].y);  
        init();  
        getchar();  
        while(scanf("%c",&t)!=EOF)  
        {  
            getchar();  
            if(t=='O')  
            {  
                scanf("%d",&a);  
                repair[num]=a;  
                is_link(a);  
                num++;  
            }  
            else  
            {  
                scanf("%d%d",&a,&b);  
                if(find1(a)==find1(b))  
                    cout<<"SUCCESS"<<endl;  
                else cout<<"FAIL"<<endl;  
            }  
        }  
        return 0;  
    }  


  • 相关阅读:
    C语言中的内存分配与释放
    CentOS下Mysql安装教程
    简单的理解deflate算法
    Memcache的一些学习
    Python学习入门基础教程(learning Python)--6.4 Python的list与函数
    Delphi Windows API判断文件共享锁定状态(使用OpenFile来判断)
    深刻:截获windows的消息并分析实例(DefWindowProc),以WM_NCHITTEST举例(Windows下每一个鼠标消息都是由 WM_NCHITTEST 消息产生的,这个消息的参数包含了鼠标位置的信息)
    WM_SYSCOMMAND消息命令整理 good
    Delphi 中 FindWindow 和 FindWindowEx 找到外部进程,然后发送消息(比如最大化)
    再来一个学历,理论与动手能力的讨论——结论是理论和实际都重要,但是上学期间应偏重理论
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746036.html
Copyright © 2011-2022 走看看