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. MinGW GCC下sleep()函数问题

    在MinGW GCC下编译带sleep()函数的测试程序,不管是包含了unistd.h头文件,还是stdio.h.stdlib.h头文件,就是找不到该函数的定义!在linux下,sleep()函数的头 ...

  2. poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

    题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...

  3. 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)

    这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...

  4. HDU 4864 (2014 Multi-University Training Contest 1 )

    考试时,想到了一个很类似的方法,但是总是差那么点,就是这么点,需要不断的努力啊!!! 题解: 基本思想是贪心. 对于价值c=500*xi+2*yi,yi最大影响100*2<500,所以就是求xi ...

  5. iOS开发:应用生命周期

    iOS应用通过委托对象AppDelegate类在应用周期的不同阶段会回调不同的方法,应用周期分为以下五种状态: Not Running(非运行状态).应用没有运行或被系统终止.   Inactive ...

  6. LA 4119 (差分数列 多项式) Always an integer

    题意: 给出一个形如(P)/D的多项式,其中P是n的整系数多项式,D为整数. 问是否对于所有的正整数n,该多项式的值都是整数. 分析: 可以用数学归纳法证明,若P(n)是k次多项式,则P(n+1) - ...

  7. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  8. Java [Leetcode 112]Path Sum

    题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  9. 省常中模拟 Test1 Day1

    临洮巨人 排序 题意:在字符串中找出 A.B.C 三个字母出现次数相同的区间个数. 初步的解法是前缀和,用 a(i), b(i), c(i) 表示在位置 i 之前(包括 i)各有 字母 A.B.C 多 ...

  10. 学习Mongodb(一)

    图片摘录自陈彦铭出品2012.5的<10天掌握MongDB> MongoDB的特点--->面向集合存储,易于存储对象类型的数据--->模式自由--->支持动态查询---& ...