Wireless Network
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 26131   Accepted: 10880

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

从输入来讲题目的要求,首先输入n代表有n台破损的电脑,然后输入的是d,代表如果两台电脑之间的距离小于等于d则这两台电脑可以相连接。
然后输入O和一个数字代表维修这台电脑,输入S和两个数字a,b是判断a和b是否可以相连接(可以通过其他电脑来相连接),如可以则输出SUCCESS,否则输出FAIL
用并查集来做,如果两台电脑可以连接就用join加入一个集合(并查集概念),最后判断两个的pre就可以判断他们是否可以相连接
值得一提的是我写这题目的时候小小的探索了一下时间问题,发现用scanf确实比cin快(尽管在某些题目cin又比scanf快,好烦呀!!!)用int flag 比 bool flag 快(差不多...)
最后附一张探索过程的截图吧   嘿嘿

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int x,y;
int pre;
} p[];
int flag[];
int n,d;
int findn(int x)
{
return x == p[x].pre ? x : findn(p[x].pre);//注意一定要这样写,如果改成普通的while(x!=p[x].pre)就会时间超限!!!以后可以这样简写!
}
void join(struct node p1,struct node p2)
{
int fx=findn(p1.pre),fy=findn(p2.pre);
if(fx!=fy)
if((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)<=d*d)//判断是否符合加入集合的条件
p[fy].pre = fx;
}
int main()
{
scanf("%d %d",&n,&d);
for(int i=; i<=n; i++)
p[i].pre = i;
memset(flag,,sizeof(flag));
for(int i=; i<=n; i++)
scanf("%d %d",&p[i].x,&p[i].y);
char c;
while(scanf("\n%c",&c)!=EOF)//scanf里面的\n用来吸收上一个scanf产生的空行
{
if(c=='O')
{
int a;
scanf("%d",&a);
flag[a] = ;
for(int i=; i<=n; i++)
{
if(flag[i]&&a!=i)//除i自己外其他点均与他建立关系,至于加不加入集合在join里面判断
join(p[i],p[a]);
}
}
else
{
int a,b;
scanf("%d %d",&a,&b);
if(findn(a)==findn(b))
printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return ;
}

POJ-2236 Wireless Network 顺便讨论时间超限问题的更多相关文章

  1. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  2. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  3. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  4. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  5. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  6. POJ 2236 Wireless Network (并查集)

    Wireless Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/A Description An earthqu ...

  7. poj 2236 Wireless Network 【并查集】

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16832   Accepted: 706 ...

  8. POJ 2236 Wireless Network [并查集+几何坐标 ]

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  9. poj 2236 Wireless Network (并查集)

    链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 ...

随机推荐

  1. 初识Apache NiFi

    一. NiFi介绍 Apache NiFi支持功能强大且可扩展的数据路由,转换和系统中介逻辑的有向图. Apache NiFi的一些高级功能和目标包括: 基于Web的用户界面 设计,控制,反馈和监控之 ...

  2. 使用Java 编写FTP中的长传文件和下载文件

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式,),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方法:通过JDK自带的API实现 ...

  3. Python基础总结之认识lambda函数、map函数、filter() 函数。第十二天开始(新手可相互督促)

    今天周日,白天在学习,晚上更新一些笔记,希望对大家能更好的理解.学习python~ lambda函数,也就是大家说的匿名函数.它没有具体的名称,也可以叫做一句话函数,我觉得也不过分,大家看下代码,来体 ...

  4. Eclipse 连接不上 hadoop 的解决办法

    先说一下我的情况,集群的 hadoop 是 1.0.4 ,之后在虚拟机上搭建了最新稳定版 1.2.1 之后,Eclipse 插件始终连接不上. 出现 Error: Call to 192.168.1. ...

  5. 【游记】NOIP2019初赛

    声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 (编辑中) 文章推荐:[游记]NOIP2019复赛

  6. Thread、ThreadPool、Task、Parallel、Async和Await基本用法、区别以及弊端

    多线程的操作在程序中也是比较常见的,比如开启一个线程执行一些比较耗时的操作(IO操作),而主线程继续执行当前操作,不会造成主线程阻塞.线程又分为前台线程和后台线程,区别是:整个程序必须要运行完前台线程 ...

  7. html学习笔记整理

    网页 1.网页的组成部分 网页是由文字,图片,视频,音频,输入框,按钮这些元素(也就是html标签)组成. 2.浏览网页常用的五大主流浏览器 谷歌,IE,火狐,欧朋,safari.浏览器的内核(渲染引 ...

  8. linux学习总结--linux100day(day2)

    Linux中的哲学--一切皆文件 为了便于操作,我们可以使用secureCRT或Xshell连接到我们的虚拟机. 要用远程工具连接到虚拟机上,我们只需要打开虚拟机上的ssh服务,在xshell中填写主 ...

  9. ASP.NET Core MVC 之区域(Area)

    区域(Area)是一个 ASP.NET MVC 功能,用于将相关功能组织为一个单独的命名空间(用于路由)和文件结构(用于视图).使用区域通过向控制器和操作添加 一个路由参数(area)来创建用于路由目 ...

  10. Sqlserver 游标的写法记录

    ---游标更新删除当前数据 ---1.声明游标 declare orderNum_03_cursor cursor scroll for select OrderId ,userId from big ...