zoukankan      html  css  js  c++  java
  • POJ2236

    https://vjudge.net/problem/POJ-2236

    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<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define maxn 50010
     7 using namespace std;
     8 int n;
     9 double d;
    10 double dx[1005],dy[1005];
    11 int pre[1005],repair[1005];
    12 double dis(int a,int b)
    13 {
    14     return sqrt((dx[a]-dx[b])*(dx[a]-dx[b])+(dy[a]-dy[b])*(dy[a]-dy[b]));
    15 }
    16 int Find(int x)
    17 {
    18     if(x!=pre[x])
    19         return pre[x]=Find(pre[x]);
    20 }
    21 int Union(int x,int y)
    22 {
    23     pre[Find(x)]=Find(y);
    24 }
    25 int main()
    26 {
    27     cin>>n>>d;
    28     for(int i=1;i<=n;i++)
    29     {
    30         cin>>dx[i]>>dy[i];
    31         pre[i]=i;
    32     }
    33     char k;
    34     int p,q,len=0;
    35     getchar();
    36     while(~scanf("%c",&k))
    37     {
    38         if(k=='O')
    39             {
    40                 cin>>p;
    41                 repair[len++]=p;
    42                 for(int i=0;i<len-1;i++)
    43                     if(dis(p,repair[i])<=d)
    44                        Union(repair[i],p);
    45             }
    46             else
    47             {
    48                 cin>>p>>q;
    49                 if(Find(p)==Find(q))
    50                     cout<<"SUCCESS"<<endl;
    51                 else
    52                     cout<<"FAIL"<<endl;
    53             }
    54             getchar();
    55     }
    56     return 0;
    57 }

    版本二:

    使用结构体

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define maxn 1005
     7 using namespace std;
     8 int n;
     9 double d;
    10 struct node
    11 {
    12     double x,y;
    13     int pre;//根节点
    14     int ranks;//树的高度
    15     bool repair;//是否修复
    16 }link[maxn];
    17 int Find(int x)
    18 {
    19     if(x!=link[x].pre)
    20     return link[x].pre=Find(link[x].pre);
    21 }
    22 double dis(int i,int j)
    23 {
    24     return sqrt((link[i].x-link[j].x)*(link[i].x-link[j].x)+(link[i].y-link[j].y)*(link[i].y-link[j].y));
    25 }
    26 void Union(int i,int j)
    27 {
    28     //if(link[i].repair&&link[j].repair)
    29        // return 0;
    30     int root1=Find(i),root2=Find(j);
    31     //if(root1==root2)return 0;
    32     if(link[root1].ranks>link[root2].ranks)
    33     {
    34         link[root2].pre=root1;
    35         link[root1].ranks+=link[root2].ranks;
    36     }
    37     else
    38     {
    39         link[root1].pre=root2;
    40         link[root2].ranks+=link[root1].ranks;
    41     }
    42     //return 1;
    43 }
    44 
    45 
    46 int main()
    47 {
    48     cin>>n>>d;
    49     for(int i=1;i<=n;i++)
    50     {
    51         cin>>link[i].x>>link[i].y;
    52         link[i].ranks=1;
    53         link[i].pre=i;
    54         link[i].repair=0;
    55     }
    56     char k;
    57     int p,q;
    58     getchar();
    59     while(~scanf("%c",&k))
    60     {
    61         if(k=='O')
    62             {
    63                 cin>>p;
    64                 link[p].repair=1;
    65                 for(int i=1;i<=n;i++)
    66                 {
    67                     if(Find(i)!=Find(p)&&link[i].repair&&dis(i,p)<=d)
    68                         Union(p,i);
    69                 }
    70             }
    71             else
    72             {
    73                 cin>>p>>q;
    74                 if(Find(p)==Find(q))
    75                     cout<<"SUCCESS"<<endl;
    76                 else
    77                     cout<<"FAIL"<<endl;
    78             }
    79             getchar();
    80     }
    81     return 0;
    82 }

    题目大意:

    n个站点,已知站点的坐标。

    ‘O'操作 每次修通一个站点,该站点与距离该站点小于d且也为修通的站点联通;

    ’S'操作 查询两个站点是否联通。

    思路:

    并查集的题。设置距离函数,如果满足距离条件则将两个点合并。如果查询到的两点根节点相同则能联通。

  • 相关阅读:
    UESTC 1061 秋实大哥与战争 线段树区间合并
    bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
    bzoj 1008: [HNOI2008]越狱 数学
    bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 优先队列+dij
    LightOJ 1138 二分
    AIM Tech Round 3 (Div. 2) A , B , C
    Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
    hdu 4542 小明系列故事——未知剩余系 反素数 + 打表
    Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数
    51nod 1060 最复杂的数 反素数
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/10763863.html
Copyright © 2011-2022 走看看