zoukankan      html  css  js  c++  java
  • Wireless Network--poj2236(并查集)

    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

    题目链接:http://poj.org/problem?id=2236
    题目要求就是:给你n个坏的计算机 和 限制的连通距离d,然后输入这几台计算机的坐标。
    然后输入一个字符O或S,O表示后面输入的 p 这台计算机修好了,S是要你求p和q这两台计算机是否能连通,连通输出SUCCESS否则输出FAIL
    现在说明两台计算机能连通的条件:一、这两台计算机的距离要<=d,二、这两台计算机要求都已经修好了

    我因为把FAIL写成FALL,wa的我都想哭了,好伤心

    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #define N 1100
    using namespace std;
    struct node
    {
        int x,y;
    };
    node a[N];
    int d,fa[N],vis[N];
    
    int judge(int x,int y)
    {
        int d0=(a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y);
        if(d0<=d*d)
            return 1;
        return 0;
    }
    
    int Find(int x)
    {
        if(x!=fa[x])
            fa[x]=Find(fa[x]);
        return fa[x];
    }
    int main()
    {
        int n,i,k,x,y;
        char str[N];
        scanf("%d%d",&n,&d);
        for(i=1;i<=n;i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        for(i=1;i<=n;i++)
            fa[i]=i;
        k=0;
        while(scanf("%s",str)!=EOF)
        {
            if(strcmp("O",str)==0)
            {
                scanf("%d",&x);
                for(i=0;i<k;i++)
                {
                    if(judge(x,vis[i])==1)//判断两点间的距离是否<=d;
                    {
                        int dx=Find(x);
                        int dy=Find(vis[i]);
                        if(dx!=dy)
                            fa[dy]=dx;
                    }
                }
                vis[k]=x;
                k++;
            }
            else
            {
                scanf("%d%d",&x,&y);
                if(Find(x)==Find(y))
                    printf("SUCCESS
    ");
                else
                    printf("FAIL
    ");
            }
        }
        return 0;
    }
    View Code
    复习时又写了一遍感觉还可以:
    View Co
  • 相关阅读:
    linux 图片压缩
    (四)基于商品属性的相似商品推荐算法——推荐与评分高的商品属性相似的商品
    (三)基于商品属性的相似商品推荐算法——批量处理商品属性,得到属性前缀及完整属性字符串
    老王为测量初学者准备的测量宝典
    全站仪 经纬仪 水准仪 操作演示视频教程 建筑工程测量放线7日通
    【地铁测量-车站】02 车站模板抄平思路
    【地铁测量-车站】01 模板放样正反算妙用
    手机操控全站仪安卓版 测量员.app
    【卡西欧Fx-5800p】市场分析 ppt
    【卡西欧Fx5800-p程序】01 坐标转换程序-带注释
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4485247.html
Copyright © 2011-2022 走看看