Wireless Network

题目链接:

http://acm.hust.edu.cn/vjudge/contest/123393#problem/A

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

题意:

给出n个点的坐标,一开始任意两点均不联通;

接着给出多个操作:

  1. 恢复点x的联通性,即x可与其他已恢复的点连接.
  2. 查询点x和y是否可达.

    (定义可达:距离小于d,或者经过多条小于d的边)

题解:

很明显的并查集模版题.

距离小于等于d即可合并;

查询时输出两点是否在同一集合.

(不要把FAIL输出成FALL).

注意:先将与点i距离不超过d的点存起来;

当恢复点i后,枚举可连接的点,只有两点都被恢复时才能合并.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1200
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int fa[maxn];
int rank[maxn]; void init_set() {
for(int i=0; i<maxn; i++) {
fa[i] = i;
rank[i] = 0;
}
} int find_set(int x) {
return fa[x] = (x==fa[x]? x:find_set(fa[x]));
} void unit_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if(rank[x] < rank[y]) swap(x, y);
fa[y] = x;
if(rank[x] == rank[y]) rank[x]++;
} LL D;
bool dis[maxn][maxn];
LL x[maxn],y[maxn];
bool vis[maxn]; int main(int argc, char const *argv[])
{
//IN; int n;
while(scanf("%d %lld", &n,&D) != EOF)
{
D = D*D;
init_set();
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) scanf("%lld %lld", &x[i],&y[i]);
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) <= D) {
dis[i][j] = dis[j][i] = 1;
}
}
} char c; int x,y;
while(scanf("%c",&c) != EOF) {
if(c=='O') {
scanf("%d", &x);
vis[x] = 1;
for(int i=1; i<=n; i++) if(dis[x][i] && vis[i])
unit_set(i, x);
}
else if(c=='S') {
scanf("%d %d", &x,&y);
if(find_set(x) == find_set(y)) puts("SUCCESS");
else puts("FAIL");
}
}
} return 0;
}

POJ 2236 Wireless Network (并查集)的更多相关文章

  1. POJ 2236 Wireless Network (并查集)

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

  2. poj 2236 Wireless Network (并查集)

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

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

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

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

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

  5. [并查集] POJ 2236 Wireless Network

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

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

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

  7. POJ 2236 Wireless Network(并查集)

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

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

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

  9. poj 2236 Wireless Network 【并查集】

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

随机推荐

  1. Check if KeyValuePair exists with LINQ's FirstOrDefault

    http://stackoverflow.com/questions/793897/check-if-keyvaluepair-exists-with-linqs-firstordefault 问题: ...

  2. python学习笔记之初识Python

    一直听说python语音的简单易用而又强大,今天终于忍不住借本书,开始接触接触一下它,下面结合书本和自己的一些体会,写一下刚刚接触python的东西,重点写一些和C++有区别的地方. (1)输入inp ...

  3. LA 2218 (半平面交) Triathlon

    题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计 ...

  4. U盘启动格式

    U盘的几种启动模式:USB-HDD.USB-ZIP.USB-HDD+.USB-ZIP+.USB-CDROM 1.  USB-HDD 硬盘仿真模式,DOS启动后显示C:盘,HP U盘格式化工具制作的U盘 ...

  5. java读取照片信息 获取照片拍摄时的经纬度

    项目结构 源码:ImageInfo.zip 第一步:添加需要的架包metadate-extractor.jar 架包下载地址:https://code.google.com/p/metadata-ex ...

  6. c & c++中const

    1 const的基本用法 常变量:  const 类型说明符 变量名 #在c语言中,仍是一个变量.c++中则变为一个常量,比如可以设置为数组的长度 常引用:  const 类型说明符 &引用名 ...

  7. html5极速3D立体式图片相册切换效果

    下载Demo  

  8. Oracle 课程二之Oracle数据库逻辑结构

    完成本课程的学习后,您应该能够: •数据库总体的逻辑结构 •深入理解数据库最小存储单元block •理解行迁移和行链接 •理解高水位线 •Drop.truncate.delete区别   1.数据库的 ...

  9. Spring AOP (上)

    工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保持进步状态.第二:为了提升技术,提高开发能力.第三:保 ...

  10. NPlot开源画图类

    一.net下的图表控件­­­NPlot的基本用法 NPlot的基本用法 图表控件一直是很难找的,特别是免费又强大的.NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散 ...