zoukankan      html  css  js  c++  java
  • POJ 2236 Wireless Network

    人生中的第一个并查集!!
    建树的时候虽然什么优化都没用。。。。 结果1S就过了

    Wireless Network
    Time Limit: 10000MSMemory Limit: 65536K
    Total Submissions: 13429Accepted: 5687

    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 10 10 20 30 4O 1O 2O 4S 1 4O 3S 1 4

    Sample Output

    FAILSUCCESS

    Source

    POJ Monthly,HQM 




    #include <cstdio>
    #include <cstring>

    using namespace std;

    int N,D;
    int djs[1010];

    struct Point
    {
        int x,y;
        int status;
    }P[1010];

    int dist(int a,int b,int d)
    {
        int s=(P[a].x-P.x)*(P[a].x-P.x)+(P[a].y-P.y)*(P[a].y-P.y);
        if(s<=d*d)
            return 1;
        else
            return 0;
    }

    void UF_init()
    {
        memset(djs,-1,sizeof(djs));
    }

    int UF_find(int*tree,int x)
    {
        if(tree[x]<0) return x;
        else
        return UF_find(tree,tree[x]);
    }

    void UF_union(int* tree,int x,int y)
    {
        int rx,ry;
        rx=UF_find(tree,x);
        ry=UF_find(tree,y);

        if(rx==ry) return ;
        else
        {
            tree[ry]=rx;
        }
    }



    int main()
    {
        scanf("%d%d",&N,&D);
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d",&P.x,&P.y);
            P.status=0;
        }

        UF_init();

        char c;
        while(scanf("%c",&c)!=EOF)
        {
            if(c=='O')
            {
                int a;
                scanf("%d",&a);
                P[a].status=1;
                for(int i=1;i<=N;i++)
                if(i!=a&&P.status&&dist(a,i,D))
                {
                    UF_union(djs,i,a);
                }

            }
            else if(c=='S')
            {
                int a,b;
                scanf("%d%d",&a,&b);
                int ra=UF_find(djs,a);
                int rb=UF_find(djs,b);

                puts(ra==rb?"SUCCESS":"FAIL");
            }
        }

        return 0;
    }
     
  • 相关阅读:
    7个最好的免费杀毒软件下载
    VMware虚拟机扩容
    tomcat的JK和JK2
    面向对象——接口
    JPA入门样例(採用JPA的hibernate实现版本号)
    JAVA数组的定义及用法
    Styles and Themes
    OpenSSL再曝CCS注入漏洞-心伤未愈又成筛子
    纯文本抽出程序库DMC TEXT FILTER
    数据结构课程设计之通讯录管理系统
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351078.html
Copyright © 2011-2022 走看看