• poj2236_并查集_Wireless Network


    Wireless Network
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 24497   Accepted: 10213

    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
    

    Source

     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 #define MAX_N 1000+5
     5 
     6 using namespace std;
     7 
     8 struct point{int x;int y;int jud;};
     9 point p[1005];
    10 int par[MAX_N];//父节点
    11 int depth[MAX_N];//深度
    12 
    13 void init(int n){
    14    for(int i=0;i<=n;i++){
    15        par[i]=i;
    16        depth[i]=1;
    17    }
    18 }
    19 int find_father(int t){
    20    if(t==par[t]){
    21        return t;
    22    }else{
    23        return par[t]=find_father(par[t]);
    24        //实现了路径压缩
    25    }
    26 }
    27 void unite(int t1,int t2){
    28    int f1=find_father(t1);
    29    int f2=find_father(t2);
    30    if(f1==f2){
    31        return ;
    32    }
    33    if(depth[f1]<depth[f2]){
    34        par[f1]=f2;
    35    }else{
    36        par[f2]=f1;
    37        if(depth[f1]==depth[f2]){
    38            depth[f1]++;
    39            //记录深度
    40        }
    41    }
    42 }
    43 
    44 bool same(int x,int y){
    45     return find_father(x)==find_father(y);
    46 }
    47 
    48 int distanc_2(point a,point b){
    49     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    50 }
    51 
    52 
    53 int main()
    54 {
    55     int n,d;
    56     char c;
    57     int t1,t2;
    58     scanf("%d %d",&n,&d);
    59     init(n);
    60     for(int i=1;i<=n;i++){
    61         scanf("%d %d",&p[i].x,&p[i].y);
    62         p[i].jud=0;
    63     }
    64         getchar();
    65     while(scanf("%c",&c)!=EOF){
    66         if(c=='O'){
    67             scanf("%d",&t1);
    68             getchar();
    69             p[t1].jud=1;
    70 
    71             for(int i=1;i<=n;i++){
    72                 if(i!=t1&&p[i].jud==1){
    73                     if(distanc_2(p[t1],p[i])<=d*d){
    74                         unite(t1,i);
    75                     }
    76                 }
    77             }
    78         }else{
    79             scanf("%d %d",&t1,&t2);
    80             getchar();
    81             if(same(t1,t2)){
    82                 printf("SUCCESS
    ");
    83             }else{
    84                 printf("FAIL
    ");
    85             }
    86         }
    87      }
    88     return 0;
    89 }
  • 相关阅读:
    ListView之setEmptyView的问题
    AdapterView的相关知识。
    分享一个程序猿的真实的爱情故事
    C#中的explicit和implicit了解一下吧
    通俗易懂,什么是.NET Core以及.NET Core能做什么
    目前下载VS2017你可能会遇到这个坑
    .NET Core中使用Dapper操作Oracle存储过程最佳实践
    分享一个.NET平台开源免费跨平台的大数据分析框架.NET for Apache Spark
    从ASP.NET Core2.2到3.0你可能会遇到这些问题
    What?VS2019创建新项目居然没有.NET Core3.0的模板?Bug?
  • 原文地址:https://www.cnblogs.com/TWS-YIFEI/p/6040252.html
Copyright © 2020-2023  润新知