• 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;  
    }  


  • 相关阅读:
    cmake使用
    CMake...
    信息熵相关知识总结
    最强NLP模型-BERT
    问答系统总结
    检索问答模型
    文本分类-TextCNN
    机器学习-Logistic回归
    Attention注意力机制介绍
    机器学习-聚类Clustering
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746036.html
Copyright © 2020-2023  润新知