首页 > 其他分享 >poj 2236 Wireless Network 并查集

poj 2236 Wireless Network 并查集

时间:2023-07-11 16:32:16浏览次数:50  
标签:node par computers int 查集 communicate Wireless computer poj


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


POJ Monthly,HQM


1.字符串的输入:1.1    使用%s代替%c,,如果单个的话取m[0]


                               1.2   使用"\n"代替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("\n%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>


标签:node,par,computers,int,查集,communicate,Wireless,computer,poj
From: https://blog.51cto.com/u_16185524/6689490

相关文章

  • POJ 2109 Power of Cryptography 数学题 double和float精度和范围
    PowerofCryptographyTimeLimit:1000MSMemoryLimit:30000KTotalSubmissions:21354Accepted:10799DescriptionCurrentworkincryptographyinvolves(amongotherthings)largeprimenumbersandcomputingpowersofnumbersamongtheseprimes.Workint......
  • poj 1182 食物链 并查集
    食物链TimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:56297Accepted:16500Description动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B,B吃C,C吃A。现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种......
  • 【模板】并查集
    简介并查集是什么并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。并查集其实就是一个树,如果要合并的话就将其中一个的根节点连接到另外一个的根......
  • P2024 [NOI2001] 食物链 || #576. 食物链【NOI2001】 (并查集)
    空降锣鼓空降OJ题解:#include<bits/stdc++.h>usingnamespacestd;intn,k;intd,x,y;intans;intfa[500050];intfind(intx){//找爸爸 if(fa[x]==x) returnfa[x]; returnfind(fa[x]);}intmain(){ cin>>n>>k; for(inti=1;i<=n*3;i++)//开三个并查集风......
  • Cesium学习笔记3——加载topojson和Geojson
    在根目录下新建bucket.css@import"../Build/CesiumUnminified/Widgets/widgets.css";@import"../Build/CesiumUnminified/Widgets/lighter.css";html{height:100%}body{background:#000;color:#eee;font-family:sans-serif;font-size:9pt;padding:0;margin:0;w......
  • #570. 【例4-8】格子游戏 (并查集)
    #570.【例4-8】格子游戏题题题题题题题题题题题题题题分析:并查集解决的是连通性(无向图连通分量)和传递性(家谱关系)问题,并且可以动态的维护。抛开格子不看,任意一个图中,增加一条边形成环当且仅当这条边连接的两点已经联通,于是可以将点分为若干个集合,每个集合对应图中的一个连通块......
  • 并查集学习笔记
    什么是并查集顾名思义,并查集有两个最主要的作用:合并集合和查询某两个元素是不是在同一个集合内。或者说:并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个......
  • POJ 2976:Dropping tests 01 分数规划
    DroppingtestsTimeLimit: 1000MS MemoryLimit: 65536KTotalSubmissions: 12291 Accepted: 4293DescriptionInacertaincourse,youtake n tests.Ifyouget ai outof bi questionscorrectontest i,yourcumulativeaverageisdefinedtobe.Gi......
  • 可删除的并查集
       当并查集要删除某一个节点时,不能直接修改该节点的根节点p[i]=i,如果这个节点不是叶子节点,会导致子树的根节点改变。而要删除单独一个非叶子节点,普通的并查集不好操作。可以在初始化并查集时将每一个节点都当作一个树,给每一个节点创建一个虚构的根节点,进行加边操作时只修......
  • 【线段树】 POJ 3667 Hotel
    这道题和那道HDOJ3308LCIS比较像。。那道题目我就写了超久,当时是不确定保存什么信息。。这道题也写了很久,主要是各种低级错误,找错找了很久,超级火。。。。#include<iostream>#include<sstream>#include<algorithm>#include<vector>#include<queue>#include<stack>#inc......