• Wireless Network(POJ 2236)


    Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 20724   Accepted: 8711

    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
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <set>
     6 #include <cmath>
     7 using namespace std;
     8 #define Max 1005
     9 int n,d;
    10 int per[Max];
    11 bool vis[Max];
    12 struct point
    13 {
    14     int x,y;    
    15 }e[1002];
    16 void init()
    17 {
    18     for(int i=0;i<=n;i++)
    19         per[i]=i;
    20     return;
    21 }
    22 int find(int x)
    23 {
    24     if(x==per[x])
    25         return x;
    26     int tem,root=x,t=x;
    27     while(root!=per[root])    root=per[root];
    28     while(t!=per[t])
    29     {
    30         tem=per[t];
    31         per[t]=root;
    32         t=tem;
    33     }
    34 }
    35 bool dis(int q,int p)
    36 {
    37     point a=e[q],b=e[p];
    38     int f=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    39     return d*d>=f;
    40 }
    41 int unite(int a,int b)
    42 {
    43     a=find(a);
    44     b=find(b);
    45     if(a!=b)
    46         per[a]=b;
    47     return 0;
    48 }
    49 int main()
    50 {
    51     int i,j;
    52     int v,u,p;
    53     char ch;
    54     freopen("in.txt","r",stdin);
    55     scanf("%d%d",&n,&d);
    56     init();
    57     memset(vis,0,sizeof(vis));
    58     for(i=1;i<=n;i++)
    59         scanf("%d%d",&e[i].x,&e[i].y);
    60     getchar();
    61     while(scanf("%c",&ch)!=EOF)
    62     {
    63         if(ch=='S')
    64         {
    65             scanf("%d%d",&u,&v);
    66             if(find(u)==find(v))
    67                 printf("SUCCESS
    ");
    68             else
    69                 printf("FAIL
    ");
    70         }
    71         else if(ch=='O')
    72         {
    73             scanf("%d",&u);
    74             vis[u]=1;
    75             for(i=1;i<=n;i++)
    76                 if(vis[i]&&i!=u&&dis(i,u))
    77                     unite(u,i);
    78         }
    79         getchar();
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    oracle连接数
    python——包
    python——软件开发目录规范
    python——模块介绍
    python——二分法
    python——函数的递归调用
    python08——for 循环
    while循环嵌套练习题
    python07——while循环
    python06入门——流程控制之if判断
  • 原文地址:https://www.cnblogs.com/a1225234/p/5181208.html
Copyright © 2020-2023  润新知