zoukankan      html  css  js  c++  java
  • poj 2236 Wireless Network 并查集

    Wireless Network
    Time Limit: 10000MS
    Memory Limit: 65536K
    Total Submissions: 20499
    Accepted: 8608

    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.字符串的输入:1.1    使用%s代替%c,,如果单个的话取m[0]
                                   1.2   使用" "代替getchar(),来读取空格
    2.并查集:            2.1   访问祖先节点使用find()代替node[i].par!!!!
    3.语法知识:        3.1  a^2代表的是a与2进行异火运算而不是a的平方,,要平方只能a*a
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    struct Node{
      int x,y,par;
    }node[1005];
    int init(int n)
    {
        for(int i=1;i<=n;i++)
            node[i].par=-1;
    }
    int find(int n)
    {
        if(node[n].par!=n)
            node[n].par=find(node[n].par);
        return node[n].par;
    }
    int unit(int x,int y)
    {
        int rx=find(x);
        int ry=find(y);
        if(rx!=ry)
            node[ry].par=rx;
        return 0;
    }
    int main()
    {
        int n,d;
        cin>>n>>d;
        init(n);
        for(int i=1;i<=n;i++)
            cin>>node[i].x>>node[i].y;
        char m[3];//1.1
        int k,s,e,q;
        while(scanf("
    %s",m)!=EOF)//1.2
        {
            if(m[0]=='O')
               {
                    cin>>k;
                    node[k].par=k;
                    for(int i=1;i<=n;i++)
                        if(node[i].par!=-1)
                         if((node[i].x-node[k].x)*(node[i].x-node[k].x)+
                            (node[i].y-node[k].y)*(node[i].y-node[k].y)<=d*d) //3.1
                            unit(i,k);
               }
            else if(m[0]=='S')
               {
                    cin>>s>>e;
                    if(node[s].par!=-1&&node[e].par!=-1&&
                       find(node[s].par)==find(node[e].par))//2.1
                         {
                             cout<<"SUCCESS"<<endl;
                             continue;
                         }
                        else
                        {
                             cout<<"FAIL"<<endl;
                             continue;
                        }
                    }
        }
        return 0;
    }
    


    <span style="color:#3333ff;"></span>

  • 相关阅读:
    leetcode 第 44 场双周赛 1 1734. 解码异或后的排列 C
    leetcode 第 44 场双周赛 1 1732. 找到最高海拔 C
    2014浙江省赛 ZOJ
    2018沈阳区域赛现场赛 Gym
    山东省ACM多校联盟省赛个人训练第六场 poj 3335 D Rotating Scoreboard
    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
    计算机爱好者协会技术贴markdown第四期
    计算机爱好者协会技术贴markdown第三期
    摇骰子
    PAT 1003 dijkstra
  • 原文地址:https://www.cnblogs.com/smilesundream/p/6642539.html
Copyright © 2011-2022 走看看