Wireless NetWork

题目: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.

import java.util.Scanner;

public class Wireless1 {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
          int n=in.nextInt();
          int d=in.nextInt();
          d*=d;
          int[] arrx=new int[n+1];
          int[] arry=new int[n+1];
          int[] arr=new int[n+1];
          int[] bool=new int[n+1];
          for(int i=1;i<=n;i++){
            arr[i]=i;
          }
          for(int i=1;i<=n;i++){
            arrx[i]=in.nextInt();
            arry[i]=in.nextInt();
          }
          while(in.hasNext())
          {
           if(in.next().equals("O")){
            int w=in.nextInt();
            if(bool[w]==1) continue;
            for(int j=1;j<=n;j++){
               if(bool[j]==0) continue;
               int u=j;
               while(arr[u]!=u)
                u=arr[u];
               int l=w;
               while(arr[l]!=l)
                l=arr[l];
               if(l==u) continue;
               int x=arrx[w]-arrx[j];
               int y=arry[w]-arry[j];
               if(x*x+y*y<=d)
                {
                if(u>l) arr[j]=arr[w]=arr[u]=l;
                else arr[j]=arr[w]=arr[l]=u;
                }
              }
            bool[w]=1;
             }
             else
             {
            int r=in.nextInt(),w=r;
            while(arr[w]!=w)
               w=arr[w];
            int t=in.nextInt(),u=t;
            while(arr[u]!=u)
               u=arr[u];
            arr[r]=w;
            arr[t]=u;
            if(arr[w]==arr[u]&&bool[w]==1) System.out.println("SUCCESS");
            else System.out.println("FAIL");
              }
           }
        // TODO Auto-generated method stub

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值