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命令: mount
    梳理一下uboot是如何从nandflash挂载文件系统的
    MDK的优化应用
    面向对象设计思想:面向对象设计的基本原则
    问题
    nodejs安装不了和npm安装不了的解决方法
    []: secureCRT连接ubuntu问题- The remote system refused the connection
    字符设备驱动[深入]:linux cdev详解
    使用MDK将STM32的标准库编译成lib使用
    liteos任务(二)
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4485247.html
Copyright © 2011-2022 走看看