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. hdoj 4712 Hamming Distance(靠人品过的)

    我先解释一下汉明距离  以下来自百度百科 在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的字符不同的个数.换句话说,它就是将 一个字符串变换成另外一个字符串所需要替换的字符个数. 例如: ...

  2. 四、Python基础(1)

    目录 四.Python基础(1) 四.Python基础(1) 1.什么是变量? 一种变化的量,量是记录世界上的状态,变指得是这些状态是会变化的. 2.为什么有变量? 因为计算机程序的运行就是一系列状态 ...

  3. sqoop增量导数据

    sqoop要实现增量导入参数到hive或者hdfs 1.需要在mysql中创建可以自动更新的字段,当插入数据时和更新数据时改字段自动更新,如图中update_time,当数据插入时会记录更新为插入时间 ...

  4. Flink+Druid构建实时OLAP的探索

    场景 k12在线教育公司的业务场景中,有一些业务场景需要实时统计和分析,如分析在线上课老师数量.学生数量,实时销售额,课堂崩溃率等,需要实时反应上课的质量问题,以便于对整个公司的业务情况有大致的了解. ...

  5. H5中的history方法Api介绍

    最近公司在做一个微信公众号,看了项目源码,看到项目中用到了history的Api来进行控制浏览器的历史记录及前进/后退键: 下面来跟大家一起来捋捋history的Api方法和使用: history.p ...

  6. Linux--shell练习题

    1.判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.” ...

  7. 洛谷 P2657 [SCOI2009]windy数

    题意简述 求l~r之间不含前导零且相邻两个数字之差至少为2的正整数的个数 题解思路 数位DP 代码 #include <cstdio> #include <cstring> # ...

  8. 8.9 day30 并发编程 进程理论 进程方法 守护进程 互斥锁

    多道技术 1.空间上的复用 多个程序共用一套计算机硬件 多道技术原理 2.时间上的复用 ​ 切换+保存状态 ​ 1.当一个程序遇到IO操作 操作系统会剥夺该程序的CPU执行权限( 提高了CPU的利用率 ...

  9. 【KakaJSON手册】06_Model转JSON

    前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON 生成JSON和JSONString struct Car: Convertible { ...

  10. c# NPOI 导出23万条记录耗时12秒

    先上测试代码: string connectionString = "Server=localhost;Initial Catalog=******;User ID=sa;Password= ...