http://poj.org/problem?id=2236

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

题解:并查集

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; int f[1010];
int N, d;
bool rep[1010];
int q, p, n; int st[1010];
int en[1010]; void init() {
for(int i = 1; i <= N; i ++) {
f[i] = i;
rep[i] = false;
}
} int Find(int x) {
if(f[x] != x) f[x] = Find(f[x]);
return f[x];
} void Merge(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if(fx != fy)
f[fx] = fy;
} int main() {
scanf("%d%d", &N, &d);
init();
for(int i = 1; i <= N; i ++)
scanf("%d%d", &st[i], &en[i]); int f1, f2;
char op;
while(~scanf("%c", &op)) {
if(op == 'O') {
scanf("%d", &n);
rep[n] = true;
for(int i = 1; i <= N; i ++) {
if(i != n && rep[i] && (en[i] - en[n]) * (en[i] - en[n]) + (st[i] - st[n]) * (st[i] - st[n]) <= d * d) {
f1 = Find(n);
f2 = Find(i);
f[f1] = f2;
}
}
}
else if(op == 'S'){
scanf("%d%d", &q, &p);
f1 = Find(p);
f2 = Find(q); if(f1 == f2) printf("SUCCESS\n");
else printf("FAIL\n");
}
}
return 0;
}

  

POJ A-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: 25022   Accepted: 103 ...

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

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

  4. POJ - 2336 Wireless Network

    Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...

  5. POJ 2236 Wireless Network(并查集)

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

  6. POJ 2236 Wireless Network (并查集)

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

  7. POJ 2236 Wireless Network (并查集)

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

  8. poj 2236 Wireless Network 【并查集】

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

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

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

  10. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

随机推荐

  1. 第10章 新建工程-库函数版—零死角玩转STM32-F429系列

    第10章     新建工程—库函数版 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  2. caffe安装中opencv的各种库问题

    提示有些库 high**** opencv的问题,好像是这几个库版本冲突,不要用anaconda里的lib库,用系统的库就行了,删掉或者从新链接过去.

  3. Kettle报表自动化

    来自我们牛逼哄哄的东哥的笔记 1.   2. 3. 选择数据库链接 贴报表SQL 4. 文件名:选择路径,excel文件由kettle自动创建,自己只需输入创建文件的名称. 拓展名:后缀写上 5. 此 ...

  4. 对于未来学习Linux的决心书,以此为鉴

    学习Linux的决心书 我叫曹佳佳,来自祖国的大西北甘肃庆阳,2016年大专毕业之后从事自己的专业风力发电行业工作了两年多在从事风电行业的过程中越来越感觉到自己的薪资待遇和以后的发展空间越来越小,而且 ...

  5. DB - RDMS - MySQL优化

    慢SQL会消耗打来难过的数据库CPU资源,特别是频繁执行的慢SQL语句,会造成大量任务的堆积,CPU瞬间增大.

  6. float 浮动详解

    浮动(float):浮动原先设定时主要是用于文本环绕图像设定的,后来发现其在css布局中有很大的帮助,故渐渐使用浮动. 浮动后的元素脱离了文档的普通流,使得浮动的元素不占据文档的位置,其他元素可以覆盖 ...

  7. python 函数复习

    # 函数 # 可读性强 复用性强 # def 函数名(): # 函数体 #return 返回值 # 所有的函数 只定义不调用就一定不执行 #先定义后调用 #函数名() #不接收返回值 #返回值 = 函 ...

  8. 虚拟机linux桥接联网问题

    Linux系统为redhat5.8 虚拟机的版本:vm8.0 本人刚刚开始接触linux,今日需要通过linux进行联网,因此也学习了一点点关于虚拟机的联网的知识,在此与大家进行分享,希望大家可以之处 ...

  9. [BZOJ1045] [HAOI2008] 糖果传递 (中位数)

    Description 题目链接 Solution 这题跟数列的中位数有关, 具体证明见刘汝佳的蓝皮书里 Code #include <cstdio> #include <algor ...

  10. 15.4,redis不重启,切换RDB备份到AOF备份

      确保redis版本在2.2以上 [root@pyyuc /data ::]#redis-server -v Redis server v= sha=: malloc=jemalloc- bits= ...