zoukankan      html  css  js  c++  java
  • poj--2236

    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

    题目大意:无线网络:ACM团队只修电脑不过夜!有N台电脑坏了,它们原本组成这样一个局域网:距离d内的两台电脑互联。现在通过修理和查看操作,
         求查看时两台电脑是否互联?

    好久没有写并查集问题了,这题就当是复习一遍吧,这题需要判断一下电脑之间是否满足他们的连接距离,如果满足才能加入到并查集中,否则不能加入并查集中

    代码:
    #include<iostream>
    using namespace std;
    const int N=1002+16;
    int pre[N];
    int xi[N],yi[N];
    bool status[N];
    int rank[N];
    int n,d;
    pair<int,int> Computer[N];
    int distance[N][N];
    void init()
    {
        for(int i=1;i<=n;i++)
        {
            pre[i]=i;
            rank[i]=0;
        }
    }
    int find(int x)
    {
        if(x==pre[x])
            return x;
        else
            return pre[x]=find(pre[x]);
    }
    void unit(int x,int y)
    {
        int tx=find(x);
        int ty=find(y);
        if(ty==tx)    return ;
        if(rank[tx]<rank[ty])
        {
            pre[tx]=ty;
        }
        else
        {
            pre[ty]=tx;
            if(rank[tx]==rank[ty])
            {
                rank[tx]++;
            }
        }
    }
    int square(int x)
    {
        return x*x;
    }
    bool check(int x,int y)
    {
        if(square(Computer[x].first-Computer[y].first)+square(Computer[x].second-Computer[y].second)>square(d))
            return    false;
        return true; 
    }
    bool same(int x,int y)
    {
        return find(x)==find(y);
    }
    int main()
    {
        while(scanf("%d%d",&n,&d)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                cin>>Computer[i].first>>Computer[i].second;
            }
            init();
            char ch;
            int p,q;
            while(cin>>ch)
            {
                if(ch=='O')
                {
                    cin>>p;
                    status[p]=true;
                    for(int i=1;i<=N;i++)
                    {
                        if(p==i)
                            continue;
                        if(status[i]&&check(p,i))
                        {
                            unit(p,i);
                        }
                    }
                }
                else
                {
                    cin>>p>>q;
                    if(same(p,q))
                    cout<<"SUCCESS"<<endl;
                    else
                    cout<<"FAIL"<<endl;
                }
            }
        }
        return 0;
    }


  • 相关阅读:
    Linux 系统监控和诊断工具:lsof
    C语言基础(21)-C语言编译过程及GCC参数简介
    VS2013-解决VS2013 4996错误
    C语言基础(20)-文件操作(fopen,getc,fclose)
    eclipse-Java compiler level does not match the version of the installed Java project facet.
    C语言基础(19)-结构体,联合体,枚举和typedef
    android studio- Gradle "xxx" project refresh failed
    C语言基础(18)-内存
    C语言基础(17)-作用域
    android.app.Service-android:process=":remote"属性解说
  • 原文地址:https://www.cnblogs.com/acmblog/p/9580266.html
Copyright © 2011-2022 走看看